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:
-
Apply Camera2 configuration to
androidx.camera.core.UseCasebuilders usingforUseCase,forImageCapture, orforSessionConfig -
Apply Camera2 configuration to active camera controls using
forCameraControl -
Query Camera2 metadata using
getCameraIdorgetCameraCharacteristics -
Create camera selectors or filters using
getCameraSelectorFromCameraIdorgetCameraFilterFromCameraId
Summary
Nested types |
|---|
@ExperimentalCamera2InteropThis 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
forCameraControl
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:
-
Capture request key-value pairs (
CameraControlCamera2Interop.setCaptureRequestOption) -
Clearing specific capture request keys (
CameraControlCamera2Interop.clearCaptureRequestOption) -
Clearing all capture request keys (
CameraControlCamera2Interop.clearAllCaptureRequestOptions) -
Capture request template type (
CameraControlCamera2Interop.setRepeatingCaptureRequestTemplateorCameraControlCamera2Interop.repeatingCaptureRequestTemplate) -
Repeating capture callbacks (
CameraControlCamera2Interop.setRepeatingCaptureCallbackorCameraControlCamera2Interop.repeatingCaptureCallback)
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 |
forImageCapture
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:
-
Physical camera ID (
ImageCaptureCamera2Interop.setPhysicalCameraId) -
Stream use case (
ImageCaptureCamera2Interop.setStreamUseCase) -
Mirror mode (
ImageCaptureCamera2Interop.setMirrorMode) -
Timestamp base (
ImageCaptureCamera2Interop.setTimestampBase) -
Dynamic range profile (
ImageCaptureCamera2Interop.setDynamicRangeProfile) -
Surface group ID (
ImageCaptureCamera2Interop.setSurfaceGroupId) -
Still capture request key-value pairs (
ImageCaptureCamera2Interop.setStillCaptureRequestOption) -
Still capture request template type (
ImageCaptureCamera2Interop.setStillCaptureRequestTemplateType) -
Still capture callbacks (
ImageCaptureCamera2Interop.setStillCaptureCallback)
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 |
forSessionConfig
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:
-
Session parameters (
SessionConfigCamera2Interop.setSessionParameter) -
Session type (
SessionConfigCamera2Interop.setSessionType) -
Color space (
SessionConfigCamera2Interop.setColorSpace) -
Session state callbacks (
SessionConfigCamera2Interop.setSessionStateCallback) -
Device state callbacks (
SessionConfigCamera2Interop.setDeviceStateCallback) -
Capture request key-value pairs (
SessionConfigCamera2Interop.setCaptureRequestOption) -
Clearing specific capture request keys (
SessionConfigCamera2Interop.clearCaptureRequestOption) -
Clearing all capture request keys (
SessionConfigCamera2Interop.clearAllCaptureRequestOptions) -
Capture request template type (
SessionConfigCamera2Interop.setRepeatingCaptureRequestTemplate) -
Repeating capture callbacks (
SessionConfigCamera2Interop.setRepeatingCaptureCallback)
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 |
forUseCase
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:
-
Physical camera ID (
UseCaseCamera2Interop.setPhysicalCameraId) -
Stream use case (
UseCaseCamera2Interop.setStreamUseCase) -
Mirror mode (
UseCaseCamera2Interop.setMirrorMode) -
Timestamp base (
UseCaseCamera2Interop.setTimestampBase) -
Dynamic range profile (
UseCaseCamera2Interop.setDynamicRangeProfile) -
Surface group ID (
UseCaseCamera2Interop.setSurfaceGroupId)
For androidx.camera.core.ImageCapture.Builder, use forImageCapture.
| Parameters | |
|---|---|
configurator: Consumer<UseCaseCamera2Interop> |
the callback applying Camera2 options to |
getCameraCharacteristics
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 |
| Returns | |
|---|---|
CameraCharacteristics |
|
| Throws | |
|---|---|
IllegalArgumentException |
if |
getCameraFilterFromCameraId
fun getCameraFilterFromCameraId(cameraId: String): CameraFilter
Creates a CameraFilter that matches a specific Camera2 camera ID.
| Parameters | |
|---|---|
cameraId: String |
target Camera2 camera ID |
| Returns | |
|---|---|
CameraFilter |
|
getCameraId
fun getCameraId(cameraInfo: CameraInfo): String
Returns the Camera2 camera ID from a CameraInfo.
Example:
String cameraId = Camera2Interop.getCameraId(cameraInfo);
| Parameters | |
|---|---|
cameraInfo: CameraInfo |
target |
| Returns | |
|---|---|
String |
Camera2 camera ID |
| Throws | |
|---|---|
IllegalArgumentException |
if |
getCameraSelectorFromCameraId
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 |
|