Android Camera2- preview, photo, video process

1. Camera2 realizes the process frame diagram of the three basic functions of preview, photo taking and video recording

Camera2 key classes:

CameraManager

Manage all camera devices on your phone. Manage all camera devices on the mobile phone, its main function is to obtain the camera list and open (openCamera) the specified camera.

It is actually a system service, obtained through getSystemService(Context.CAMERA_SERVICE), its function is mainly to obtain the camera list and open the specified camera.

Some basic information of the camera can be obtained through the CameraManager object, and this information is stored in the CameraCharacteristic object.

In addition to obtaining the property information of the Camera, the most important function of the CameraManager object is to open the camera (openCamera). Only through the CameraManager can the CameraDevice object be obtained to operate the camera.

CameraDevice

The specific camera device has a series of parameters (preview size, photo size, etc.), which can be obtained through the getCameraCharacteristics() method of CameraManager. Its role is mainly to create CameraCaptureSession and CaptureRequest.

CameraCaptureSession

The camera capture session represents a session between the upper layer and the lower layer. Through this session, instructions can be sent to the camera to let the camera perform operations such as previewing, taking pictures, and recording videos. It is obtained in the callback of mCameraDevice.createCaptureSession. The main function is to handle the work of taking pictures and previewing (very important).

If you want to get an image from a camera device, you must first create a CameraCaptureSession to transfer the carrier of the received data to the camera device. Currently, the carriers that can receive camera data are Surface and SurfaceTexture.

By case, the camera's preview data can be received using SurfaceView and TextureView. Use ImageReader to take pictures, and MediaCodec or MediaRecorder to record videos.

Most camera operations are implemented by submitting a Capture request to CameraCaptureSession, such as taking pictures, taking continuous shots, setting flash mode, touching to focus, displaying preview images, and so on.

CaptureRequest

CaptureRequest represents a camera capture request. After the CaptureCaptureSession is created, you can use this session to issue instructions, indicating whether you need to preview, take pictures, or record videos. The CaptureRequest object can carry the set parameters, such as whether to auto-focus, auto-exposure, auto-white balance, etc. The creation of the CaptureRequest object uses the builder pattern, which needs to be created through CaptureRequest.Builder, by calling the build method.

CaptureRequest is the information carrier when submitting the Capture request to CameraCaptureSession, which includes the parameter configuration of this Capture and the Surface that receives the image data. CaptureRequest can configure a lot of information, including image format, image resolution, sensor control, flash control, 3A control, etc. It can be said that most of the camera parameters are configured through CaptureRequest. It is worth noting that each CaptureRequest represents the operation of a frame, which means that you can precisely control the Capture operation of each frame.

CaptureRequest defines the output buffer and display interface (TextureView or SurfaceView), etc.

CaptureResut

CaptureResut represents some result information returned by the capture request, from which some Metadata data information can be obtained.

CaptureResult is the result of each Capture operation, which includes a lot of status information, including flash status, focus status, timestamp, and so on. For example, you can use CaptureResult to obtain the focus status and time stamp of this photo when the photo is taken. It should be noted that CaptureResult does not contain any image data. As we said earlier when we introduced Surface, the image data is all obtained from Surface.

Surface
Surface is a memory space used to fill image data. For example, you can use SurfaceView's Surface to receive each frame of preview data for displaying the preview screen, or use ImageReader's Surface to receive JPEG or YUV data. Each Surface can have its own size and data format, and you can get a list of sizes supported by a certain data format from CameraCharacteristics.

2. Concept

Capture

All operations and parameter configurations of the camera are ultimately for image capture. For example, focusing is to make the image of a certain area clearer, and adjusting exposure compensation is to adjust the brightness of the image. Therefore, all camera operations and parameter configurations in Camera2 are abstracted into Capture (capture), so don't simply understand Capture directly as taking pictures, because the Capture operation may only focus on making the preview image clearer. If you are familiar with Camera1, then you may ask where is setFlashMode()? Where is setFocusMode()? Where is takePicture()? Let me tell you, they are all realized through Capture.

Capture is subdivided into three types in terms of execution methods: [Single Mode], [Multiple Mode] and [Repeat Mode]. Let’s explain them one by one:

  • One-shot mode (One-shot): Refers to the Capture operation that is performed only once, such as setting the flash mode, focusing mode, and taking a photo. Multiple captures in one-time mode will enter the queue and be executed sequentially.

  • Multi-time mode (Burst): refers to the continuous multiple execution of the specified Capture operation. The biggest difference between this mode and multiple execution of single mode is that no other Capture operations are allowed to be inserted during multiple consecutive captures, such as continuous shooting of 100 photos. During the shooting of these 100 photos, any new Capture request will be queued until 100 photos are taken. Multiple sets of captures in multiple modes will enter the queue and be executed sequentially.

  • Repeating mode (Repeating): Refers to the repeated execution of the specified capture operation. When other modes of capture are submitted, the mode will be suspended and other modes of capture will be executed. The capture of this pattern is globally unique, that is, the newly submitted capture of the repeating pattern will overwrite the old capturing of the repeating pattern.

 Some advanced features only supported by Camera2

If I were to give a strong reason why you should use Camera2, it would be the best reason to build higher quality camera applications through the advanced features provided by Camera2.

1) Check the camera information before turning on the camera
For some reasons, you may need to check the camera information before deciding whether to turn on the camera, such as checking whether the flash is available. On Caemra1, you cannot check detailed camera information before starting the camera, because the information is provided through an already started camera instance. On Camera2, we have a CameraCharacteristics instance that is completely separated from the camera instance to provide camera information, so we can check almost all camera information without opening the camera.

2) Taking pictures without opening the preview
On Camera1, opening the preview is a very important link, because you can only take pictures after opening the preview, so you have to open the preview even if the preview screen is contrary to the actual business needs. But Camera2 does not force you to open the preview before taking pictures.

3) Take multiple pictures of different formats and sizes at a time
On Camera1, only one picture can be taken at a time, not to mention multiple pictures of different formats and sizes. And Camera2 supports taking multiple pictures at a time, even multiple pictures with different formats and sizes. For example, you can shoot a 1440x1080 JPEG image and a full-size RAW image at the same time.

4) Controlling the exposure time
When taking pictures in a dark environment, if the exposure time can be extended appropriately, the brightness of the image can be improved. On Camera2, you can configure the exposure time of the photo within the specified exposure time range, so as to achieve long-exposure pictures, and you can even extend the exposure time of each frame of the preview screen so that the entire preview screen can also ensure a certain brightness in a dark environment. But you can only YY on Camera1.

5) Continuous shooting
Before the emergence of Camera2, the function of continuous shooting of 30 pictures may only be possible with the system camera (except for the method of capturing the preview screen through OpenGL). It may also be for this reason that all third-party cameras on the market do not support continuous shooting. With Camera2, you can make your camera application support continuous shooting, even take 30 pictures with different exposure times in a row.

6), flexible 3A control
3A (AF, AE, AWB) control has been maximized on Camera2, the application layer can flexibly configure the 3A process according to business needs and obtain 3A status in real time, while Camera1 provides much less interfaces in 3A control and monitoring. For example, you can perform AE operation before taking a photo, and monitor whether the flash is turned on for this photo.

 Some suggestions for migrating from Camera1 to Camera2

If you are familiar with Camera1 and plan to migrate from Camera1 to Camera2, I hope the following suggestions can help you:
1) Camera1 strictly distinguishes two processes of preview and photo taking, while Camera2 abstracts these two processes into Capture behavior, except that one is a repeated Capture and the other is a one-time Capture.
2) Some API calls of Camera2 will also be time-consuming, so it is recommended that you use an independent thread to perform all camera operations, try to avoid calling Camera2 API directly in the main thread, HandlerThread is a good choice.
3) All camera operations of Camera2 can register related callback interfaces, and then write business logic in different callback methods, which may make your code complicated because it is not linear enough. It is recommended that you try to use the sub-thread blocking method to ensure the linear execution of the code as much as possible. For example, the child thread is blocked waiting for CaptureResult, and then continues to perform subsequent operations, instead of splitting the code into the CaptureCallback.onCaptureCompleted() method.

Supongo que te gusta

Origin blog.csdn.net/xiaowang_lj/article/details/131828899
Recomendado
Clasificación