无法以图像格式YUV_420_888创建CaptureSession

时间:2017-09-06 06:56:08

标签: android android-camera2

我正在尝试使用推荐格式YUV_420_888进行相机预览,  map.isOutputSupportedFor(ImageFormat.YUV_420_888)返回true,map.getOuputSizes(ImageFormat.YUV_420_888)结果包含大小640x480,但代码仍然失败。

// We set up a CaptureRequest.Builder with the output Surface.
        mPreviewRequestBuilder
                = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        mPreviewRequestBuilder.addTarget(surface);

        // Here, we create a CameraCaptureSession for camera preview.
        //mCameraDevice.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()),
        ImageReader imageReader = ImageReader.newInstance(640, 480, ImageFormat.YUV_420_888, 2);
        imageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
            @Override
            public void onImageAvailable(ImageReader reader) {

            }
        }, null);
        //mCameraDevice.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()),
        mCameraDevice.createCaptureSession(Arrays.asList(surface, imageReader.getSurface()),
                new CameraCaptureSession.StateCallback() {

                    @Override
                    public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
                        // The camera is already closed
                        if (null == mCameraDevice) {
                            return;
                        }

                        // When the session is ready, we start displaying the preview.
                        mCaptureSession = cameraCaptureSession;
                        try {
                            // Auto focus should be continuous for camera preview.
                            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                                    CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
                            // Flash is automatically enabled when necessary.
                            setAutoFlash(mPreviewRequestBuilder);

                            // Finally, we start displaying the camera preview.
                            mPreviewRequest = mPreviewRequestBuilder.build();
                            mCaptureSession.setRepeatingRequest(mPreviewRequest,
                                    mCaptureCallback, mBackgroundHandler);
                        } catch (CameraAccessException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onConfigureFailed(
                            @NonNull CameraCaptureSession cameraCaptureSession) {
                        showToast("Failed");
                    }
                }, null
        );

logcat错误消息:

  

09-06 14:47:06.238 17720-17783 / com.example.android.camera2basic E / AndroidRuntime:FATAL EXCEPTION:RequestThread-0
  过程:com.example.android.camera2basic,PID:17720
  java.lang.NullPointerException:尝试在空对象引用上调用虚方法'int android.util.Size.getWidth()'   在android.hardware.camera2.legacy.RequestThreadManager.checkAspectRatiosMatch(RequestThreadManager.java:590)
  在android.hardware.camera2.legacy.RequestThreadManager.configureOutputs(RequestThreadManager.java:441)
  在android.hardware.camera2.legacy.RequestThreadManager.access $ 800(RequestThreadManager.java:61)
  在android.hardware.camera2.legacy.RequestThreadManager $ 5.handleMessage(RequestThreadManager.java:658)
  在android.os.Handler.dispatchMessage(Handler.java:98)
  在android.os.Looper.loop(Looper.java:135)
  在android.os.HandlerThread.run(HandlerThread.java:61)

0 个答案:

没有答案