ExtensionsManager


public final class ExtensionsManager


Provides interfaces for third party app developers to get capabilities info of extension functions.

Many Android devices contain powerful cameras, with manufacturers devoting a lot of effort to build many cutting-edge features, or special effects, into these camera devices. CameraX Extensions allows third party apps to enable the available extension modes on the supported devices. The extension modes which might be supported via CameraX Extensions are ExtensionMode.BOKEH, ExtensionMode.HDR, ExtensionMode.NIGHT, ExtensionMode.FACE_RETOUCH and ExtensionMode.AUTO. The known supported devices are listed in the Supported devices page.

CameraX Extensions are built on the top of CameraX Core libraries. To enable an extension mode, an ExtensionsManager instance needs to be retrieved first. For kotlin users, it is recommended to use ExtensionsManager.getInstance which is a suspend function. For Java users, please use getInstanceAsync.

After retrieving the ExtensionsManager instance, the availability of a specific extension mode can be checked by isExtensionAvailable. For an available extension mode, an extension enabled CameraSelector can be obtained by calling getExtensionEnabledCameraSelector. After binding use cases by the extension enabled CameraSelector, the extension mode will be applied to the bound Preview and ImageCapture. The following sample code describes how to enable an extension mode for use cases.

suspend fun bindUseCasesWithBokehMode(context: Context, lifecycleOwner: LifecycleOwner) {
// Create a camera provider.
val cameraProvider = ProcessCameraProvider.awaitInstance(context)

// Retrieve the ExtensionsManager instance.
val extensionsManager = ExtensionsManager.getInstance(context, cameraProvider)

val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
// Query if extension is available.
if (extensionsManager.isExtensionAvailable(cameraSelector, ExtensionMode.BOKEH)) {
// Create an ExtensionSessionConfig.
val imageCapture = ImageCapture.Builder().build()
val preview = Preview.Builder().build()
ExtensionSessionConfig sessionConfig = ExtensionSessionConfig(
ExtensionMode.BOKEH,
extensionsManager,
imageCapture,
preview
)
cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector, sessionConfig)
}
}

Without enabling CameraX Extensions, any device should be able to support the use cases combination of ImageCapture, Preview and ImageAnalysis. To support the CameraX Extensions functionality, the ImageCapture or Preview might need to occupy a different format of stream. This might restrict the app to not be able to bind ImageCapture, Preview and ImageAnalysis at the same time if the device's hardware level is not CameraMetadata.INFO_SUPPORTED_HARDWARE_LEVEL_FULL or above. If enabling an extension mode is more important and the ImageAnalysis could be optional to the app design, the extension mode can be enabled successfully when only binding ImageCapture, Preview even if the device's hardware level is CameraMetadata.INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED.

While CameraX Extensions dose not directly support androidx.camera.video.VideoCapture, androidx.camera.video.VideoCapture can still be used when any extension mode is enabled. When the app binds androidx.camera.video.VideoCapture and enables any extension mode, androidx.camera.video.VideoCapture can obtain the shared stream of Preview and record it as a video.

For some devices, the vendor library implementation might only support a subset of the all supported sizes retrieved by StreamConfigurationMap.getOutputSizes. CameraX will select the supported sizes for the use cases according to the use cases' configuration and combination.

Summary

Public methods

final CameraExtensionsControl

Retrieves a CameraExtensionsControl object that allows customization of capture request settings for supported camera extensions.

final @NonNull CameraExtensionsInfo

Retrieves a CameraExtensionsInfo object that allows to observe or monitor capture request settings and results for supported camera extensions.

final Range<@NonNull Long>
getEstimatedCaptureLatencyRange(
    @NonNull CameraSelector cameraSelector,
    int mode
)

Returns the estimated capture latency range in milliseconds for the target camera and extension mode.

final @NonNull CameraSelector
getExtensionEnabledCameraSelector(
    @NonNull CameraSelector cameraSelector,
    int mode
)

Returns a modified CameraSelector that will enable the specified extension mode.

static final @NonNull ExtensionsManager
getInstance(
    @NonNull Context context,
    @NonNull CameraProvider cameraProvider
)

Retrieves the ExtensionsManager.

static final @NonNull ListenableFuture<@NonNull ExtensionsManager>
getInstanceAsync(
    @NonNull Context context,
    @NonNull CameraProvider cameraProvider
)

Retrieves the ExtensionsManager associated with the current process.

final boolean
isExtensionAvailable(@NonNull CameraSelector cameraSelector, int mode)

Checks if a specific extension mode is available for a given CameraSelector.

final boolean
isImageAnalysisSupported(
    @NonNull CameraSelector cameraSelector,
    int mode
)

Returns whether the given extension mode supports the ImageAnalysis use case on the camera specified by the given CameraSelector.

Public methods

getCameraExtensionsControl

Added in 1.4.0
public final CameraExtensionsControl getCameraExtensionsControl(@NonNull CameraControl cameraControl)

Retrieves a CameraExtensionsControl object that allows customization of capture request settings for supported camera extensions.

Parameters
@NonNull CameraControl cameraControl

the camera control for a camera with a specific extension mode turned on.

Returns
CameraExtensionsControl

a CameraExtensionsControl object to manage extension-related settings. Or returns null if the provided CameraControl doesn't represent a camera with enabled extensions.

getCameraExtensionsInfo

Added in 1.4.0
public final @NonNull CameraExtensionsInfo getCameraExtensionsInfo(@NonNull CameraInfo cameraInfo)

Retrieves a CameraExtensionsInfo object that allows to observe or monitor capture request settings and results for supported camera extensions.

If the provided CameraInfo doesn't represent a camera with enabled extensions, a placeholder CameraExtensionsInfo object will be returned, indicating no extension type and strength support.

Parameters
@NonNull CameraInfo cameraInfo

the camera info for a camera with a specific extension mode turned on.

Returns
@NonNull CameraExtensionsInfo

a CameraExtensionsInfo object for observing extension-specific capture request settings and results.

getEstimatedCaptureLatencyRange

Added in 1.1.0
public final Range<@NonNull LonggetEstimatedCaptureLatencyRange(
    @NonNull CameraSelector cameraSelector,
    int mode
)

Returns the estimated capture latency range in milliseconds for the target camera and extension mode.

This includes the time spent processing the multi-frame capture request along with any additional time for encoding of the processed buffer in the framework if necessary.

Parameters
@NonNull CameraSelector cameraSelector

The CameraSelector to find a camera which supports the specified extension mode.

int mode

The extension mode to check.

Returns
Range<@NonNull Long>

the range of estimated minimal and maximal capture latency in milliseconds. Returns null if no capture latency info can be provided or if the device doesn't support the extension mode on this camera.

getExtensionEnabledCameraSelector

Added in 1.1.0
public final @NonNull CameraSelector getExtensionEnabledCameraSelector(
    @NonNull CameraSelector cameraSelector,
    int mode
)

Returns a modified CameraSelector that will enable the specified extension mode.

The returned extension CameraSelector can be used to bind use cases to a desired LifecycleOwner and then the specified extension mode will be enabled on the camera.

Parameters
@NonNull CameraSelector cameraSelector

The base CameraSelector on top of which the extension config is applied. isExtensionAvailable can be used to check whether any camera can support the specified extension mode for the base camera selector.

int mode

The target extension mode.

Returns
@NonNull CameraSelector

a CameraSelector for the specified Extensions mode.

Throws
kotlin.IllegalArgumentException

If this device doesn't support extensions function, no camera can be found to support the specified extension mode, or the base CameraSelector has contained extension related configuration in it.

getInstance

public static final @NonNull ExtensionsManager getInstance(
    @NonNull Context context,
    @NonNull CameraProvider cameraProvider
)

Retrieves the ExtensionsManager.

Parameters
@NonNull Context context

The application context.

@NonNull CameraProvider cameraProvider

A CameraProvider will be used to query the information of cameras on the device.

Returns
@NonNull ExtensionsManager

A fully initialized ExtensionsManager for the current process.

See also
getInstanceAsync

getInstanceAsync

Added in 1.6.0-alpha02
public static final @NonNull ListenableFuture<@NonNull ExtensionsManagergetInstanceAsync(
    @NonNull Context context,
    @NonNull CameraProvider cameraProvider
)

Retrieves the ExtensionsManager associated with the current process.

An application must wait until the ListenableFuture completes to get an ExtensionsManager instance. The ExtensionsManager instance can be used to access the extensions related functions.

Parameters
@NonNull Context context

The context to initialize the extensions library.

@NonNull CameraProvider cameraProvider

A CameraProvider will be used to query the information of cameras on the device. The CameraProvider can be the androidx.camera.lifecycle.ProcessCameraProvider which is obtained by androidx.camera.lifecycle.ProcessCameraProvider.getInstance.

isExtensionAvailable

Added in 1.1.0
public final boolean isExtensionAvailable(@NonNull CameraSelector cameraSelector, int mode)

Checks if a specific extension mode is available for a given CameraSelector.

To use Ultra HDR, you must first check for support and then enable the format. This feature is available on capable devices starting from API level 34.

  1. Obtain a CameraInfo instance by calling CameraProvider.getCameraInfo with the extension-enabled CameraSelector from getExtensionEnabledCameraSelector.

  2. Use this CameraInfo to get the ImageCaptureCapabilities via ImageCapture.getImageCaptureCapabilities.

  3. Check the supported formats by calling ImageCaptureCapabilities.getSupportedOutputFormats. The presence of ImageCapture.OUTPUT_FORMAT_JPEG_ULTRA_HDR indicates Ultra HDR support.

  4. When Ultra HDR is supported, configure the ImageCapture to use it by calling ImageCapture.Builder.setOutputFormat with ImageCapture.OUTPUT_FORMAT_JPEG_ULTRA_HDR.

Note: Camera extensions do not support 10-bit preview or video capture. When using extensions, the dynamic range must be DynamicRange.SDR (the default).

Parameters
@NonNull CameraSelector cameraSelector

The base CameraSelector used to select the camera.

int mode

The extension mode to verify.

Returns
boolean

true if the extension mode is available for the camera selector, false otherwise.

isImageAnalysisSupported

Added in 1.3.0
public final boolean isImageAnalysisSupported(
    @NonNull CameraSelector cameraSelector,
    int mode
)

Returns whether the given extension mode supports the ImageAnalysis use case on the camera specified by the given CameraSelector. If it returns false, invoking ProcessCameraProvider.bindToLifecycle with an ImageAnalysis use case will throw an IllegalArgumentException.

Parameters
@NonNull CameraSelector cameraSelector

The CameraSelector to find a camera which supports the specified extension mode.

int mode

The extension mode to check.

Returns
boolean

true if ImageAnalysis can be bound when the specified extension mode is enabled on the camera specified by the given CameraSelector. Returns false otherwise. If the device doesn't support this extension mode on this camera, it will also return false.