ImageAnalysis.Builder


public final class ImageAnalysis.Builder implements ExtendableBuilder


Builder for a ImageAnalysis.

Summary

Public constructors

Creates a new Builder object.

Public methods

@NonNull ImageAnalysis

Builds an ImageAnalysis from the current state.

@NonNull ImageAnalysis.Builder

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

@NonNull ImageAnalysis.Builder

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.

@NonNull ImageAnalysis.Builder
setImageQueueDepth(int depth)

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

@NonNull ImageAnalysis.Builder
setOutputImageFormat(int outputImageFormat)

Sets output image format.

@NonNull ImageAnalysis.Builder
@RequiresApi(value = 23)
setOutputImageRotationEnabled(boolean outputImageRotationEnabled)

Enable or disable output image rotation.

@NonNull ImageAnalysis.Builder

Sets the resolution selector to select the preferred supported resolution.

@NonNull ImageAnalysis.Builder
setTargetAspectRatio(int aspectRatio)

This method is deprecated.

use ResolutionSelector with AspectRatioStrategy to specify the preferred aspect ratio settings instead.

@NonNull ImageAnalysis.Builder

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

@NonNull ImageAnalysis.Builder

This method is deprecated.

use ResolutionSelector with ResolutionStrategy to specify the preferred resolution settings instead.

@NonNull ImageAnalysis.Builder
setTargetRotation(int rotation)

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

Public constructors

Builder

Added in 1.0.0
public Builder()

Creates a new Builder object.

Public methods

build

Added in 1.5.0-alpha02
public @NonNull ImageAnalysis build()

Builds an ImageAnalysis from the current state.

Returns
@NonNull ImageAnalysis

A ImageAnalysis populated with the current state.

Throws
java.lang.IllegalArgumentException

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

setBackgroundExecutor

Added in 1.0.0
public @NonNull ImageAnalysis.Builder setBackgroundExecutor(@NonNull 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
@NonNull Executor executor

The executor which will be used for background tasks.

Returns
@NonNull ImageAnalysis.Builder

the current Builder.

setBackpressureStrategy

Added in 1.0.0
public @NonNull 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 STRATEGY_BLOCK_PRODUCER and STRATEGY_KEEP_ONLY_LATEST.

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

Parameters
int strategy

The strategy to use.

Returns
@NonNull ImageAnalysis.Builder

The current Builder.

setImageQueueDepth

Added in 1.0.0
public @NonNull ImageAnalysis.Builder setImageQueueDepth(int depth)

Sets the number of images available to the camera pipeline for 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 analyze. 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 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 STRATEGY_BLOCK_PRODUCER mode. For 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
int depth

The total number of images available to the camera.

Returns
@NonNull ImageAnalysis.Builder

The current Builder.

setOutputImageFormat

Added in 1.1.0
public @NonNull ImageAnalysis.Builder setOutputImageFormat(int outputImageFormat)

Sets output image format.

The supported output image format is OUTPUT_IMAGE_FORMAT_YUV_420_888 and OUTPUT_IMAGE_FORMAT_RGBA_8888.

If not set, OUTPUT_IMAGE_FORMAT_YUV_420_888 will be used. Requesting OUTPUT_IMAGE_FORMAT_RGBA_8888 will have extra overhead because format conversion takes time.

Parameters
int outputImageFormat

The output image format.

Returns
@NonNull ImageAnalysis.Builder

The current Builder.

setOutputImageRotationEnabled

Added in 1.1.0
@RequiresApi(value = 23)
public @NonNull ImageAnalysis.Builder setOutputImageRotationEnabled(boolean outputImageRotationEnabled)

Enable or disable output image rotation.

On API 22 and below, this API has no effect. User needs to handle the image rotation based on the getRotationDegrees.

setTargetRotation is to adjust the rotation degree information returned by getRotationDegrees based on sensor rotation and user still needs to rotate the output image to achieve the target rotation. Once this is enabled, user doesn't need to handle the rotation, the output image will be a rotated ImageProxy and getRotationDegrees will return 0.

Turning this on will add more processing overhead to every image analysis frame. The average processing time is about 10-15ms for 640x480 image on a mid-range device. By default, the rotation is disabled.

Parameters
boolean outputImageRotationEnabled

flag to enable or disable.

Returns
@NonNull ImageAnalysis.Builder

The current Builder.

See also

setResolutionSelector

Added in 1.3.0
public @NonNull ImageAnalysis.Builder setResolutionSelector(@NonNull ResolutionSelector resolutionSelector)

Sets the resolution selector to select the preferred supported resolution.

ImageAnalysis has a default ResolutionStrategy with bound size as 640x480 and fallback rule of FALLBACK_RULE_CLOSEST_HIGHER_THEN_LOWER. Applications can override this default strategy with a different resolution strategy.

When using the camera-camera2 CameraX implementation, which resolution is finally selected depends on the camera device's hardware level, capabilities and the bound use cases combination. The device hardware level and capabilities information can be retrieved via the interop class getCameraCharacteristic with INFO_SUPPORTED_HARDWARE_LEVEL and REQUEST_AVAILABLE_CAPABILITIES.

A LIMITED-level above device can support a RECORD size resolution for ImageAnalysis when it is bound together with Preview and ImageCapture. The trade-off is the selected resolution for the ImageCapture is also restricted by the RECORD size. To successfully select a RECORD size resolution for ImageAnalysis, a ResolutionStrategy of selecting RECORD size resolution should be set on both ImageCapture and ImageAnalysis. This indicates that the application clearly understand the trade-off and prefer the ImageAnalysis to have a larger resolution rather than the ImageCapture to have a MAXIMUM size resolution. For the definitions of RECORD, MAXIMUM sizes and more details see the Regular capture section in android.hardware.camera2.CameraDevice's. The RECORD size refers to the camera device's maximum supported recording resolution, as determined by CamcorderProfile. The MAXIMUM size refers to the camera device's maximum output resolution for that format or target from getOutputSizes.

The existing setTargetResolution and setTargetAspectRatio APIs are deprecated and are not compatible with setResolutionSelector. Calling either of these APIs together with setResolutionSelector will result in an IllegalArgumentException being thrown when you attempt to build the ImageAnalysis instance.

Returns
@NonNull ImageAnalysis.Builder

The current Builder.

setTargetAspectRatio

Added in 1.0.0
Deprecated in 1.3.0
public @NonNull 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 and the resulting aspect ratio may not be exactly as requested.

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

Parameters
int aspectRatio

The desired ImageAnalysis AspectRatio

Returns
@NonNull ImageAnalysis.Builder

The current Builder.

setTargetName

Added in 1.0.0
public @NonNull ImageAnalysis.Builder setTargetName(@NonNull 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
@NonNull String targetName

A unique string identifier for the instance of the class being configured.

Returns
@NonNull ImageAnalysis.Builder

the current Builder.

setTargetResolution

Added in 1.0.0
Deprecated in 1.3.0
public @NonNull ImageAnalysis.Builder setTargetResolution(@NonNull 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 in the coordinate frame after rotating the supported sizes by the 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.

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

When using the camera-camera2 CameraX implementation, which resolution will be finally selected will depend on the camera device's hardware level and the bound use cases combination. The device hardware level information can be retrieved by INFO_SUPPORTED_HARDWARE_LEVEL from the interop class getCameraCharacteristic. A LIMITED-level above device can support a RECORD size resolution for ImageAnalysis when it is bound together with Preview and ImageCapture. The trade-off is the selected resolution for the ImageCapture will also be restricted by the RECORD size. To successfully select a RECORD size resolution for ImageAnalysis, a RECORD size target resolution should be set on both ImageCapture and ImageAnalysis. This indicates that the application clearly understand the trade-off and prefer the ImageAnalysis to have a larger resolution rather than the ImageCapture to have a MAXIMUM size resolution. For the definitions of RECORD, MAXIMUM sizes and more details see the Regular capture section in android.hardware.camera2.CameraDevice's. The RECORD size refers to the camera device's maximum supported recording resolution, as determined by CamcorderProfile. The MAXIMUM size refers to the camera device's maximum output resolution for that format or target from getOutputSizes.

Parameters
@NonNull Size resolution

The target resolution to choose from supported output sizes list.

Returns
@NonNull ImageAnalysis.Builder

The current Builder.

setTargetRotation

Added in 1.0.0
public @NonNull ImageAnalysis.Builder setTargetRotation(int rotation)

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

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.

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

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

If not set, the target rotation will default 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.

Parameters
int rotation

The rotation of the intended target.

Returns
@NonNull ImageAnalysis.Builder

The current Builder.