ImageAnalysis.Builder

public static final class ImageAnalysis.Builder
extends Object implements ExtendableBuilder<T extends UseCase>

java.lang.Object
   ↳ androidx.camera.core.ImageAnalysis.Builder


Builder for a ImageAnalysis.

Summary

Public constructors

ImageAnalysis.Builder()

Creates a new Builder object.

Public methods

ImageAnalysis build()

Builds an ImageAnalysis from the current state.

ImageAnalysis.Builder setBackgroundExecutor(Executor executor)

Sets the default executor that will be used for background tasks.

ImageAnalysis.Builder setBackpressureStrategy(int strategy)

Sets the backpressure strategy to apply to the image producer to deal with scenarios where images may be produced faster than they can be analyzed.

ImageAnalysis.Builder setImageQueueDepth(int depth)

Sets the number of images available to the camera pipeline for ImageAnalysis.STRATEGY_BLOCK_PRODUCER mode.

ImageAnalysis.Builder setTargetAspectRatio(int aspectRatio)

Sets the aspect ratio of the intended target for images from this configuration.

ImageAnalysis.Builder setTargetName(String targetName)

Sets the name of the target object being configured, used only for debug logging.

ImageAnalysis.Builder setTargetResolution(Size resolution)

Sets the resolution of the intended target from this configuration.

ImageAnalysis.Builder setTargetRotation(int rotation)

Sets the rotation of the intended target for images from this configuration.

Inherited methods

Public constructors

ImageAnalysis.Builder

public ImageAnalysis.Builder ()

Creates a new Builder object.

Public methods

build

public ImageAnalysis build ()

Builds an ImageAnalysis from the current state.

Returns
ImageAnalysis A ImageAnalysis populated with the current state.

Throws
IllegalArgumentException if attempting to set both target aspect ratio and target resolution.

setBackgroundExecutor

public ImageAnalysis.Builder setBackgroundExecutor (Executor executor)

Sets the default executor that will be used for background tasks.

If not set, the background executor will default to an automatically generated Executor.

Parameters
executor Executor: The executor which will be used for background tasks.

Returns
ImageAnalysis.Builder the current Builder.

setBackpressureStrategy

public ImageAnalysis.Builder setBackpressureStrategy (int strategy)

Sets the backpressure strategy to apply to the image producer to deal with scenarios where images may be produced faster than they can be analyzed.

The available values are ImageAnalysis.STRATEGY_BLOCK_PRODUCER and ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST.

If not set, the backpressure strategy will default to ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST.

Parameters
strategy int: The strategy to use.

Returns
ImageAnalysis.Builder The current Builder.

setImageQueueDepth

public ImageAnalysis.Builder setImageQueueDepth (int depth)

Sets the number of images available to the camera pipeline for ImageAnalysis.STRATEGY_BLOCK_PRODUCER mode.

The image queue depth is the number of images available to the camera to fill with data. This includes the image currently being analyzed by ImageAnalysis.Analyzer.analyze(ImageProxy). Increasing the image queue depth may make camera operation smoother, depending on the backpressure strategy, at the cost of increased memory usage.

When the backpressure strategy is set to ImageAnalysis.STRATEGY_BLOCK_PRODUCER, increasing the image queue depth may make the camera pipeline run smoother on systems under high load. However, the time spent analyzing an image should still be kept under a single frame period for the current frame rate, on average, to avoid stalling the camera pipeline.

The value only applies to ImageAnalysis.STRATEGY_BLOCK_PRODUCER mode. For ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST the value is ignored.

If not set, and this option is used by the selected backpressure strategy, the default will be a queue depth of 6 images.

Parameters
depth int: The total number of images available to the camera.

Returns
ImageAnalysis.Builder The current Builder.

setTargetAspectRatio

public ImageAnalysis.Builder setTargetAspectRatio (int aspectRatio)

Sets the aspect ratio of the intended target for images from this configuration.

The aspect ratio is the ratio of width to height in the sensor orientation.

It is not allowed to set both target aspect ratio and target resolution on the same use case. Attempting so will throw an IllegalArgumentException when building the Config.

The target aspect ratio is used as a hint when determining the resulting output aspect ratio which may differ from the request, possibly due to device constraints. Application code should check the resulting output's resolution.

If not set, resolutions with aspect ratio 4:3 will be considered in higher priority.

Parameters
aspectRatio int: The desired ImageAnalysis AspectRatio

Returns
ImageAnalysis.Builder The current Builder.

setTargetName

public ImageAnalysis.Builder setTargetName (String targetName)

Sets the name of the target object being configured, used only for debug logging.

The name should be a value that can uniquely identify an instance of the object being configured.

If not set, the target name will default to a unique name automatically generated with the class canonical name and random UUID.

Parameters
targetName String: A unique string identifier for the instance of the class being configured.

Returns
ImageAnalysis.Builder the current Builder.

setTargetResolution

public ImageAnalysis.Builder setTargetResolution (Size resolution)

Sets the resolution of the intended target from this configuration.

The target resolution attempts to establish a minimum bound for the image resolution. The actual image resolution will be the closest available resolution in size that is not smaller than the target resolution, as determined by the Camera implementation. However, if no resolution exists that is equal to or larger than the target resolution, the nearest available resolution smaller than the target resolution will be chosen. Resolutions with the same aspect ratio of the provided Size will be considered in higher priority before resolutions of different aspect ratios.

It is not allowed to set both target aspect ratio and target resolution on the same use case. Attempting so will throw an IllegalArgumentException when building the Config.

The resolution Size should be expressed at the use cases's target rotation. For example, a device with portrait natural orientation in natural target rotation requesting a portrait image may specify 480x640, and the same device, rotated 90 degrees and targeting landscape orientation may specify 640x480.

The maximum available resolution that could be selected for an ImageAnalysis is limited to be under 1080p. The limitation of 1080p for ImageAnalysis has considered both performance and quality factors so that users can obtain reasonable quality and smooth output stream under 1080p.

If not set, resolution of 640x480 will be selected to use in priority.

Parameters
resolution Size: The target resolution to choose from supported output sizes list.

Returns
ImageAnalysis.Builder The current Builder.

setTargetRotation

public ImageAnalysis.Builder setTargetRotation (int rotation)

Sets the rotation of the intended target for images from this configuration.

This adjust the ImageInfo.getRotationDegrees() of the ImageProxy passed to ImageAnalysis.Analyzer.analyze(ImageProxy). 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.

This is one of four valid values: Surface.ROTATION_0, Surface.ROTATION_90, Surface.ROTATION_180, Surface.ROTATION_270. Rotation values are relative to the "natural" rotation, Surface.ROTATION_0.

In general, it is best to additionally set the target rotation dynamically on the use case. See ImageAnalysis.setTargetRotation(int) for additional documentation.

If not set, the target rotation will default to the value of Display.getRotation() of the default display at the time the use case is created.

Parameters
rotation int: The rotation of the intended target.

Returns
ImageAnalysis.Builder The current Builder.