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 intgetTargetCoordinateSystem()

Implement this method to return the target coordinate system.

public SizegetTargetResolutionOverride()

Implement this method to override the target resolution of ImageAnalysis.

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 getTargetResolutionOverride()

Implement this method to override the target resolution of ImageAnalysis.

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

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 resolution to override the target resolution of ImageAnalysis, or null if no overriding 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 could either mean value of the target coordinate system is ImageAnalysis.COORDINATE_SYSTEM_ORIGINAL, or currently there is no valid transformation for the target coordinate system, for example, if currently 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()