CameraProvider
interface CameraProvider
LifecycleCameraProvider |
Provides access to a camera which has its opening and closing controlled by a |
ProcessCameraProvider |
A singleton which can be used to bind the lifecycle of cameras to any |
A CameraProvider provides basic access to a set of cameras such as querying for camera existence or information.
A device might have multiple cameras. According to the applications' design, they might need to search for a suitable camera which supports their functions. A CameraProvider allows the applications to check whether any camera exists to fulfill the requirements or to get CameraInfo instances of all cameras to retrieve the camera information.
Summary
Public functions |
|
|---|---|
open Unit |
addCameraPresenceListener(Adds a listener for changes in camera presence. |
open CameraInfo |
getCameraInfo(cameraSelector: CameraSelector)Returns the |
open CameraInfo |
getCameraInfo(Returns the |
Boolean |
hasCamera(cameraSelector: CameraSelector)Checks whether this provider supports at least one camera that meets the requirements from a |
open Unit |
Removes a previously registered camera presence listener. |
Public properties |
|
|---|---|
List<CameraInfo> |
The |
Public functions
addCameraPresenceListener
open fun addCameraPresenceListener(
executor: Executor,
listener: CameraPresenceListener
): Unit
Adds a listener for changes in camera presence.
The listener will be notified when cameras are added to or removed from the set of devices that can be used by CameraX. This list of "usable" cameras has already been processed by any configured CameraSelector limiters and compatibility filters.
Important Note on Synchronization: To prevent race conditions, this method immediately invokes CameraPresenceListener.onCamerasAdded once on the provided executor with a Set containing all cameras that are currently available. This guarantees that the listener's state is synchronized with the provider's state at the moment of registration.
This listener reports on persistent hardware changes and does not fire for temporary, recoverable errors, such as when a camera is in use by another application.
| Parameters | |
|---|---|
executor: Executor |
The |
listener: CameraPresenceListener |
The listener to be added. |
getCameraInfo
open fun getCameraInfo(cameraSelector: CameraSelector): CameraInfo
Returns the CameraInfo instance of the camera resulted from the specified CameraSelector.
The returned CameraInfo corresponds to the camera that will be bound when calling bindToLifecycle with the specified CameraSelector.
| Parameters | |
|---|---|
cameraSelector: CameraSelector |
the |
| Returns | |
|---|---|
CameraInfo |
the corresponding |
| Throws | |
|---|---|
IllegalArgumentException |
if the given |
getCameraInfo
open fun getCameraInfo(
cameraSelector: CameraSelector,
sessionConfig: SessionConfig
): CameraInfo
Returns the CameraInfo instance of the camera resulted from the specified CameraSelector and SessionConfig.
The returned CameraInfo corresponds to the camera that will be bound when calling bindToLifecycle with the specified CameraSelector and SessionConfig.
This method is used for the case when the input SessionConfig can affect the camera selection or the information provided by the obtained CameraInfo object. For example, the androidx.camera.extensions.ExtensionSessionConfig.
| Parameters | |
|---|---|
cameraSelector: CameraSelector |
the |
sessionConfig: SessionConfig |
the |
| Returns | |
|---|---|
CameraInfo |
the corresponding |
| Throws | |
|---|---|
IllegalArgumentException |
if the given |
hasCamera
fun hasCamera(cameraSelector: CameraSelector): Boolean
Checks whether this provider supports at least one camera that meets the requirements from a CameraSelector.
If this method returns true, then the camera selector can be used to bind use cases and retrieve a Camera instance.
| Parameters | |
|---|---|
cameraSelector: CameraSelector |
the |
| Returns | |
|---|---|
Boolean |
|
| Throws | |
|---|---|
CameraInfoUnavailableException |
if unable to access cameras, perhaps due to insufficient permissions. |
removeCameraPresenceListener
open fun removeCameraPresenceListener(listener: CameraPresenceListener): Unit
Removes a previously registered camera presence listener.
Once removed, the listener will no longer receive updates. If the listener was not previously registered, this method is a no-op.
| Parameters | |
|---|---|
listener: CameraPresenceListener |
The same listener instance that was passed to |
Public properties
availableCameraInfos
val availableCameraInfos: List<CameraInfo>
The CameraInfo instances of the available cameras.
While iterating through all the available CameraInfo, if one of them meets some predefined requirements, a CameraSelector that uniquely identifies its camera can be retrieved using CameraInfo.getCameraSelector, which can then be used to bind use cases to that camera.