Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't take multiple Images using camerax

I am trying to take multiple pictures using camerax but only the first picture is taken, code and log output will show what I mean.

Here is the code :

        Log.d(TAG, "------------------ taking new picture1");
        mImageCapture.takePicture(new ImageCapture.OnImageCapturedListener() {
            @Override
            public void onCaptureSuccess(ImageProxy imageProxy, int rotationDegrees) {
                Image image = imageProxy.getImage();
                Log.d(TAG, "taking new picture onCapture Success 1 called");
            }

            @Override
            public void onError(ImageCapture.UseCaseError useCaseError, String message, @Nullable Throwable cause) {
                super.onError(useCaseError, message, cause);
                Log.d(TAG, "--------- error in image capture 1" + message);


            }
        });

        Log.d(TAG, "------------------ taking new picture 2");
        mImageCapture.takePicture(new ImageCapture.OnImageCapturedListener() {
            @Override
            public void onCaptureSuccess(ImageProxy imageProxy, int rotationDegrees) {
                Image image = imageProxy.getImage();
                Log.d(TAG, "taking new picture onCapture Success 2 called");


            }

            @Override
            public void onError(ImageCapture.UseCaseError useCaseError, String message, @Nullable Throwable cause) {
                super.onError(useCaseError, message, cause);
                Log.d(TAG, "--------- error in image capture 2" + message);
            }
        });

The relevant log output is:

2019-09-04 12:23:00.978 28970-29006/com.example.david.digified_android D/ScanDocumentFragment: ------------------ taking new picture1
2019-09-04 12:23:00.980 28970-29006/com.example.david.digified_android D/ScanDocumentFragment: ------------------ taking new picture 2
2019-09-04 12:23:02.063 28970-28970/com.example.david.digified_android D/ScanDocumentFragment: taking new picture onCapture Success 1 called

but taking new picture onCapture Success 2 called never happens

although according to documentation taking two pictures is not wrong :

TakePicture returns immediately and a listener is called to provide the results after the capture completes. Multiple calls to takePicture will take pictures sequentially starting after the previous picture is captured.

https://developer.android.com/reference/androidx/camera/core/ImageCapture?hl=en

like image 635
David Ibrahim Avatar asked Jan 25 '23 21:01

David Ibrahim


2 Answers

It seems this is an issue with the library, and here's the bug: https://issuetracker.google.com/issues/140518887

Update It seems it's not a bug according to the comments on the issue by the team and the problem is that I have to call image.close(); when I finish processing so that my code should be :

Log.d(TAG, "------------------ taking new picture1");
mImageCapture.takePicture(new ImageCapture.OnImageCapturedListener() {
   @Override
   public void onCaptureSuccess(ImageProxy imageProxy, int rotationDegrees) {
      Image image = imageProxy.getImage();
      Log.d(TAG, "taking new picture onCapture Success 1 called");
      image.close();
   }

});

Log.d(TAG, "------------------ taking new picture 2");
mImageCapture.takePicture(new ImageCapture.OnImageCapturedListener() {
   @Override
   public void onCaptureSuccess(ImageProxy imageProxy, int rotationDegrees) {
      Image image = imageProxy.getImage();
      Log.d(TAG, "taking new picture onCapture Success 2 called");
      image.close();
   }
});
like image 159
David Ibrahim Avatar answered Jan 28 '23 10:01

David Ibrahim


Phenomenon problem:

After calling mImageCapture.takePicture () twice in a row, there is no response after calling mImageCapture.takePicture () again. If it exits, it will enter the onError callback, indicating that the camera cannot be found.

In this case, you will find that the console has two messages as follows:

D/ImageCapture: Send image capture request [current, pending] = [0, 1].

W/ImageCapture: Too many images acquired. Close the image so that the next image can be processed.

reasons:

The captured image is too much and must be closed to run down. Then, look at the source code of ImageCapture and search for imageProxy.close(), and found that there are four places: two places are called during capture and two places are judging not called at the moment, that is, there is no close after taking a picture and return successfully under normal circumstances, so this will cause this problem.

Solution:

In the successful callback function of mImageCapture.takePicture (), manually close the image after using it, and this problem can be solved.

mImageCapture.takePicture(new ImageCapture.OnImageCapturedListener(){
        @Override
        public void onCaptureSuccess(ImageProxy imageProxy, int rotationDegrees) {
            //Do something
            Log.d(TAG, "taking new picture onCapture Success 1 called");
            imageProxy.close();
        }

        @Override
        public void onError(ImageCapture.UseCaseError useCaseError, String message, @Nullable Throwable cause) {
            super.onError(useCaseError, message, cause);
            Log.d(TAG, "--------- error in image capture 1" + message);


        }
    });

resource: https://blog.csdn.net/qq_37980878/article/details/120060170

like image 25
Claudio Avatar answered Jan 28 '23 11:01

Claudio