2
votes

I am working with the Camera2 API and I am following a tutorial. I have the TextureView set up and I have the SurfaceTextureListener on that TextureView. When the listener is invoked, I use the CameraManager to fetch the camera id I want to use and use the width and height provided to me to set the Preview Size. I also use the CameraManager to open the camera specifying the camera id and the Camera Device State Callback. On the onOpened override method, I invoke a method called startPreview():

private void startPreview() {
    SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
    surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
    Surface previewSurface = new Surface(surfaceTexture);

    try {
        mCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        mCaptureRequestBuilder.addTarget(previewSurface);

        mCameraDevice.createCaptureSession(Arrays.asList(previewSurface, mImageReader.getSurface()),
                new CameraCaptureSession.StateCallback() {
                    @Override
                    public void onConfigured(CameraCaptureSession session) {
                        Log.d(TAG, "onConfigured: startPreview");
                        mPreviewCaptureSession = session;
                        try {
                            mPreviewCaptureSession.setRepeatingRequest(mCaptureRequestBuilder.build(),
                                    null, mBackgroundHandler);
                        } catch (CameraAccessException e) {
                            e.printStackTrace();
                        }
                    }

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

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

What is a Capture Request and Capture Session? The Android documentation states createCaptureRequest:

Create a CaptureRequest.Builder for new capture requests

Ok, so what is a CaptureRequest.Builder? The documentation:

A builder for capture requests.

Both definitions reference the others without giving any meaning.

1
Could you provide source code that builds up? Any links - E J Chathuranga

1 Answers

3
votes

A CaptureRequest defines the parameters for camera device (e.g. exposition, resolution). The Camera2 API provides templates to make it easier to prepare the best CaptureRequest, fine tuned for the specific camera and for the purpose.

To pass a CaptureRequest to the camera device, we use a CameraCaptureSession, which provides the context for single (e.g. taking a photo) or repeated (e.g. displaying live preview) requests.

Creating a session is an expensive operation and can take several hundred milliseconds, since it requires configuring the camera device's internal pipelines and allocating memory buffers for sending images to the desired targets.