public interface

ImageAnalysis.Analyzer

 androidx.camera.core.ImageAnalysis.Analyzer

Subclasses:

MlKitAnalyzer

Overview

Interface for analyzing images.

Implement Analyzer and pass it to ImageAnalysis.setAnalyzer(Executor, ImageAnalysis.Analyzer) to receive images and perform custom processing by implementing the ImageAnalysis.Analyzer.analyze(ImageProxy) function.

Summary

Methods
public voidanalyze(ImageProxy image)

Analyzes an image to produce a result.

public SizegetDefaultTargetResolution()

Implement this method to set a default target resolution for the ImageAnalysis.

public intgetTargetCoordinateSystem()

Implement this method to return the target coordinate system.

public voidupdateTransform(Matrix matrix)

Implement this method to receive the for coordinate transformation.

Methods

public void analyze(ImageProxy image)

Analyzes an image to produce a result.

This method is called once for each image from the camera, and called at the frame rate of the camera. Each analyze call is executed sequentially.

It is the responsibility of the application to close the image once done with it. If the images are not closed then it may block further images from being produced (causing the preview to stall) or drop images as determined by the configured backpressure strategy. The exact behavior is configurable via ImageAnalysis.Builder.setBackpressureStrategy(int).

Images produced here will no longer be valid after the ImageAnalysis instance that produced it has been unbound from the camera.

The image provided has format .

The provided image is typically in the orientation of the sensor, meaning CameraX does not perform an internal rotation of the data. The rotationDegrees parameter allows the analysis to understand the image orientation when processing or to apply a rotation. For example, if the target rotation) is natural orientation, rotationDegrees would be the rotation which would align the buffer data ordering to natural orientation.

Timestamps are in nanoseconds and monotonic and can be compared to timestamps from images produced from UseCases bound to the same camera instance. More detail is available depending on the implementation. For example with CameraX using a androidx.camera.camera2 implementation additional detail can be found in android.hardware.camera2.CameraDevice documentation.

Parameters:

image: The image to analyze

See also:

public Size getDefaultTargetResolution()

Implement this method to set a default target resolution for the ImageAnalysis.

Implement this method if the ImageAnalysis.Analyzer requires a specific resolution to work. The return value will be used as the default target resolution for the ImageAnalysis. Return null if no falling back is needed. By default, this method returns null.

If the app does not set a target resolution for ImageAnalysis, then this value will be used as the target resolution. If the ImageAnalysis has set a target resolution, e.g. if ImageAnalysis.Builder.setTargetResolution(Size) is called, then the ImageAnalysis will use the app value over this value.

Note that this method is invoked by CameraX at the time of binding to lifecycle. In order for this value to be effective, the ImageAnalysis.Analyzer has to be set before ImageAnalysis is bound to a lifecycle. Otherwise, the value will be ignored.

Returns:

the default resolution of ImageAnalysis, or null if no specific resolution is needed.

public int getTargetCoordinateSystem()

Implement this method to return the target coordinate system.

The coordinates detected by analyzing camera frame usually needs to be transformed. For example, in order to highlight a detected face, the app needs to transform the bounding box from the ImageAnalysis's coordinate system to the View's coordinate system. This method allows the implementer to set a target coordinate system.

The value will be used by CameraX to calculate the transformation and forward it to the ImageAnalysis.Analyzer via ImageAnalysis.Analyzer.updateTransform(Matrix). By default, this method returns ImageAnalysis.COORDINATE_SYSTEM_ORIGINAL.

For now, camera-core only supports ImageAnalysis.COORDINATE_SYSTEM_ORIGINAL, please see libraries derived from camera-core, for example, camera-view.

See also: ImageAnalysis.Analyzer.updateTransform(Matrix)

public void updateTransform(Matrix matrix)

Implement this method to receive the for coordinate transformation.

The value represents the transformation from the camera sensor to the target coordinate system defined in ImageAnalysis.Analyzer.getTargetCoordinateSystem(). It should be used by the implementation to transform the coordinates detected in the camera frame. For example, the coordinates of the detected face.

If the value is null, it means that no valid transformation is available. It could have different causes depending on the value of ImageAnalysis.Analyzer.getTargetCoordinateSystem():

  • If the target coordinate system is ImageAnalysis.COORDINATE_SYSTEM_ORIGINAL, it is always invalid because in that case, the coordinate system depends on how the analysis algorithm processes the ImageProxy.
  • It is also invalid if the target coordinate system is not available, for example if the analyzer targets the viewfinder and the view finder is not visible in UI.

This method is invoked whenever a new transformation is ready. For example, when the view finder is first a launched as well as when it's resized.

See also: ImageAnalysis.Analyzer.getTargetCoordinateSystem()