首页 > 解决方案 > CameraX - ImageCapture 返回的 ImageProxy 无效

问题描述

我正在使用 CameraX 捕获图像,然后使用 Firebase ML 套件来识别文本。

当我们拍照时,CameraX 从 ImageCapture 中返回 ImageProxy。

imageCapture.takePicture(Executors.newSingleThreadExecutor(), new ImageCapture.OnImageCapturedListener() {
                @Override
                public void onCaptureSuccess(ImageProxy image, int rotationDegrees) {
                    super.onCaptureSuccess(image, rotationDegrees);

                    startTextRecognization(image, rotationDegrees);
                }

                @Override
                public void onError(@NonNull ImageCapture.ImageCaptureError imageCaptureError, @NonNull String message, @Nullable Throwable cause) {
                    super.onError(imageCaptureError, message, cause);

                }
            });

将该图像传递给 Firebase -

public void startTextRecognizing(ImageProxy imageProxy, int rotationDegrees) {
    this.imageProxy = imageProxy;
    this.rotationDegrees = rotationDegrees;

    if (imageProxy == null || imageProxy.getImage() == null) {
        return;
    }

    Image mediaImage = imageProxy.getImage();
    FirebaseVisionImage firebaseVisionImage =
            FirebaseVisionImage.fromMediaImage(mediaImage, rotationDegrees);

    FirebaseVisionTextRecognizer detector = FirebaseVision.getInstance()
            .getOnDeviceTextRecognizer();

    Task<FirebaseVisionText> result =
            detector.processImage(firebaseVisionImage)
                    .addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
                        @Override
                        public void onSuccess(FirebaseVisionText firebaseVisionText) {
                            extractText(firebaseVisionText);
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Log.d("*Rahul", "Text Recognization Failed");
                        }
                    });

    //result.getResult();

}

我收到一个错误-

issue capture request for camera 0
2020-02-12 18:27:27.497 6788-6842/com.example.myapplication D/CaptureSession: Issuing capture request.
2020-02-12 18:27:27.786 6788-7173/com.example.myapplication D/*Rahul:  Success Fully Captured - 
2020-02-12 18:27:27.794 6788-6788/com.example.myapplication D/AndroidRuntime: Shutting down VM


--------- beginning of crash
2020-02-12 18:27:27.795 6788-6788/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 6788
java.lang.IllegalStateException: Image is already closed
    at android.media.Image.throwISEIfImageIsInvalid(Image.java:72)
    at android.media.ImageReader$SurfaceImage.getFormat(ImageReader.java:819)
    at com.google.firebase.ml.vision.common.FirebaseVisionImage.fromMediaImage(com.google.firebase:firebase-ml-vision@@24.0.1:6)
    at com.example.myapplication.TextRocognizationHelper.startTextRecognizing(TextRocognizationHelper.java:40)
    at com.example.myapplication.CameraActivity$3.run(CameraActivity.java:198)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7356)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)


--------- beginning of system

你能帮忙吗,我在哪里做错了?ImageCapture 返回的图像格式无效。

标签: androidfirebasefirebase-mlkitandroid-camerax

解决方案


当您调用super.onCaptureSuccess(image, rotationDegrees)它时会关闭图像,因此要么在您的文本识别方法之后移动它,要么删除该行并稍后通过调用来关闭图像image.close()

题外话 -您使用的是哪个CameraX 版本?我没有ImageCapture.OnImageCapturedListener在最新的库版本中找到该类。您应该使用可用的最新版本,因为它仍处于 alpha 阶段,并且核心 api 正在发生变化(尽管开发人员说当时最新的 - alpha10 接近 beta 版本,因此核心 api 现在不应该有太大变化)。


推荐阅读