Camera2Interop


class Camera2Interop


Provides utilities to configure and query Camera2 APIs.

Note: Using Camera2 interop options can override internal CameraX configurations. If an option configured via interop conflicts with options required by CameraX internally, the option from Camera2Interop will override, which may result in unexpected behavior or interfere with CameraX functionality.

Use this class to:

Summary

Nested types

This class is deprecated. Use the camera2Interop extension function on UseCase.Builder (e.g., 'builder.camera2Interop { setPhysicalCameraId(physicalCameraId) }') or SessionConfig.Builder for capture request options and callbacks instead.

Public companion functions

CameraControlCamera2Configurator

Creates a CameraControlCamera2Configurator to modify a androidx.camera.core.CameraControl.

ImageCaptureCamera2Configurator

Creates an ImageCaptureCamera2Configurator to modify ImageCapture builders.

SessionConfigCamera2Configurator

Creates a SessionConfigCamera2Configurator to modify a session configuration builder.

UseCaseCamera2Configurator

Creates a UseCaseCamera2Configurator to modify Preview, ImageAnalysis, and VideoCapture builders.

CameraCharacteristics

Returns the CameraCharacteristics from a CameraInfo.

CameraFilter

Creates a CameraFilter that matches a specific Camera2 camera ID.

String
getCameraId(cameraInfo: CameraInfo)

Returns the Camera2 camera ID from a CameraInfo.

CameraSelector

Creates a CameraSelector targeting a specific Camera2 camera ID.

Public companion functions

forCameraControl

Added in 1.7.0-alpha03
fun forCameraControl(configurator: Consumer<CameraControlCamera2Interop>): CameraControlCamera2Configurator

Creates a CameraControlCamera2Configurator to modify a androidx.camera.core.CameraControl.

Use this with androidx.camera.core.CameraControl.applyInteropAsync to dynamically send Camera2 capture request options to an active camera. All options configured within a single configurator are applied atomically in a single repeating capture request update. Subsequent calls to androidx.camera.core.CameraControl.applyInteropAsync update parameters incrementally without clearing previously set keys, unless explicitly cleared using CameraControlCamera2Interop.clearCaptureRequestOption or CameraControlCamera2Interop.clearAllCaptureRequestOptions. This overwrites options set with SessionConfigCamera2Interop via androidx.camera.core.SessionConfig.Builder.setInterop.

The CameraControlCamera2Interop target allows configuring options such as:

Warning: Callbacks configured via interop receive raw android.hardware.camera2.CameraCaptureSession instances. Directly invoking state-altering methods on these raw objects (such as android.hardware.camera2.CameraCaptureSession.close or android.hardware.camera2.CameraCaptureSession.abortCaptures) bypasses CameraX pipeline management and may cause state desynchronization, stream interruption, or application crashes.

Parameters
configurator: Consumer<CameraControlCamera2Interop>

the callback applying Camera2 options to CameraControlCamera2Interop

forImageCapture

Added in 1.7.0-alpha03
fun forImageCapture(configurator: Consumer<ImageCaptureCamera2Interop>): ImageCaptureCamera2Configurator

Creates an ImageCaptureCamera2Configurator to modify ImageCapture builders.

Use this with androidx.camera.core.ImageCapture.Builder.setInterop in Java to apply Camera2 options. The ImageCaptureCamera2Interop target allows configuring options such as:

The capture request keys for one-shot still captures (such as androidx.camera.core.ImageCapture.takePicture) are determined by copying all repeating request keys (which may include keys added via androidx.camera.core.SessionConfig.Builder.setInterop or androidx.camera.core.CameraControl.applyInteropAsync) and then overriding them with the still capture request keys configured here.

Warning: Callbacks configured via interop receive raw android.hardware.camera2.CameraCaptureSession instances. Directly invoking state-altering methods on these raw objects (such as android.hardware.camera2.CameraCaptureSession.close or android.hardware.camera2.CameraCaptureSession.abortCaptures) bypasses CameraX pipeline management and may cause state desynchronization, stream interruption, or application crashes.

Parameters
configurator: Consumer<ImageCaptureCamera2Interop>

the callback applying Camera2 options to ImageCaptureCamera2Interop

forSessionConfig

Added in 1.7.0-alpha03
fun forSessionConfig(configurator: Consumer<SessionConfigCamera2Interop>): SessionConfigCamera2Configurator

Creates a SessionConfigCamera2Configurator to modify a session configuration builder.

Use this with androidx.camera.core.SessionConfig.Builder.setInterop in Java to apply Camera2 options to a custom session configuration. The SessionConfigCamera2Interop target allows configuring options such as:

Warning: Callbacks configured via interop receive raw android.hardware.camera2.CameraDevice and android.hardware.camera2.CameraCaptureSession instances. Directly invoking state-altering methods on these raw objects (such as android.hardware.camera2.CameraCaptureSession.close, android.hardware.camera2.CameraCaptureSession.abortCaptures, or android.hardware.camera2.CameraDevice.close) bypasses CameraX pipeline management and may cause state desynchronization, stream interruption, or application crashes.

Parameters
configurator: Consumer<SessionConfigCamera2Interop>

the callback applying Camera2 options to SessionConfigCamera2Interop

forUseCase

Added in 1.7.0-alpha03
fun forUseCase(configurator: Consumer<UseCaseCamera2Interop>): UseCaseCamera2Configurator

Creates a UseCaseCamera2Configurator to modify Preview, ImageAnalysis, and VideoCapture builders.

Use this with androidx.camera.core.UseCase.InteropConfigurable.setInterop in Java to apply Camera2 options. The UseCaseCamera2Interop target allows configuring options such as:

For androidx.camera.core.ImageCapture.Builder, use forImageCapture.

Parameters
configurator: Consumer<UseCaseCamera2Interop>

the callback applying Camera2 options to UseCaseCamera2Interop

getCameraCharacteristics

Added in 1.7.0-alpha03
fun getCameraCharacteristics(cameraInfo: CameraInfo): CameraCharacteristics

Returns the CameraCharacteristics from a CameraInfo.

Example:

CameraCharacteristics characteristics = Camera2Interop.getCameraCharacteristics(cameraInfo);
Integer sensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
Parameters
cameraInfo: CameraInfo

target CameraInfo

Throws
IllegalArgumentException

if cameraInfo does not contain Camera2 information

getCameraFilterFromCameraId

Added in 1.7.0-alpha03
fun getCameraFilterFromCameraId(cameraId: String): CameraFilter

Creates a CameraFilter that matches a specific Camera2 camera ID.

Parameters
cameraId: String

target Camera2 camera ID

getCameraId

Added in 1.7.0-alpha03
fun getCameraId(cameraInfo: CameraInfo): String

Returns the Camera2 camera ID from a CameraInfo.

Example:

String cameraId = Camera2Interop.getCameraId(cameraInfo);
Parameters
cameraInfo: CameraInfo

target CameraInfo

Returns
String

Camera2 camera ID

Throws
IllegalArgumentException

if cameraInfo does not contain Camera2 information

getCameraSelectorFromCameraId

Added in 1.7.0-alpha03
fun getCameraSelectorFromCameraId(cameraId: String): CameraSelector

Creates a CameraSelector targeting a specific Camera2 camera ID.

Example:

CameraSelector cameraSelector = Camera2Interop.getCameraSelectorFromCameraId("0");
cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector, useCase);
Parameters
cameraId: String

target Camera2 camera ID

Returns
CameraSelector

CameraSelector that filters for cameraId