CameraIdentifier


class CameraIdentifier


An opaque, stable identifier for a camera device recognized by CameraX.

This identifier is designed to be unique and stable for a specific camera device across application sessions and device connect/disconnect events, provided the underlying system can consistently identify the camera. It serves as a reliable key for uniquely identifying cameras within CameraX APIs, such as in CameraPresenceListener callbacks.

Applications should not attempt to parse the string representation of this identifier or assume any particular format for its internal value, as it may change across CameraX versions or device implementations.

Instances of CameraIdentifier are vended by CameraX (e.g., via CameraInfo.getCameraIdentifier). Applications should not instantiate this class directly.

To re-select a camera for binding, use the CameraSelector.of factory method. This class also includes convenience methods like isOf to check if this identifier corresponds to a given Camera instance.

Selecting a Specific Camera

The primary way to use a CameraIdentifier is to re-select that specific camera for binding. This is done by creating a CameraSelector with the CameraSelector.of static factory method.

val cameraProvider = ...
val specificId = ... // From a listener or saved preferences

// Create a selector that will only match the specific camera
val selector = CameraSelector.of(specificId)

try {
// Bind use cases to this exact camera
val camera = cameraProvider.bindToLifecycle(lifecycleOwner, selector, useCaseGroup)
} catch (e: IllegalArgumentException) {
// This occurs if the camera with the specific ID is no longer available.
}

Obtaining CameraInfo from an Identifier

To get the CameraInfo for a specific CameraIdentifier, you must use a CameraSelector to query the CameraProvider. This ensures that the camera is currently available.

val cameraProvider = ...
val identifier = ...

try {
val info = cameraProvider.getCameraInfo(CameraSelector.of(identifier))
// Use the CameraInfo object...
} catch (e: IllegalArgumentException) {
// This exception is thrown if the camera corresponding to the
// identifier is not currently available.
}

Summary

Public functions

open operator Boolean
equals(other: Any?)
open Int
Boolean
isOf(camera: Camera)

Checks if this identifier corresponds to the given Camera instance.

Boolean
isOf(cameraInfo: CameraInfo)

Checks if this identifier corresponds to the given CameraInfo instance.

open String

Public functions

equals

open operator fun equals(other: Any?): Boolean

hashCode

open fun hashCode(): Int

isOf

Added in 1.6.0-alpha01
fun isOf(camera: Camera): Boolean

Checks if this identifier corresponds to the given Camera instance.

This is a convenience method that simplifies checking if a CameraIdentifier matches a currently bound Camera object.

Usage Example:

// Instead of:
// if (identifier.equals(myCamera.cameraInfo.cameraIdentifier)) { ... }

// You can write:
if (identifier.isOf(myCamera)) { ... }

isOf

Added in 1.6.0-alpha01
fun isOf(cameraInfo: CameraInfo): Boolean

Checks if this identifier corresponds to the given CameraInfo instance.

This is a convenience method for checking against a CameraInfo object, which might be retrieved from CameraProvider.availableCameraInfos.

toString

open fun toString(): String