1

我正在使用 Camera2 api 来显示相机的预览。我还想实现一个 ImageReader 来处理图像。我有一个开始预览功能。当我称之为预览时,它只是黑色的。如果我从 Arrays.asList() 中删除“mimageReader.getSurface()”,我可以看到相机预览。如何显示相机预览并使用 ImageReader?

 private void startPreview() 
  {
        List<Surface> outputSurfaces = new ArrayList<>();
        List surfaces = new ArrayList<>();

        SurfaceTexture surfaceTexture =  textureView.getSurfaceTexture();
        surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
        Surface previewSurface = new Surface(surfaceTexture);

        try {
            mCaptureRequestBuilder = _cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);




           mCaptureRequestBuilder.addTarget(previewSurface);

            _cameraDevice.createCaptureSession(Arrays.asList(previewSurface,mimageReader.getSurface()),
                    new CameraCaptureSession.StateCallback() {
                        @Override
                        public void onConfigured(CameraCaptureSession session) {
                            Log.d("", "onConfigured: startPreview");

                            try {
                                session.setRepeatingRequest( mCaptureRequestBuilder.build(),null,mthreadhandler);
                            } catch (CameraAccessException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onConfigureFailed(CameraCaptureSession session) {
                            Log.d("", "onConfigureFailed: startPreview");

                        }
                    }, null);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }
4

2 回答 2

0

您必须创建一个 ImageReader 的新实例,设置其 onImageAvailableListener(您可以在此处处理预览帧)并将其添加到 PreviewRequestBuilder。例如:

mImageReader = ImageReader.newInstance(
                mPreviewSize.getWidth(), mPreviewSize.getHeight(), ImageFormat.YUV_420_888, 2);

mImageReader.setOnImageAvailableListener(mOnGetPreviewListener, mBackgroundHandler);

mPreviewRequestBuilder.addTarget(mImageReader.getSurface());
于 2017-07-05T02:23:31.250 回答
0

可能是因为你的函数 startPreview() 没有被调用,当我们没有得到 surfaceTexture 值时就会发生这种情况。

这是我的 startPreview 方法

protected void createCameraPreview() {
    try {
        SurfaceTexture texture = textureView.getSurfaceTexture();
        assert texture != null;

        texture.setDefaultBufferSize(imageDimension.getWidth(), imageDimension.getHeight());
        Surface surface = new Surface(texture);

        captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);

        captureRequestBuilder.addTarget(surface);
        cameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback(){
            @Override
            public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
                //The camera is already closed
                if (null == cameraDevice) {
                    return;
                }
                // When the session is ready, we start displaying the preview.
                cameraCaptureSessions = cameraCaptureSession;
                updatePreview();
            }
            @Override
            public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
                Toast.makeText(MainActivity.this, "Configuration change", Toast.LENGTH_SHORT).show();
            }
        }, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

然后我调用 updatePreview 函数重新开始预览

protected void updatePreview() {
    if(null == cameraDevice) {
        Log.e(TAG, "updatePreview error, return");
    }
    captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
    try {
        cameraCaptureSessions.setRepeatingRequest(captureRequestBuilder.build(), null, mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
于 2019-07-25T09:43:01.217 回答