ImageAnalysis

class ImageAnalysis : UseCase
kotlin.Any
   ↳ androidx.camera.core.UseCase
   ↳ androidx.camera.core.ImageAnalysis

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.

After the analyzer function returns, the ImageProxy will be closed and the corresponding android.media.Image is released back to the ImageReader.

Summary

Nested classes

abstract

Interface for analyzing images.

The different ways that the image sent to the analyzer is acquired from the underlying .

Public constructors

Creates a new image analysis use case from the given configuration.

Public methods

ImageAnalysis.Analyzer?

Retrieves a previously set analyzer.

Unit

Removes a previously set analyzer.

Unit
setAnalyzer(@Nullable analyzer: ImageAnalysis.Analyzer?)

Sets an analyzer to receive and analyze images.

Unit

Sets the target rotation.

String

Inherited functions

Public constructors

<init>

ImageAnalysis(config: ImageAnalysisConfig!)

Creates a new image analysis use case from the given configuration.

Parameters
config ImageAnalysisConfig!: for this use case instance

Public methods

getAnalyzer

@UiThread @Nullable fun getAnalyzer(): ImageAnalysis.Analyzer?

Retrieves a previously set analyzer.

Return
ImageAnalysis.Analyzer?: The last set analyzer or null if no analyzer is set.
Exceptions
IllegalStateException If not called on main thread.

removeAnalyzer

@UiThread fun removeAnalyzer(): Unit

Removes a previously set analyzer.

This is equivalent to calling setAnalyzer(null). This will stop data from streaming to the ImageAnalysis.

Exceptions
IllegalStateException If not called on main thread.

setAnalyzer

@UiThread fun setAnalyzer(@Nullable analyzer: ImageAnalysis.Analyzer?): Unit

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 setting the analyzer to null or by calling .

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.

Parameters
analyzer ImageAnalysis.Analyzer?: of the images or null to stop data streaming to ImageAnalysis.
Exceptions
IllegalStateException If not called on main thread.

setTargetRotation

fun setTargetRotation(rotation: Int): Unit

Sets the target rotation.

This informs the use case so it can adjust the rotation value sent to Analyzer#analyze(ImageProxy, int).

In most cases this should be set to the current rotation returned by . In that case, the rotation parameter sent to the analyzer will be the rotation, which if applied to the output image, will make the image match the display orientation.

While rotation can also be set via ImageAnalysisConfig.Builder#setTargetRotation(int), using ImageAnalysis#setTargetRotation(int) allows the target rotation to be set dynamically. This can be useful if an app locks itself to portrait, and uses the orientation sensor to set rotation, to process landscape images when the device is rotated by examining the rotation received by the Analyzer function.

If no target rotation is set by the application, it is set to the value of Display#getRotation() of the default display at the time the use case is created.

Parameters
rotation Int: Target rotation of the output image, expressed as one of Surface#ROTATION_0, Surface#ROTATION_90, Surface#ROTATION_180, or Surface#ROTATION_270.

toString

fun toString(): String