使用Camera2 API android捕获双摄像头JPEG图像

时间:2016-12-12 07:10:43

标签: android android-camera2

我正在尝试从Snapdragon 820平台上的两个摄像头并行获取JPEG图像。

我没有得到第一个相机图像回调。我只得到第二个相机JPEG回调。

这是我的代码:

protected void takePictureBack() {
    Log.d(TAG, "takePictureBack() called");
    if (null == cameraDeviceBack) {
        Log.e(TAG, "cameraDeviceBack is null");
        return;
    }
    try {

        final File file_back = new File(Environment.getExternalStorageDirectory() + "/pic_back.jpg");


        final CaptureRequest.Builder captureBuilderBack = cameraDeviceBack.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        List<Surface> outputSurfaces = new ArrayList<Surface>(3);
        outputSurfaces.add(new Surface(mTextureViewBack.getSurfaceTexture()));
            ImageReader reader = ImageReader.newInstance(640, 480, ImageFormat.JPEG, 1);
            outputSurfaces.add(reader.getSurface());
            captureBuilderBack.addTarget(reader.getSurface());

            ImageReader.OnImageAvailableListener readerListenerBack = new ImageReader.OnImageAvailableListener() {
                @Override
                public void onImageAvailable(ImageReader reader) {
                    Log.d(TAG, "onImageAvailable() called with: reader = [" + reader + "]");
                    if (reader.getImageFormat() == ImageFormat.JPEG) {
                        Log.d(TAG, "onImageAvailable() called with back: reader = JPEG");
                        Image image = null;
                        try {
                            image = reader.acquireLatestImage();
                            ByteBuffer buffer = image.getPlanes()[0].getBuffer();
                            byte[] bytes = new byte[buffer.capacity()];
                            buffer.get(bytes);
                            save(bytes);

                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            if (image != null) {
                                image.close();
                            }
                        }
                    }
                }

                private void save(byte[] bytes) throws IOException {
                    OutputStream output = null;
                    try {
                        output = new FileOutputStream(file_back);
                        output.write(bytes);
                    } finally {
                        if (null != output) {
                            output.close();
                        }
                    }
                }

            };
            reader.setOnImageAvailableListener(readerListenerBack, mBackgroundHandlerBack);


        captureBuilderBack.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);



        final CameraCaptureSession.CaptureCallback captureListenerBack = new CameraCaptureSession.CaptureCallback() {
            @Override
            public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
                super.onCaptureCompleted(session, request, result);
                if (DEBUG) Log.d(TAG, "onCaptureCompleted: take picture back successfully");
                //Toast.makeText(getActivity(), "Take picture successfully", Toast.LENGTH_SHORT).show();
                createCameraPreviewBack();
                mCaptureResultBack = result;
            }
        };
        cameraDeviceBack.createCaptureSession(outputSurfaces, new CameraCaptureSession.StateCallback() {
            @Override
            public void onConfigured(CameraCaptureSession session) {
                try {
                    session.capture(captureBuilderBack.build(), captureListenerBack, mBackgroundHandlerBack);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onConfigureFailed(CameraCaptureSession session) {
            }
        }, mBackgroundHandlerBack);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

前置摄像头捕获代码也一样。

单个相机JPEG捕捉工作正常。

任何想法,为什么我没有获得bot JPEG图像回调?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我们需要将“ImageReader reader”作为全局变量。

通过这种改变,我可以从两个相机中获得两个JPEG。