ImageAnalysis.Builder

class ImageAnalysis.Builder : ExtendableBuilder


Builder for a ImageAnalysis.

Summary

Public constructors

Creates a new Builder object.

Public functions

ImageAnalysis

Builds an ImageAnalysis from the current state.

ImageAnalysis.Builder

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

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.

ImageAnalysis.Builder

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

ImageAnalysis.Builder
setOutputImageFormat(outputImageFormat: Int)

Sets output image format.

ImageAnalysis.Builder
@RequiresApi(value = 23)
setOutputImageRotationEnabled(outputImageRotationEnabled: Boolean)

Enable or disable output image rotation.

ImageAnalysis.Builder

Sets the resolution selector to select the preferred supported resolution.

ImageAnalysis.Builder
setTargetAspectRatio(aspectRatio: Int)

This function is deprecated.

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

ImageAnalysis.Builder
setTargetName(targetName: String)

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

ImageAnalysis.Builder

This function is deprecated.

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

ImageAnalysis.Builder

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

Public constructors

Builder

Added in 1.0.0
Builder()

Creates a new Builder object.

Public functions

build

Added in 1.4.0-alpha04
fun build(): ImageAnalysis

Builds an ImageAnalysis from the current state.

Returns
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
fun setBackgroundExecutor(executor: Executor): ImageAnalysis.Builder

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

Added in 1.0.0
fun setBackpressureStrategy(strategy: Int): 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.

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
strategy: Int

The strategy to use.

Returns
ImageAnalysis.Builder

The current Builder.

setImageQueueDepth

Added in 1.0.0
fun setImageQueueDepth(depth: Int): ImageAnalysis.Builder

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
depth: Int

The total number of images available to the camera.

Returns
ImageAnalysis.Builder

The current Builder.

setOutputImageFormat

Added in 1.1.0
fun setOutputImageFormat(outputImageFormat: Int): ImageAnalysis.Builder

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
outputImageFormat: Int

The output image format.

Returns
ImageAnalysis.Builder

The current Builder.

setOutputImageRotationEnabled

Added in 1.1.0
@RequiresApi(value = 23)
fun setOutputImageRotationEnabled(outputImageRotationEnabled: Boolean): ImageAnalysis.Builder

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
outputImageRotationEnabled: Boolean

flag to enable or disable.

Returns
ImageAnalysis.Builder

The current Builder.

See also

setResolutionSelector

Added in 1.3.0
fun setResolutionSelector(resolutionSelector: ResolutionSelector): ImageAnalysis.Builder

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
ImageAnalysis.Builder

The current Builder.

setTargetAspectRatio

Added in 1.0.0
Deprecated in 1.3.0
fun setTargetAspectRatio(aspectRatio: Int): ImageAnalysis.Builder

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
aspectRatio: Int

The desired ImageAnalysis AspectRatio

Returns
ImageAnalysis.Builder

The current Builder.

setTargetName

Added in 1.0.0
fun setTargetName(targetName: String): ImageAnalysis.Builder

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

Added in 1.0.0
Deprecated in 1.3.0
fun setTargetResolution(resolution: Size): ImageAnalysis.Builder

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
resolution: Size

The target resolution to choose from supported output sizes list.

Returns
ImageAnalysis.Builder

The current Builder.

setTargetRotation

Added in 1.0.0
fun setTargetRotation(rotation: Int): ImageAnalysis.Builder

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
rotation: Int

The rotation of the intended target.

Returns
ImageAnalysis.Builder

The current Builder.