LifecycleCameraProvider


@ExperimentalCameraProviderConfiguration
public interface LifecycleCameraProvider extends CameraProvider


Provides access to a camera which has its opening and closing controlled by a LifecycleOwner.

This allows configuring multiple instances with different Context and CameraXConfig. The use cases can be bound to different camera providers simultaneously, but only one LifecycleOwner can be active at a time.

The sample shows how to configure and create two camera providers differently.

import androidx.camera.camera2.Camera2Config
import androidx.camera.core.CameraSelector
import androidx.camera.core.CameraXConfig
import androidx.camera.lifecycle.LifecycleCameraProvider

val cameraProvider1 =
    LifecycleCameraProvider.createInstance(
        context1,
        CameraXConfig.Builder.fromConfig(Camera2Config.defaultConfig())
            .setCameraExecutor(executor1)
            .build(),
    )
cameraProvider1.bindToLifecycle(lifecycleOwner1, CameraSelector.DEFAULT_FRONT_CAMERA, useCase1)

// Switch to different lifecycle owner.

val cameraProvider2 =
    LifecycleCameraProvider.createInstance(
        context2,
        CameraXConfig.Builder.fromConfig(Camera2Config.defaultConfig())
            .setCameraExecutor(executor2)
            .build(),
    )
cameraProvider2.bindToLifecycle(lifecycleOwner2, CameraSelector.DEFAULT_BACK_CAMERA, useCase2)

Summary

Public methods

abstract @NonNull ConcurrentCamera

Binds list of SingleCameraConfigs to LifecycleOwner.

abstract @NonNull Camera
bindToLifecycle(
    @NonNull LifecycleOwner lifecycleOwner,
    @NonNull CameraSelector cameraSelector,
    @NonNull SessionConfig sessionConfig
)

Binds a SessionConfig to a LifecycleOwner.

abstract @NonNull Camera
bindToLifecycle(
    @NonNull LifecycleOwner lifecycleOwner,
    @NonNull CameraSelector cameraSelector,
    @NonNull UseCaseGroup useCaseGroup
)

Binds a UseCaseGroup to a LifecycleOwner.

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

Binds the collection of UseCase to a LifecycleOwner.

default static final @NonNull LifecycleCameraProvider
createInstance(@NonNull Context context, CameraXConfig cameraXConfig)

Creates a lifecycle camera provider instance.

default static final @NonNull ListenableFuture<@NonNull LifecycleCameraProvider>
createInstanceAsync(
    @NonNull Context context,
    CameraXConfig cameraXConfig
)

Creates a lifecycle camera provider instance asynchronously.

abstract boolean
isBound(@NonNull SessionConfig sessionConfig)

Returns true if the exact same instance of SessionConfig is bound to a lifecycle, false otherwise.

abstract boolean

Returns true if this UseCase is bound to a lifecycle or included in a bound SessionConfig, false otherwise.

abstract void
unbind(@NonNull SessionConfig sessionConfig)

Unbinds the specified SessionConfig instance from the lifecycle provider.

abstract void
unbind(UseCase... useCases)

Unbinds all specified use cases from the lifecycle provider.

abstract void

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

Inherited methods

From androidx.camera.core.CameraProvider
default void

Adds a listener for changes in camera presence.

abstract @NonNull List<@NonNull CameraInfo>

The CameraInfo instances of the available cameras.

default @NonNull CameraInfo

Returns the CameraInfo instance of the camera resulted from the specified CameraSelector.

default @NonNull CameraInfo
getCameraInfo(
    @NonNull CameraSelector cameraSelector,
    @NonNull SessionConfig sessionConfig
)

Returns the CameraInfo instance of the camera resulted from the specified CameraSelector and SessionConfig.

abstract boolean
hasCamera(@NonNull CameraSelector cameraSelector)

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

default void

Removes a previously registered camera presence listener.

Public methods

bindToLifecycle

Added in 1.5.0
abstract @NonNull ConcurrentCamera bindToLifecycle(
    @NonNull List<ConcurrentCamera.SingleCameraConfig> singleCameraConfigs
)

Binds list of SingleCameraConfigs to LifecycleOwner.

This function only supports combinations that are available via availableConcurrentCameraInfos. If the input list of SingleCameraConfigs does not match any of the supported combinations returned by availableConcurrentCameraInfos, IllegalArgumentException will be thrown. If cameras are already used by other UseCases, UnsupportedOperationException will be thrown.

To set up concurrent camera, call availableConcurrentCameraInfos to get the list of available combinations of concurrent cameras. Each sub-list contains the CameraInfos for a combination of cameras that can be operated concurrently. Each camera can have its own UseCases and LifecycleOwner. See {@docRoot}training/camerax/architecture#lifecycles

There are two modes:

  1. Non-Composition mode: These SingleCameraConfigs have different preview and video capture use cases and there is no androidx.camera.core.CompositionSettings. In this mode, these previews and video captures can stream separately. CameraX doesn't perform any composition. You can also bind an extra image capture along with the preview and the video capture use cases.

  2. Composition mode: If the concurrent cameras are binding the same instances of preview and video capture use cases, the concurrent cameras video recording is supported. The concurrent camera preview stream will be shared with video capture and record the concurrent cameras streams as a composited stream. The androidx.camera.core.CompositionSettings can be used to configure the position of each camera stream and different layouts can be built. See androidx.camera.core.CompositionSettings for more details. The composition settings can also be updated dynamically by invoking androidx.camera.core.ConcurrentCamera.setCompositionSettings. The composition mode only supports preview and video capture. ImageCapture is currently not supported. androidx.camera.core.CameraEffect can be applied on the composited stream. Howev