ProcessCameraProvider

public final class ProcessCameraProvider
extends Object implements CameraProvider

java.lang.Object
   ↳ androidx.camera.lifecycle.ProcessCameraProvider


A singleton which can be used to bind the lifecycle of cameras to any LifecycleOwner within an application's process.

Only a single process camera provider can exist within a process, and it can be retrieved with getInstance(Context).

Heavyweight resources, such as open and running camera devices, will be scoped to the lifecycle provided to bindToLifecycle(LifecycleOwner, CameraSelector, UseCase). Other lightweight resources, such as static camera characteristics, may be retrieved and cached upon first retrieval of this provider with getInstance(Context), and will persist for the lifetime of the process.

This is the standard provider for applications to use.

Summary

Public methods

Camera bindToLifecycle(LifecycleOwner lifecycleOwner, CameraSelector cameraSelector, UseCase... useCases)

Binds the collection of UseCase to a LifecycleOwner.

Camera bindToLifecycle(LifecycleOwner lifecycleOwner, CameraSelector cameraSelector, UseCaseGroup useCaseGroup)

Binds a UseCaseGroup to a LifecycleOwner.

static void configureInstance(CameraXConfig cameraXConfig)

Perform one-time configuration of the ProcessCameraProvider singleton with the given CameraXConfig.

List<CameraInfo> getAvailableCameraInfos()

Returns CameraInfo instances of the available cameras.

static ListenableFuture<ProcessCameraProvider> getInstance(Context context)

Retrieves the ProcessCameraProvider associated with the current process.

boolean hasCamera(CameraSelector cameraSelector)

Checks whether this provider supports at least one camera that meets the requirements from a CameraSelector.

boolean isBound(UseCase useCase)

Returns true if the UseCase is bound to a lifecycle.

void unbind(UseCase... useCases)

Unbinds all specified use cases from the lifecycle.

void unbindAll()

Unbinds all use cases from the lifecycle and removes them from CameraX.

Inherited methods

Public methods

bindToLifecycle

public Camera bindToLifecycle (LifecycleOwner lifecycleOwner, 
                CameraSelector cameraSelector, 
                UseCase... useCases)

Binds the collection of UseCase to a LifecycleOwner.

The state of the lifecycle will determine when the cameras are open, started, stopped and closed. When started, the use cases receive camera data.

Binding to a lifecycleOwner in state currently in Lifecycle.State.STARTED or greater will also initialize and start data capture. If the camera was already running this may cause a new initialization to occur temporarily stopping data from the camera before restarting it.

Multiple use cases can be bound via adding them all to a single bindToLifecycle call, or by using multiple bindToLifecycle calls. Using a single call that includes all the use cases helps to set up a camera session correctly for all uses cases, such as by allowing determination of resolutions depending on all the use cases bound being bound. If the use cases are bound separately, it will find the supported resolution with the priority depending on the binding sequence. If the use cases are bound with a single call, it will find the supported resolution with the priority in sequence of ImageCapture, Preview and then ImageAnalysis. The resolutions that can be supported depends on the camera device hardware level that there are some default guaranteed resolutions listed in CameraDevice.createCaptureSession(List, android.hardware.camera2.CameraCaptureSession.StateCallback, Handler).

Currently up to 3 use cases may be bound to a Lifecycle at any time. Exceeding capability of target camera device will throw an IllegalArgumentException.

A UseCase should only be bound to a single lifecycle and camera selector a time. Attempting to bind a use case to a lifecycle when it is already bound to another lifecycle is an error, and the use case binding will not change. Attempting to bind the same use case to multiple camera selectors is also an error and will not change the binding.

If different use cases are bound to different camera selectors that resolve to distinct cameras, but the same lifecycle, only one of the cameras will operate at a time. The non-operating camera will not become active until it is the only camera with use cases bound.

The Camera returned is determined by the given camera selector, plus other internal requirements, possibly from use case configurations. The camera returned from bindToLifecycle may differ from the camera determined solely by a camera selector. If the camera selector can't resolve a valid camera under the requirements, an IllegalArgumentException will be thrown.

Only