Android CameraX

Tram Ho

1. Overview

CameraX is a Jetpack support library, if you are not familiar with Jetpack then you can imagine this is a collection of libraries, tools and guidelines to help developers develop applications easily with limited. Boring code, simple complex tasks. You can refer to Jetpack here

CameraX will help you build camera applications easily by providing APIs suitable for Android 5.0 devices (API 21). Compared to Camera2, using CameraX helps you not to worry about compatibility issues with devices.

Advantages when using CameraX:

  1. Easy to use

CameraX introduces a number of use cases that allow you to focus on the work to be done:

  • Preview : display an image preview
  • Image analysis : allows CPU buffer access for analysis to use machine learning algorithms like MLKit
  • Image capture : capture and save images
  1. Consistency between devices

Camera management in the application is not easy such as: aspect ratio, orientation, rotation, preview size, high-resolution image size. But with CameraX, it will be much simpler,

  1. New experience

CameraX has an add-on called Extensions that allows you to access the features provided for the camera. Some of the features that this extension offers are: Portrait, HDR, Night, Beauty.

2. Camerax Architecture

Let’s explore the CameraX example:

CameraX is linked to the application life cycle using the bindToLifecycle () method. CameraX listens to the life cycle to decide when to turn on the camera, when to take a picture, when to turn off the camera. And use CameraX.unbindAll () when you want to unlink the life cycle.

  • Config

CameraX automatically provides the specified feature of the device on which the application will be run, i.e. CameraX will automatically determine the best resolution to use if you do not provide the specified resolution or if the resolution Your installation is not supported on the device. The purpose of CameraX is to successfully initiate a session with the camera, which means CameraX will compromise the resolution and aspect ratio based on the device’s permission.

3. Preview

To add a preview to the application, using the PreviewView , this view can crop, scale, rotate

  • Config CameraXConfig.Provider

Implement CameraXConfig.Provider in the Application class

  • Add PreviewView to the layout

  • Request CameraProvider

  • Check CameraProvider

  • Select the camera and link to the lifecycle

Once CameraProvider has been successfully created and checked, perform the following steps:

  • Create Preview and connect with PreviewView
  • Specify the LensFacing camera option
  • Associated with lifecycle

4. Analyze Image

Image analysis provides CPU buffers to perform image analysis tasks using computer vision, machine learning.

Image analysis can work in two modes: blocking and non-blocking.

The blocking mode is enabled using setBackpressureStrategy () with the value STRATEGY_BLOCK_PRODUCER. In this mode, the runtime receives frames from the camera in sequential order.

Non-blocking mode uses setBackpressureStrategy () with STRATEGY_KEEP_ONLY_LATEST value, this mode will only receive the last frame from the camera

After configuring the modes, use the setAnalyzer () method to use image analysis methods and don’t forget to bind to the Lifecycle.

5. Image Capture

After configuring the camera in the above steps, you can now capture the image and save it

The article here is also long enough, please make an appointment to read the next article I will write more about the awesome and extremely magical extensions of this CameraX such as HDR, Bokeh, …

Refer

https://developer.android.com/training/camerax

https://codelabs.developers.google.com/codelabs/camerax-getting-started/#0

Share the news now

Source : Viblo