Reputation: 395
I am using CameraX to capture an image and then using Firebase ML kit to recognise Text.
CameraX return ImageProxy in from ImageCapture when we take pic.
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);
}
});
Passing that image to 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();
}
I am getting an error-
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
Can you help as, where i am doing wrong? The image returned by ImageCapture is coming invalid format.
Upvotes: 1
Views: 3137
Reputation: 770
When you call super.onCaptureSuccess(image, rotationDegrees)
it closes the image, so either move it after your text recognition method or remove the line and close the image yourself later by calling image.close()
.
Off topic - what CameraX version are you using? I didn't find the ImageCapture.OnImageCapturedListener
class in the newest library version. You should use newest one available because it's still in alpha and the core api is changing (although the devs said that the newest at the time - alpha10 is near the beta release so the core api shouldn't change much now).
Upvotes: 3