Я использую CameraX для захвата изображения, а затем использую комплект Firebase ML для распознавания текста.
CameraX возвращает ImageProxy из ImageCapture, когда мы берем пи c.
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, имеет недопустимый формат.