CameraManagerCompat

@RequiresApi(21) class CameraManagerCompat
kotlin.Any
   ↳ androidx.camera.camera2.internal.compat.CameraManagerCompat

Helper for accessing features in CameraManager in a backwards compatible fashion.

Summary

Nested classes
abstract

Provides backwards compatibility to CameraManager features.

Public methods
static CameraManagerCompat
from(@NonNull context: Context)

Get a CameraManagerCompat instance for a provided context.

static CameraManagerCompat
from(@NonNull context: Context, @NonNull compatHandler: Handler)

Get a CameraManagerCompat instance for a provided context, and using the provided compat handler for scheduling to Executor APIs.

CameraCharacteristicsCompat

Returns a CameraCharacteristicsCompat associated with the given camera id.

Array<String!>

Return the list of currently connected camera devices by identifier, including cameras that may be in use by other camera API clients.

Unit
openCamera(@NonNull cameraId: String, @NonNull executor: Executor, @NonNull callback: StateCallback)

Open a connection to a camera with the given ID.

Unit
registerAvailabilityCallback(@NonNull executor: Executor, @NonNull callback: AvailabilityCallback)

Register a callback to be notified about camera device availability.

Unit

Remove a previously-added callback; the callback will no longer receive connection and disconnection callbacks.

CameraManager

Gets the underlying framework CameraManager object.

Public methods

from

@NonNull static fun from(@NonNull context: Context): CameraManagerCompat

Get a CameraManagerCompat instance for a provided context.

from

@NonNull static fun from(
    @NonNull context: Context,
    @NonNull compatHandler: Handler
): CameraManagerCompat

Get a CameraManagerCompat instance for a provided context, and using the provided compat handler for scheduling to Executor APIs.

Parameters
context Context: Context used to retrieve the CameraManager.
compatHandler Handler: Handler used for all APIs taking an Executor argument on lower API levels. If the API level does not support directly executing on an Executor, it will first be posted to this handler and the executor will be called from there.

getCameraCharacteristicsCompat

@NonNull fun getCameraCharacteristicsCompat(@NonNull cameraId: String): CameraCharacteristicsCompat

Returns a CameraCharacteristicsCompat associated with the given camera id.

It will return cached instance if the CameraCharacteristicsCompat has been retrieved by the same camera id. If cached instance is returned, it won't throw any CameraAccessExceptionCompat exception even when camera is disconnected.

The returned CameraCharacteristicsCompat will also cache the retrieved values to speed up the subsequent query.

Parameters
cameraId String: The id of the camera device to query. This could be either a standalone camera ID which can be directly opened by openCamera, or a physical camera ID that can only used as part of a logical multi-camera.
Return
CameraCharacteristicsCompat a CameraCharacteristicsCompat associated with the given camera id.
Exceptions
IllegalArgumentException if the cameraId does not match any known camera device.
CameraAccessExceptionCompat if the camera device has been disconnected or the device is in Do Not Disturb mode with an early version of Android P.

getCameraIdList

@NonNull fun getCameraIdList(): Array<String!>

Return the list of currently connected camera devices by identifier, including cameras that may be in use by other camera API clients.

The behavior of this method matches that of CameraManager#getCameraIdList(), except that CameraAccessExceptionCompat is thrown in place of CameraAccessException for convenience.

Return
Array<String!> The list of currently connected camera devices.

openCamera

@RequiresPermission("android.permission.CAMERA") fun openCamera(
    @NonNull cameraId: String,
    @NonNull executor: Executor,
    @NonNull callback: StateCallback
): Unit

Open a connection to a camera with the given ID.

The behavior of this method matches that of CameraManager#openCamera(String, CameraDevice.StateCallback, Handler), except that it uses Executor as an argument instead of Handler.

Parameters
cameraId String: The unique identifier of the camera device to open
executor Executor: The executor which will be used when invoking the callback.
callback StateCallback: The callback which is invoked once the camera is opened
Exceptions
CameraAccessExceptionCompat if the camera is disabled by device policy, has been disconnected, is being used by a higher-priority camera API client or the device is in Do Not Disturb mode with an early version of Android P.
IllegalArgumentException if cameraId, the callback or the executor was null, or the cameraId does not match any currently or previously available camera device.
SecurityException if the application does not have permission to access the camera

registerAvailabilityCallback

fun registerAvailabilityCallback(
    @NonNull executor: Executor,
    @NonNull callback: AvailabilityCallback
): Unit

Register a callback to be notified about camera device availability.

The behavior of this method matches that of CameraManager#registerAvailabilityCallback(CameraManager.AvailabilityCallback, Handler), except that it uses Executor as an argument instead of Handler.

When registering an availability callback with registerAvailabilityCallback(Executor, CameraManager.AvailabilityCallback), it should always be unregistered by calling unregisterAvailabilityCallback(CameraManager.AvailabilityCallback) on the same instance of CameraManagerCompat. Unregistering through a difference instance or directly through CameraManager may fail to unregister the callback with the camera service.

Parameters
executor Executor: The executor which will be used to invoke the callback.
callback AvailabilityCallback: the new callback to send camera availability notices to
Exceptions
IllegalArgumentException if the executor is null.

unregisterAvailabilityCallback

fun unregisterAvailabilityCallback(@NonNull callback: AvailabilityCallback): Unit

Remove a previously-added callback; the callback will no longer receive connection and disconnection callbacks.

All callbacks registered through an instance of CameraManagerCompat should be unregistered through the same instance, otherwise the callback may fail to unregister with the camera service.

Removing a callback that isn't registered has no effect.

Parameters
callback AvailabilityCallback: The callback to remove from the notification list

unwrap

@NonNull fun unwrap(): CameraManager

Gets the underlying framework CameraManager object.

This method can be used gain access to CameraManager methods not exposed by CameraManagerCompat.