public final class

ImageAnalysis.Builder

extends java.lang.Object

implements ImageOutputConfig.Builder<ImageAnalysis.Builder>, ThreadConfig.Builder<ImageAnalysis.Builder>, UseCaseConfig.Builder<ImageAnalysis, ImageAnalysisConfig, ImageAnalysis.Builder>

 java.lang.Object

↳androidx.camera.core.ImageAnalysis.Builder

Overview

Builder for a ImageAnalysis.

Summary

Constructors
publicBuilder()

Creates a new Builder object.

Methods
public ImageAnalysisbuild()

Builds an ImageAnalysis from the current state.

public static ImageAnalysis.BuilderfromConfig(ImageAnalysisConfig configuration)

Generates a Builder from another Config object.

public MutableConfiggetMutableConfig()

public ImageAnalysisConfiggetUseCaseConfig()

public ImageAnalysis.BuildersetBackgroundExecutor(java.util.concurrent.Executor executor)

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

public ImageAnalysis.BuildersetBackpressureStrategy(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.

public ImageAnalysis.BuildersetCameraSelector(CameraSelector cameraSelector)

public ImageAnalysis.BuildersetCaptureOptionUnpacker(CaptureConfig.OptionUnpacker optionUnpacker)

public ImageAnalysis.BuildersetDefaultCaptureConfig(CaptureConfig captureConfig)

public ImageAnalysis.BuildersetDefaultResolution(Size resolution)

Sets the default resolution of the intended target from this configuration.

public ImageAnalysis.BuildersetDefaultSessionConfig(SessionConfig sessionConfig)

public ImageAnalysis.BuildersetImageQueueDepth(int depth)

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

public ImageAnalysis.BuildersetImageReaderProxyProvider(ImageReaderProxyProvider imageReaderProxyProvider)

public ImageAnalysis.BuildersetMaxResolution(Size resolution)

public ImageAnalysis.BuildersetOnePixelShiftEnabled(boolean onePixelShiftEnabled)

public ImageAnalysis.BuildersetOutputImageFormat(int outputImageFormat)

Sets output image format.

public ImageAnalysis.BuildersetOutputImageRotationEnabled(boolean outputImageRotationEnabled)

Enable or disable output image rotation.

public ImageAnalysis.BuildersetSessionOptionUnpacker(SessionConfig.OptionUnpacker optionUnpacker)

public ImageAnalysis.BuildersetSupportedResolutions(java.util.List<> resolutions)

public ImageAnalysis.BuildersetSurfaceOccupancyPriority(int priority)

public ImageAnalysis.BuildersetTargetAspectRatio(int aspectRatio)

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

public ImageAnalysis.BuildersetTargetClass(java.lang.Class<ImageAnalysis> targetClass)

public ImageAnalysis.BuildersetTargetName(java.lang.String targetName)

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

public ImageAnalysis.BuildersetTargetResolution(Size resolution)

Sets the resolution of the intended target from this configuration.

public ImageAnalysis.BuildersetTargetRotation(int rotation)

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

public ImageAnalysis.BuildersetUseCaseEventCallback(UseCase.EventCallback useCaseEventCallback)

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public Builder()

Creates a new Builder object.

Methods

public static ImageAnalysis.Builder fromConfig(ImageAnalysisConfig configuration)

Generates a Builder from another Config object.

Parameters:

configuration: An immutable configuration to pre-populate this builder.

Returns:

The new Builder.

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: The strategy to use.

Returns:

The current Builder.

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: The total number of images available to the camera.

Returns:

The current Builder.

public ImageAnalysis.Builder setOutputImageFormat(int outputImageFormat)

Sets output image format.

The supported output image format is ImageAnalysis.OUTPUT_IMAGE_FORMAT_YUV_420_888 and ImageAnalysis.OUTPUT_IMAGE_FORMAT_RGBA_8888.

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

Parameters:

outputImageFormat: The output image format.

Returns:

The current Builder.

public ImageAnalysis.Builder setOutputImageRotationEnabled(boolean outputImageRotationEnabled)

Enable or disable output image rotation.

ImageAnalysis.setTargetRotation(int) is to adjust the rotation degree information returned by ImageInfo.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 ImageInfo.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: flag to enable or disable.

Returns:

The current Builder.

public ImageAnalysis.Builder setOnePixelShiftEnabled(boolean onePixelShiftEnabled)

public MutableConfig getMutableConfig()

public ImageAnalysisConfig getUseCaseConfig()

public ImageAnalysis build()

Builds an ImageAnalysis from the current state.

Returns:

A ImageAnalysis populated with the current state.

public ImageAnalysis.Builder setTargetClass(java.lang.Class<ImageAnalysis> targetClass)

public ImageAnalysis.Builder setTargetName(java.lang.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: A unique string identifier for the instance of the class being configured.

Returns:

the current Builder.

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 and the resulting aspect ratio may not be exactly as requested.

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

Parameters:

aspectRatio: The desired ImageAnalysis AspectRatio

Returns:

The current Builder.

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: , , , . Rotation values are relative to the "natural" rotation, .

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 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: The rotation of the intended target.

Returns:

The current Builder.

See also: ImageAnalysis.setTargetRotation(int)

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 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 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 from the interop class Camera2CameraInfo. 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 . The MAXIMUM size refers to the camera device's maximum output resolution for that format or target from .

Parameters:

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

Returns:

The current Builder.

public ImageAnalysis.Builder setDefaultResolution(Size resolution)

Sets the default resolution of the intended target from this configuration.

Parameters:

resolution: The default resolution to choose from supported output sizes list.

Returns:

The current Builder.

public ImageAnalysis.Builder setMaxResolution(Size resolution)

public ImageAnalysis.Builder setSupportedResolutions(java.util.List<> resolutions)

public ImageAnalysis.Builder setBackgroundExecutor(java.util.concurrent.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 java.util.concurrent.Executor.

Parameters:

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

Returns:

the current Builder.

public ImageAnalysis.Builder setDefaultSessionConfig(SessionConfig sessionConfig)

public ImageAnalysis.Builder setDefaultCaptureConfig(CaptureConfig captureConfig)

public ImageAnalysis.Builder setSessionOptionUnpacker(SessionConfig.OptionUnpacker optionUnpacker)

public ImageAnalysis.Builder setCaptureOptionUnpacker(CaptureConfig.OptionUnpacker optionUnpacker)

public ImageAnalysis.Builder setSurfaceOccupancyPriority(int priority)

public ImageAnalysis.Builder setCameraSelector(CameraSelector cameraSelector)

public ImageAnalysis.Builder setUseCaseEventCallback(UseCase.EventCallback useCaseEventCallback)

public ImageAnalysis.Builder setImageReaderProxyProvider(ImageReaderProxyProvider imageReaderProxyProvider)