public final class ImageAnalysis extends UseCase

A use case providing CPU accessible images for an app to perform image analysis on.

ImageAnalysis acquires images from the camera via an ImageReader. Each image is provided to an ImageAnalysis.Analyzer function which can be implemented by application code, where it can access image data for application analysis via an ImageProxy.

The application is responsible for calling close to close the image. Failing to close the image will cause future images to be stalled or dropped depending on the backpressure strategy.

Summary

Constants

COORDINATE_SYSTEM_ORIGINAL

public static final int COORDINATE_SYSTEM_ORIGINAL = 0

ImageAnalysis.Analyzer option for returning the original coordinates.

Use this option if no additional transformation is needed by the Analyzer implementation. The coordinates returned by the Analyzer should be within (0, 0) - (width, height) where width and height are the dimensions of the ImageProxy.

By using this option, CameraX will pass null to updateTransform.

COORDINATE_SYSTEM_SENSOR

public static final int COORDINATE_SYSTEM_SENSOR = 2

ImageAnalysis.Analyzer option for returning the sensor coordinates.

Use this option if the app wishes to get the detected objects in camera sensor coordinates. The coordinates returned by the Analyzer should be within (left, right) - (width, height), where the left, right, width and height are bounds of the camera sensor's active array.

By using this option, CameraX will pass getSensorToBufferTransformMatrix's inverse to updateTransform.

COORDINATE_SYSTEM_VIEW_REFERENCED

public static final int COORDINATE_SYSTEM_VIEW_REFERENCED = 1

ImageAnalysis.Analyzer option for returning UI coordinates.

When the ImageAnalysis.Analyzer is configured with this option, it will receive a Matrix that will receive a value that represents the transformation from camera sensor to the View, which can be used for highlighting detected result in UI. For example, laying over a bounding box on top of the detected face.

Note this option will only work with an artifact that displays the camera feed in UI. Generally, this is used by higher-level libraries such as the CameraController API that incorporates a viewfinder UI. It will not be effective when used with camera-core directly.

OUTPUT_IMAGE_FORMAT_NV21

public static final int OUTPUT_IMAGE_FORMAT_NV21 = 3

Images sent to the analyzer will be formatted in NV21.

All ImageProxy sent to analyze will be in YUV_420_888 format with their image data formatted in NV21.

The output ImageProxy has three planes with the order of Y, U, V. The pixel stride of U or V planes are 2. The byte buffer pointer position of V plane will be ahead of the position of the U plane. Applications can directly read the plane[2] to get all the VU interleaved data.

Due to limitations on some Android devices in producing images in NV21 format, the android.media.Image object obtained from getImage will be the original image produced by the camera capture pipeline. This may result in discrepancies between the android.media.Image and the ImageProxy, such as:

  • Plane data may differ.
  • Width and height may differ.
  • Other properties may also differ.

Developers should be aware of these potential differences and use the properties from the ImageProxy when necessary.

STRATEGY_BLOCK_PRODUCER

public static final int STRATEGY_BLOCK_PRODUCER = 1

Block the producer from generating new images.

Once the producer has produced the number of images equal to the image queue depth, and none have been closed, the producer will stop producing images. Note that images may be queued internally and not be delivered to the analyzer until the last delivered image has been closed with close. These internally queued images will count towards the total number of images that the producer can provide at any one time.

When the producer stops producing images, it may also stop producing images for other use cases, such as Preview, so it is important for the analyzer to keep up with frame rate, on average. Failure to keep up with frame rate may lead to jank in the frame stream and a diminished user experience. If more time is needed for analysis on some frames, consider increasing the image queue depth with setImageQueueDepth.

STRATEGY_KEEP_ONLY_LATEST

public static final int STRATEGY_KEEP_ONLY_LATEST = 0

Only deliver the latest image to the analyzer, dropping images as they arrive.

This strategy ignores the value set by setImageQueueDepth. Only one image will be delivered for analysis at a time. If more images are produced while that image is being analyzed, they will be dropped and not queued for delivery. Once the image being analyzed is closed by calling close, the next latest image will be delivered.

Internally this strategy may make use of an internal Executor to receive and drop images from the producer. A performance-tuned executor will be created internally unless one is explicitly provided by setBackgroundExecutor. In order to ensure smooth operation of this backpressure strategy, any user supplied Executor must be able to quickly respond to tasks posted to it, so setting the executor manually should only be considered in advanced use cases.

Public methods

clearAnalyzer

public void clearAnalyzer()

Removes a previously set analyzer.

This will stop data from streaming to the ImageAnalysis.

getImageQueueDepth

public int getImageQueueDepth()

Returns the number of images available to the camera pipeline, including the image being analyzed, for the STRATEGY_BLOCK_PRODUCER backpressure mode.

The image queue depth is set when constructing an ImageAnalysis instance using setImageQueueDepth. If not set, and this option is used by the backpressure strategy, the default will be a queue depth of 6 images.

getTargetRotation

public int getTargetRotation()

Returns the rotation of the intended target for images.

The rotation can be set when constructing an ImageAnalysis instance using setTargetRotation, or dynamically by calling setTargetRotation. If not set, the target rotation defaults to the value of getRotation of the default display at the time the use case is created. The use case is fully created once it has been attached to a camera.

Returns
int

The rotation of the intended target for images.

isOutputImageRotationEnabled

public boolean isOutputImageRotationEnabled()

Checks if output image rotation is enabled. It returns false by default.

Returns
boolean

true if enabled, false otherwise.

setAnalyzer

public void setAnalyzer(
    @NonNull Executor executor,
    @NonNull ImageAnalysis.Analyzer analyzer
)

Sets an analyzer to receive and analyze images.

Setting an analyzer will signal to the camera that it should begin sending data. The stream of data can be stopped by calling clearAnalyzer.

Applications can process or copy the image by implementing the Analyzer. If frames should be skipped (no analysis), the analyzer function should return, instead of disconnecting the analyzer function completely.

Setting an analyzer function replaces any previous analyzer. Only one analyzer can be set at any time.

setTargetRotation

public void setTargetRotation(int rotation)

Sets the target rotation.

This adjust the getRotationDegrees of the ImageProxy passed to analyze. The rotation value of ImageInfo will be the rotation, which if applied to the output image, will make the image match target rotation specified here.

While rotation can also be set via setTargetRotation, using setTargetRotation allows the target rotation to be set dynamically.

In general, it is best to use an android.view.OrientationEventListener to set the target rotation. This way, the rotation output to the Analyzer will indicate which way is down for a given image. This is important since display orientation may be locked by device default, user setting, or app configuration, and some devices may not transition to a reverse-portrait display orientation. In these cases, set target rotation dynamically according to the android.view.OrientationEventListener, without re-creating the use case. snapToSurfaceRotation is a helper function to convert the orientation of the android.view.OrientationEventListener to a rotation value. See snapToSurfaceRotation for more information and sample code.

When this function is called, value set by setTargetResolution will be updated automatically to make sure the suitable resolution can be selected when the use case is bound.

If not set here or by configuration, the target rotation will default to the value of getRotation of the default display at the time the use case is bound. To return to the default value, set the value to

context.getSystemService(WindowManager.class).getDefaultDisplay().getRotation();

Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.

Last updated 2026-01-30 UTC.