Detailed explanation of amera2 Preview in Android development

  • 2021-12-19 06:45:33
  • OfStack

Preface to the catalog 1. What modules are needed for Camera2 Preview 2. Functions and relationships between modules 2.1. 1 SurfaceTextureListener2.1. 1 For SurfaceTexture, first look at the description of SurfaceTexture 2.1. 2 Use of SurfaceTextureListener 2.2. 2 Use CameraManager to open Camera2.3 CameraDevice StateCallback2.3. 1 StateCallback 2.3. 2 Example code for StateCallback 2.4 CameraDevice createCaptureSession2.4. 1 New CaptureRequest. Builder2.4. 2 New CameraDevice createCaptureSession2.4. 3 Create CaptureRequest Summary

Preface

Camera2 is the new Camera framework of Android. As a whole, Camera2 provides many standard interfaces for applications, so that more functions can be controlled by parameters. However, flexibility also brings complexity to the architecture. In this paper, we discuss the minimum set of modules used in Camera2 by discussing the Preview function of Camera2.

1. What modules are required for Camera2 Preview

To sum up, the following modules are used.

SurfaceTextureListener of SurfaceTexture CameraManager StateCallback of CameraDevice createCaptureSession of CameraDevice CaptureRequest. Builder, CaptureRequest

2. Functions of each module and the relationship between them

The following is a detailed analysis of the functions and relationships of these modules.

2.1 SurfaceTextureListener of SurfaceTexture

2.1. 1 See the description of SurfaceTexture first

The main purpose is to receive camera preview data and display it on UI.

Captures frames from an image stream as an OpenGL ES texture.
Frames are captured from the image stream as OpenGL ES textures.

The image stream may come from either camera preview or video decode. A android.view.Surface created from a SurfaceTexture can be used as an output destination for the android.hardware.camera2 , android.media.MediaCodec , android.media.MediaPlayer , and android.renderscript.Allocation APIs. When updateTexImage is called, the contents of the texture object specified when the SurfaceTexture was created are updated to contain the most recent image from the image stream. This may cause some frames of the stream to be skipped.

2.1. 2 Use of SurfaceTextureListener

You can use this Listener to get notifications when the surface texture associated with this Surface Texture is available.
Upon receipt of an SurfaceTexture availability notification, an action of initializing the camera is performed.
The sample code is as follows,


private final SurfaceTextureListener mSurfaceTextureListener = new SurfaceTextureListener() {
        @Override
        public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
            Log.i(TAG, "onSurfaceTextureAvailable: ++");
            try {
                Log.i(TAG, "onCreate: call initCamera()");
                initCamera();
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
        }
 
        @Override
        public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {
            Log.i(TAG, "onSurfaceTextureSizeChanged: ++");
        }
 
        @Override
        public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
            Log.i(TAG, "onSurfaceTextureDestroyed: ++");
            return false;
        }
 
        @Override
        public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {
            Log.i(TAG, "onSurfaceTextureUpdated: ++");
        }
    };

2.2 CameraManager

2.2. 1 Role of CameraManager

A system service manager for detecting, characterizing, and connecting to CameraDevices .
A system service manager for detecting, characterizing and connecting camera equipment.

2.2. 2 Open Camera with CameraManager

The sample code is as follows,

Note:
The first parameter of openCamera is which camera to open, and 0 stands for the rear camera; 1 stands for the front camera; 2 stands for external camera. The front camera is turned on here.
The second parameter is StateCallback of CameraDevice, which will be resolved later.
The third parameter is Handler, where we put Handler in a newly created thread;


CameraManager manager = (CameraManager) this.getSystemService(Context.CAMERA_SERVICE);
manager.openCamera("1", stateCallback, mBackgroundHander);

2.3 StateCallback of CameraDevice

2.3. 1 Role of StateCallback

A callback objects for receiving updates about the state of a camera device.
Callback object used to receive camera device status updates.

2.3. 2 Sample code for StateCallback

We will create Preview Session in the onOpened () function!


 
            CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {
                @Override
                public void onOpened(@NonNull CameraDevice camera) {
                    Log.i(TAG, "onOpened: ++");
                    mCameraDevice = camera;
                    createPreviewSession();
                }
 
                @Override
                public void onDisconnected(@NonNull CameraDevice camera) {
                    Log.i(TAG, "onDisconnected: ++");
                }
 
                @Override
                public void onError(@NonNull CameraDevice camera, int error) {
                    Log.i(TAG, "onError: ++");
                }
            };

2.4 createCaptureSession of CameraDevice

2.4. 1 New CaptureRequest. Builder

A builder for capture requests.

2.4. 2 createCaptureSession for new CameraDevice

A configured capture session for a CameraDevice , used for capturing images from the camera or reprocessing images captured from the camera in the same session previously.

2.4. 3 Creating CaptureRequest

An immutable package of settings and outputs needed to capture a single image from the camera device.

Contains the configuration for the capture hardware (sensor, lens, flash), the processing pipeline, the control algorithms, and the output buffers. Also contains the list of target Surfaces to send image data to for this capture.

The sample code for this section is as follows,


 
    private void createPreviewSession() {
        SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
        // should be preview size, will got it later
        surfaceTexture.setDefaultBufferSize(mTextureView.getWidth(), mTextureView.getHeight());
        mImageReader = ImageReader.newInstance(mTextureView.getWidth(), mTextureView.getHeight(),
                ImageFormat.JPEG, /*maxImages*/2);
 
        Surface surface =new Surface(surfaceTexture);
 
        try {
            mPreviewRequstBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
            mPreviewRequstBuilder.addTarget(surface);
 
            mCameraDevice.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()),
                    new CameraCaptureSession.StateCallback() {
                        @Override
                        public void onConfigured(@NonNull CameraCaptureSession session) {
                            Log.i(TAG, "onConfigured: ");
                            if (mCameraDevice == null) {
                                return;
                            } else {
                                mCaptureSession = session;
                            }
 
                            mPreviewRequest = mPreviewRequstBuilder.build();
                            try {
                                mCaptureSession.setRepeatingRequest(mPreviewRequest, null, mBackgroundHander);
                            } catch (CameraAccessException e) {
                                e.printStackTrace();
                            }
                        }
 
                        @Override
                        public void onConfigureFailed(@NonNull CameraCaptureSession session) {
                            Log.i(TAG, "onConfigureFailed: ");
                        }
                    }, mBackgroundHander);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

Summary

Camera2 displays Preview video streams using the SurfaceTexture component;
Using CameraManager to open camera of CameraDevice, and inserting StateCallback of callback function CameraDevice to feedback camera status;
When the Camera condition is Opened, CaptureRequest. Builder is preview, and surface is associated with builder; After that, createCaptureSession of CameraDevice is created. When onConfigured information is received, sensor, lens, flash, etc. are configured, then build function of Builder is called to complete configuration of CaptureRequest, and finally session is set to repeat mode.


Related articles: