Jetpack support library CameraX first experience

1. Overview of CameraX

CameraX overview

Getting started with CameraX

CameraX is a Jetpack support library designed to help you simplify the development of camera applications. It provides a consistent and easy-to-use API interface, suitable for most Android devices, and is backward compatible to Android 5.0 (API level 21).

Although it takes advantage of camera2's capabilities, it uses a simpler and use-case-based approach that has lifecycle awareness. It also solves device compatibility issues, so you do n’t need to include device-specific code in the code base. These functions reduce the amount of code that needs to be written when adding camera functions to the application.

Finally, with CameraX, developers only need two lines of code to take advantage of the same camera experience and features as the pre-installed camera application. CameraX Extensions is an optional plug-in, through which you can add portrait, HDR, night mode and beauty effects to your application on supported devices.

2. Use of CameraX

2.1 CameraX has the following minimum version requirements:

  • Android API level 21
  • Android Architecture Components 1.1.1

2.2, use CameraX

Developers use CameraX to interact with the device ’s camera using an abstract concept called “use case”. The current use cases are as follows:

  • Preview: Prepare a preview SurfaceTexture
  • Image analysis: Provide buffers accessible to the CPU for analysis (for example, machine learning)
  • Picture capture: Take and save a photo

Note: You must first apply for and open Camera permissions:

<uses-permission android:name="android.permission.CAMERA" />

2.2.1. Realize preview:

The preview use case generates a SurfaceTexture to stream camera input. It also provides other information needed to crop, scale, or rotate the View to ensure it displays properly.

When the camera is active, the picture preview will be streamed to this SurfaceTexture. You can connect SurfaceTexture to TextureView or GLSurfaceView.

PreviewConfig --> Preview

    PreviewConfig config = new PreviewConfig.Builder().build();
    Preview preview = new Preview(config);

    preview.setOnPreviewOutputUpdateListener(
        new Preview.OnPreviewOutputUpdateListener() {
            @Override
            public void onUpdated(Preview.PreviewOutput previewOutput) {
                // Your code here. For example, use previewOutput.getSurfaceTexture()
                // and post to a GL renderer.
        });
    });

    CameraX.bindToLifecycle((LifecycleOwner) this, preview);

2.2.2. Analyze the pictures:

The image analysis use case provides your application with images that the CPU can access to perform image processing, computer vision, or machine learning inference. The application implements the Analyzer method that runs on each frame.

ImageAnalysisConfig --> ImageAnalysis

    ImageAnalysisConfig config =
        new ImageAnalysisConfig.Builder()
            .setTargetResolution(new Size(1280, 720))
            .build();

    ImageAnalysis imageAnalysis = new ImageAnalysis(config);

    imageAnalysis.setAnalyzer(
        new ImageAnalysis.Analyzer() {
            @Override
            public void analyze(ImageProxy image, int rotationDegrees) {
                // insert your code here.
            }
    });
    CameraX.bindToLifecycle((LifecycleOwner) this, imageAnalysis, preview);
   

2.2.3. Picture shooting:

The photo shooting use case aims to take high-resolution, high-quality photos. It not only provides simple camera manual control functions, but also provides automatic white balance, automatic exposure, and automatic focus (3A) functions. The calling program is responsible for deciding how to use the photos taken, including the following options:

  • takePicture (OnImageCapturedListener): This method is used to provide a memory buffer for the captured pictures.
  • takePicture (File, OnImageSavedListener): This method saves the captured picture to the file location provided.
  • takePicture (File, OnImageSavedListener, Metadata): You can use this method to specify the metadata to be embedded in the Exif of the saved file.

ImageCaptureConfig --> ImageCapture

    ImageCaptureConfig config =
        new ImageCaptureConfig.Builder()
            .setTargetRotation(getWindowManager().getDefaultDisplay().getRotation())
            .build();

    ImagesCapture imageCapture = new ImageCapture(config);
   

2.2.4. Bind the use case to the life cycle

 //将用例绑定到生命周期
 //如果Android Studio抱怨“this”不是一个生命周期所有者
 //尝试重新构建项目或更新appcompat依赖项
 //版本1.1.0或更高。
 CameraX.bindToLifecycle((LifecycleOwner) this, imageCapture, imageAnalysis, preview);

3. Code example

CameraXApp example

Official CameraX example

Published 45 original articles · Like 24 · Visits 50,000+

Guess you like

Origin blog.csdn.net/zhijiandedaima/article/details/93202045