无法使用相机x

时间:2019-09-04 10:36:47

标签: android-camerax

我正在尝试使用camerax拍摄多张照片,但只拍摄了第一张照片,代码和日志输出将显示我的意思。

这是代码:

        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);
            }
        });

相关的日志输出为:

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

但是在称为Capture Success 2的新照片从未发生

虽然根据文档拍摄了两张照片没有错:

  

TakePicture立即返回,并在捕获完成后调用侦听器以提供结果。多次调用takePicture将在捕获前一张照片后开始顺序拍摄照片。

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

1 个答案:

答案 0 :(得分:0)

这似乎是库的问题,这是错误: https://issuetracker.google.com/issues/140518887

更新 根据团队对此问题的评论,这似乎不是一个错误,问题是我必须在处理完毕后致电image.close();,以便我的代码应为:

 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();
            }

        });