# ImageAnalysis.Analyzer

Artifact: [androidx.camera:camera-core](https://developer.android.com/jetpack/androidx/releases/camera) [View Source](https://cs.android.com/search?q=file:androidx/camera/core/ImageAnalysis.java+class:androidx.camera.core.ImageAnalysis.Analyzer) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/camera#1.0.0)

*** ** * ** ***


```
public interface ImageAnalysis.Analyzer
```

<br />

Known direct subclasses [MlKitAnalyzer](https://developer.android.com/reference/androidx/camera/mlkit/vision/MlKitAnalyzer)

|---|---|
| `https://developer.android.com/reference/androidx/camera/mlkit/vision/MlKitAnalyzer` | An implementation of `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis.Analyzer` with ML Kit libraries. |

*** ** * ** ***

Interface for analyzing images.

Implement Analyzer and pass it to `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis#setAnalyzer(java.util.concurrent.Executor,androidx.camera.core.ImageAnalysis.Analyzer)` to receive images and perform custom processing by implementing the `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis.Analyzer#analyze(androidx.camera.core.ImageProxy)` function.

## Summary

| ### Public methods |
|---|---|
| `abstract void` | `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis.Analyzer#analyze(androidx.camera.core.ImageProxy)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/camera/core/ImageProxy image)` Analyzes an image to produce a result. |
| `default @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/util/Size.html` | `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis.Analyzer#getDefaultTargetResolution()()` Implement this method to set a default target resolution for the `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis`. |
| `default int` | `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis.Analyzer#getTargetCoordinateSystem()()` Implement this method to return the target coordinate system. |
| `default void` | `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis.Analyzer#updateTransform(android.graphics.Matrix)(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/graphics/Matrix.html matrix)` Implement this method to receive the `https://developer.android.com/reference/android/graphics/Matrix.html` for coordinate transformation. |

## Public methods

### analyze

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/camera#1.0.0)

```
abstract void analyze(@NonNull 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 `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis.Builder#setBackpressureStrategy(int)`.

Images produced here will no longer be valid after the `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis` instance that produced it has been unbound from the camera.

The image provided has format `https://developer.android.com/reference/android/graphics/ImageFormat.html#YUV_420_888` by default. It can be configured to other formats such as `https://developer.android.com/reference/android/graphics/PixelFormat.html#RGBA_8888` or `https://developer.android.com/reference/android/graphics/ImageFormat.html#PRIVATE` via `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis.Builder#setOutputImageFormat(int)`.

When the output image format is set to `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis#OUTPUT_IMAGE_FORMAT_PRIVATE()`, the returned `https://developer.android.com/reference/androidx/camera/core/ImageProxy` is not CPU accessible. Calling `https://developer.android.com/reference/androidx/camera/core/ImageProxy#toBitmap()` will throw an `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` and `https://developer.android.com/reference/androidx/camera/core/ImageProxy#getPlanes()` will return an empty array.

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 `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis#setTargetRotation(int)`) 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 `https://developer.android.com/reference/androidx/camera/camera2/package-summary` implementation additional detail can be found in `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.html` documentation.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/camera/core/ImageProxy image` | The image to analyze |

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/Image.html#getTimestamp()` |   |
| `https://developer.android.com/reference/android/hardware/camera2/CaptureResult.html#SENSOR_TIMESTAMP` |   |

### getDefaultTargetResolution

Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/camera#1.2.0)

```
default @Nullable Size getDefaultTargetResolution()
```

Implement this method to set a default target resolution for the `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis`.

Implement this method if the `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis.Analyzer` requires a specific resolution to work. The return value will be used as the default target resolution for the `https://developer.android.com/reference/androidx/camera/core/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 `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis`, then this value will be used as the target resolution. If the `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis` has set a target resolution, e.g. if `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis.Builder#setTargetResolution(android.util.Size)` is called, then the `https://developer.android.com/reference/androidx/camera/core/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 `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis.Analyzer` has to be set before `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis` is bound to a lifecycle. Otherwise, the value will be ignored.

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/util/Size.html` | the default resolution of `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis`, or `null` if no specific resolution is needed. |

### getTargetCoordinateSystem

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/camera#1.1.0)

```
default 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 `https://developer.android.com/reference/androidx/camera/core/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 `https://developer.android.com/reference/android/graphics/Matrix.html` and forward it to the `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis.Analyzer` via `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis.Analyzer#updateTransform(android.graphics.Matrix)`. By default, this method returns `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis#COORDINATE_SYSTEM_ORIGINAL()`.

For now, camera-core only supports `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis#COORDINATE_SYSTEM_ORIGINAL()`, please see libraries derived from camera-core, for example, camera-view.

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis.Analyzer#updateTransform(android.graphics.Matrix)` |   |

### updateTransform

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/camera#1.1.0)

```
default void updateTransform(@Nullable Matrix matrix)
```

Implement this method to receive the `https://developer.android.com/reference/android/graphics/Matrix.html` for coordinate transformation.

The value represents the transformation from the camera sensor to the target coordinate system defined in `https://developer.android.com/reference/androidx/camera/core/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 `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis.Analyzer#getTargetCoordinateSystem()`:

- If the target coordinate system is `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis#COORDINATE_SYSTEM_ORIGINAL()`, it is always invalid because in that case, the coordinate system depends on how the analysis algorithm processes the `https://developer.android.com/reference/androidx/camera/core/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 |
|---|---|
| `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis.Analyzer#getTargetCoordinateSystem()` |   |