CameraCharacteristicsWrapper


interface CameraCharacteristicsWrapper : CameraCharacteristicsMetadata

Known direct subclasses
FakeCameraCharacteristics

A fake implementation of CameraCharacteristicsWrapper for testing.


Wrapper interface providing compatibility-focused access to CameraCharacteristics.

Use this interface to query camera capabilities, physical properties, and supported configurations. It abstracts OS version differences and caches expensive-to-retrieve properties to ensure efficient access across all API levels.

CameraCharacteristicsWrapper implements Metadata, allowing it to support type-safe retrieval of custom library-defined metadata keys via Metadata.Key in addition to native CameraCharacteristics.Key keys.

It also implements UnsafeWrapper, which allows unwrapping to the underlying native CameraCharacteristics using UnsafeWrapper.unwrapAs when platform-specific APIs are required. Note that bypassing the wrapper by unwrapping avoids compatibility fixes and caching.

Note: This interface is not stable for inheritance. Implementations should not be created directly by clients. For testing, use the fakes in androidx.camera.common.testing package (such as FakeCameraCharacteristics).

import androidx.camera.common.CameraCharacteristicsWrapper
import androidx.camera.common.CameraCharacteristicsWrappers
import androidx.camera.common.CameraId

val cameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
val characteristics = cameraManager.getCameraCharacteristics(cameraIdString)
val cameraId = CameraId(cameraIdString)

// Wrap the native CameraCharacteristics instance into a CameraCharacteristicsWrapper.
val cameraCharacteristics: CameraCharacteristicsWrapper =
    CameraCharacteristicsWrappers.wrap(cameraId, characteristics)
import androidx.camera.common.CameraCharacteristicsWrapper
import androidx.camera.common.CameraCharacteristicsWrappers
import androidx.camera.common.CameraId

val cameraId = CameraId(cameraIdString)

// Load CameraCharacteristicsWrapper directly using Context.
// This checks camera permissions and marks isRestricted accordingly.
val characteristicsFromContext: CameraCharacteristicsWrapper =
    CameraCharacteristicsWrappers.loadFrom(context, cameraId)

// Or load directly using CameraManager:
val cameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
val characteristicsFromManager: CameraCharacteristicsWrapper =
    CameraCharacteristicsWrappers.loadFrom(cameraManager, cameraId)
import androidx.camera.common.CameraCharacteristicsWrappers.availableColorSpaceProfiles
import androidx.camera.common.CameraCharacteristicsWrappers.availableDynamicRangeProfiles
import androidx.camera.common.CameraCharacteristicsWrappers.streamConfigurationMap

// Access standard CameraCharacteristics keys using the get/bracket operator:
val sensorOrientation = cameraCharacteristics[CameraCharacteristics.SENSOR_ORIENTATION]
val lensFacing = cameraCharacteristics[CameraCharacteristics.LENS_FACING]

// Access extended/custom properties using the extension properties.
// Note: Querying these properties will query custom/compatibility metadata keys first
// (such as STREAM_CONFIGURATION_MAP, AVAILABLE_COLOR_SPACE_PROFILES,
// AVAILABLE_DYNAMIC_RANGE_PROFILES) and fallback to creating internal wrappers from the
// native platform characteristics. In unit tests, test fakes supply the custom wrapped
// versions via these metadata keys.
val streamConfigurationMap = cameraCharacteristics.streamConfigurationMap
val colorSpaceProfiles = cameraCharacteristics.availableColorSpaceProfiles
val dynamicRangeProfiles = cameraCharacteristics.availableDynamicRangeProfiles
import androidx.camera.common.CameraCharacteristicsWrapper
import androidx.camera.common.CameraCharacteristicsWrappers.streamConfigurationMap
import androidx.camera.common.CameraId
import androidx.camera.common.testing.FakeCameraCharacteristics
import androidx.camera.common.testing.FakeStreamConfigurationMap
import androidx.camera.common.testing.FakeStreamConfigurationMap.OutputKey
import androidx.camera.common.testing.FakeStreamConfigurationMap.OutputValues

// Create a fake stream configuration map with custom resolutions and formats.
val fakeStreamConfigurationMap =
    FakeStreamConfigurationMap(
        outputsTable =
            linkedMapOf(
                OutputKey(ImageFormat.YUV_420_888, Size(1920, 1080)) to OutputValues(),
                OutputKey(ImageFormat.JPEG, Size(1920, 1080)) to
                    OutputValues(
                        stallDuration = 200_000_000L // 200ms stall duration
                    ),
            )
    )

// Supply the fake stream configuration map via STREAM_CONFIGURATION_MAP metadata key.
val fakeCharacteristics: CameraCharacteristicsWrapper =
    FakeCameraCharacteristics(
        cameraId = CameraId("0"),
        cameraCharacteristics =
            mapOf(
                CameraCharacteristics.LENS_FACING to CameraCharacteristics.LENS_FACING_BACK,
                CameraCharacteristics.SENSOR_ORIENTATION to 90,
            ),
        cameraMetadata =
            mapOf(
                CameraCharacteristicsWrapper.Keys.STREAM_CONFIGURATION_MAP to
                    fakeStreamConfigurationMap
            ),
    )

// The extension property accesses the wrapped version (STREAM_CONFIGURATION_MAP) first,
// returning the FakeStreamConfigurationMap in unit tests without needing real camera hardware.
val streamConfigMap = checkNotNull(fakeCharacteristics.streamConfigurationMap)
val supportedFormats = streamConfigMap.getOutputFormats()
val yuvSizes = streamConfigMap.getOutputSizes(ImageFormat.YUV_420_888)

Summary

Nested types

Custom Metadata.Key definitions for querying library-defined camera characteristics on a CameraCharacteristicsWrapper.

Public properties

CameraId

The CameraId identifying this camera device.

Set<CaptureRequest.Key<*>>

A Set of all CaptureRequest.Keys supported by this camera device for CaptureRequests.

Set<CaptureResult.Key<*>>

A Set of all CaptureResult.Keys supported by this camera device for CaptureResults.

Set<CameraCharacteristics.Key<*>>

A Set of CameraCharacteristics.Keys whose values can change dynamically based on the physical state of the device.

Boolean

Indicates whether the wrapper queried camera characteristics without camera permission.

Set<CameraCharacteristics.Key<*>>

A Set of all CameraCharacteristics.Keys supported by this camera device.

Set<CameraId>

A Set of physical camera IDs that this logical camera device is made up of.

Set<CaptureRequest.Key<*>>

A Set of physical CaptureRequest.Keys that can be overridden for physical devices backing a logical multi-camera.

Set<CameraCharacteristics.Key<*>>

A Set of CameraCharacteristics.Keys that require camera clients to obtain the Manifest.permission.CAMERA permission.

Set<CaptureRequest.Key<*>>

A Set of CaptureRequest.Keys that the camera device can pass as part of the capture session initialization.

Set<CameraCharacteristics.Key<*>>

A Set of CameraCharacteristics.Keys whose values are capture session specific.

Inherited functions

From androidx.camera.common.CameraCharacteristicsMetadata
operator T?

Retrieves the value of the specified CameraCharacteristics.Key.

open T
<T : Any> getOrDefault(key: CameraCharacteristics.Key<T>, default: T)

Retrieves the value of the specified CameraCharacteristics.Key, or returns default if the value is null or the key is unsupported.

From androidx.camera.common.Metadata
operator T?
<T : Any> get(key: Metadata.Key<T>)

Retrieves the value associated with the specified Metadata.Key.

open T
<T : Any> getOrDefault(key: Metadata.Key<T>, default: T)

Retrieves the value associated with the specified Metadata.Key, or returns default if the key is not present.

From androidx.camera.common.UnsafeWrapper
T?
<T : Any> unwrapAs(type: Class<T>)

Attempts to unwrap this object into the specified underlying type.

Inherited properties

From androidx.camera.common.Metadata
Set<Metadata.Key<*>>

The set of all custom Metadata.Keys currently available in this container.

Public properties

cameraId

Added in 1.7.0-rc01
val cameraId: CameraId

The CameraId identifying this camera device.

This corresponds to the camera ID used to open the camera with android.hardware.camera2.CameraManager.openCamera.

captureRequestKeys

Added in 1.7.0-rc01
val captureRequestKeys: Set<CaptureRequest.Key<*>>

A Set of all CaptureRequest.Keys supported by this camera device for CaptureRequests.

This property is equivalent to calling CameraCharacteristics.getAvailableCaptureRequestKeys, but may be cached for efficiency.

captureResultKeys

Added in 1.7.0-rc01
val captureResultKeys: Set<CaptureResult.Key<*>>

A Set of all CaptureResult.Keys supported by this camera device for CaptureResults.

This property is equivalent to calling CameraCharacteristics.getAvailableCaptureResultKeys, but may be cached for efficiency.

dynamicKeys

Added in 1.7.0-rc01
val dynamicKeys: Set<CameraCharacteristics.Key<*>>

A Set of CameraCharacteristics.Keys whose values can change dynamically based on the physical state of the device.

Do not cache values retrieved using these keys. The values can change at runtime, for example when a foldable device changes its posture.

At present, only the SENSOR_ORIENTATION key on foldable devices can change based on the posture of the device, but may be expanded in the future if other camera properties change dynamically based on the posture or other physical state of the device.

The primary purpose of this property is to indicate which keys should not be cached.

isRestricted

Added in 1.7.0-rc01
val isRestricted: Boolean

Indicates whether the wrapper queried camera characteristics without camera permission.

When true, the application did not hold the android.Manifest.permission.CAMERA permission when this instance was created. In this restricted mode, querying keys listed in restrictedKeys may return null or empty values.

keys

Added in 1.7.0-rc01
val keys: Set<CameraCharacteristics.Key<*>>

A Set of all CameraCharacteristics.Keys supported by this camera device.

This property is equivalent to calling android.hardware.camera2.CameraCharacteristics.getKeys on the underlying camera characteristics, but may be cached for efficiency.

See also
getKeys

physicalCameraIds

Added in 1.7.0-rc01
val physicalCameraIds: Set<CameraId>

A Set of physical camera IDs that this logical camera device is made up of.

On API levels prior to 28 (Android P), this property returns an empty set.

physicalCaptureRequestKeys

Added in 1.7.0-rc01
val physicalCaptureRequestKeys: Set<CaptureRequest.Key<*>>

A Set of physical CaptureRequest.Keys that can be overridden for physical devices backing a logical multi-camera.

On API levels prior to 28 (Android P), this property returns an empty set.

restrictedKeys

Added in 1.7.0-rc01
val restrictedKeys: Set<CameraCharacteristics.Key<*>>

A Set of CameraCharacteristics.Keys that require camera clients to obtain the Manifest.permission.CAMERA permission.

On API levels prior to 29 (Android Q), this property returns an empty set.

sessionCaptureRequestKeys

Added in 1.7.0-rc01
val sessionCaptureRequestKeys: Set<CaptureRequest.Key<*>>

A Set of CaptureRequest.Keys that the camera device can pass as part of the capture session initialization.

On API levels prior to 28 (Android P), this property returns an empty set.

sessionKeys

Added in 1.7.0-rc01
val sessionKeys: Set<CameraCharacteristics.Key<*>>

A Set of CameraCharacteristics.Keys whose values are capture session specific.

On API levels prior to 35 (Android 15), this property returns an empty set.