LifecycleCameraController


class LifecycleCameraController : CameraController


A controller that provides most of the CameraX features.

This a high level controller that provides most of the CameraX core features in a single class. It handles camera initialization, creates and configures UseCases, and bind them to a LifecycleOwner when ready. It also listens to device motion sensor and set the target rotation for the use cases.

Code sample:

    // Setup.
    CameraController controller = new LifecycleCameraController(context);
    controller.bindToLifecycle(lifecycleOwner);
    PreviewView previewView = findViewById(R.id.preview_view);
    previewView.setController(controller);

    // Use case features
    controller.takePicture(...);

    // Camera control features
    controller.setZoomRatio(.5F);

Summary

Public constructors

Public functions

Unit

Sets the LifecycleOwner to be bound with the controller.

Unit

Clears the previously set LifecycleOwner and stops the camera.

Inherited Constants

From androidx.camera.view.CameraController
const Int

This property is deprecated.

Use COORDINATE_SYSTEM_VIEW_REFERENCED instead.

const Int

Bitmask option to enable ImageAnalysis.

const Int

Bitmask option to enable ImageCapture.

const Int

The previous tap-to-focus action was failed to complete.

const Int

The previous tap-to-focus action was completed successfully and the camera is focused.

const Int

The previous tap-to-focus action was completed successfully but the camera is still unfocused, similar to the CONTROL_AF_STATE_NOT_FOCUSED_LOCKED state.

const Int

No tap-to-focus action has been started by the end user.

const Int

A tap-to-focus action has started but not completed.

const Int

Bitmask option to enable video capture use case.

Inherited functions

From androidx.camera.view.CameraController
Unit

Removes all effects.

Unit

Removes a previously set analyzer.

ListenableFuture<Void!>

Enable the torch or disable the torch.

CameraControl?

Gets the CameraControl of the currently attached camera.

CameraInfo?

Gets the CameraInfo of the currently attached camera.

CameraSelector

Gets the CameraSelector.

Executor?

Gets the default executor for ImageAnalysis background tasks.

Int

Returns the mode with which images are acquired.

Int

Gets the image queue depth of ImageAnalysis.

Int

Gets the output image format for ImageAnalysis.

ResolutionSelector?

Returns the ResolutionSelector for ImageAnalysis.

CameraController.OutputSize?

This function is deprecated.

Use getImageAnalysisResolutionSelector instead.

Int

Gets the flash mode for ImageCapture.

Executor?

Gets the default executor for ImageCapture IO tasks.

Int

Returns the image capture mode.

ResolutionSelector?

Returns the ResolutionSelector for ImageCapture.

CameraController.OutputSize?

This function is deprecated.

Use getImageCaptureResolutionSelector instead.

ListenableFuture<Void!>

Gets a ListenableFuture that completes when camera initialization completes.

ResolutionSelector?

Returns the ResolutionSelector for Preview.

CameraController.OutputSize?

This function is deprecated.

Use getPreviewResolutionSelector instead.

LiveData<Int!>

Returns a LiveData with the latest tap-to-focus state.

LiveData<Int!>

Returns a LiveData of current TorchState.

DynamicRange

Gets the DynamicRange for video capture.

Int

Gets the mirror mode for video capture.

QualitySelector

Returns the QualitySelector for VIDEO_CAPTURE.

Range<Int!>

Gets the target frame rate in frames per second for video capture.

LiveData<ZoomState!>

Returns a LiveData of ZoomState.

Boolean

Checks if the given CameraSelector can be resolved to a camera.

Boolean

Checks if ImageAnalysis is enabled.

Boolean

Checks if ImageCapture is enabled.

Boolean

Returns whether pinch-to-zoom is enabled.

Boolean

Returns whether there is an in-progress video recording.

Boolean

Returns whether tap-to-focus is enabled.

Boolean

Checks if video capture is enabled.

Unit

Sets the CameraSelector.

Unit

Sets CameraEffect.

Unit
@MainThread
setEnabledUseCases(enabledUseCases: Int)

Enables or disables use cases.

Unit

Sets an analyzer to receive and analyze images.

Unit

Sets the executor that will be used for ImageAnalysis background tasks.

Unit

Sets the backpressure strategy to apply to the image producer to deal with scenarios where images may be produced faster than they can be analyzed.

Unit

Sets the image queue depth of ImageAnalysis.

Unit
@MainThread
setImageAnalysisOutputImageFormat(imageAnalysisOutputImageFormat: Int)

Sets the output image format for ImageAnalysis.

Unit

Sets the ResolutionSelector for ImageAnalysis.

Unit

This function is deprecated.

Use setImageAnalysisResolutionSelector instead.

Unit

Sets the flash mode for ImageCapture.

Unit

Sets the default executor that will be used for ImageCapture IO tasks.

Unit

Sets the image capture mode.

Unit

Sets the ResolutionSelector for ImageCapture.

Unit

This function is deprecated.

Use setImageCaptureResolutionSelector instead.

ListenableFuture<Void!>
@MainThread
setLinearZoom(linearZoom: @FloatRange(from = 0.0, to = 1.0) Float)

Sets current zoom by a linear zoom value ranging from 0f to 1.0f.

Unit

Enables/disables pinch-to-zoom.

Unit

Sets the ResolutionSelector for Preview.

Unit

This function is deprecated.

Use setPreviewResolutionSelector instead.

Unit

Enables/disables tap-to-focus.

Unit

Sets the DynamicRange for video capture.

Unit

Sets the mirror mode for video capture.

Unit

Sets the QualitySelector for VIDEO_CAPTURE.

Unit

Sets the target frame rate range in frames per second for video capture.

ListenableFuture<Void!>

Sets current zoom by ratio.

Recording
@RequiresApi(value = 26)
@MainThread
startRecording(
    outputOptions: FileDescriptorOutputOptions,
    audioConfig: AudioConfig,
    executor: Executor,
    listener: Consumer<VideoRecordEvent!>
)

Takes a video to a given file descriptor.

Recording
@MainThread
startRecording(
    outputOptions: FileOutputOptions,
    audioConfig: AudioConfig,
    executor: Executor,
    listener: Consumer<VideoRecordEvent!>
)

Takes a video to a given file.

Recording
@MainThread
startRecording(
    outputOptions: MediaStoreOutputOptions,
    audioConfig: AudioConfig,
    executor: Executor,
    listener: Consumer<VideoRecordEvent!>
)

Takes a video to MediaStore.

Unit

Captures a new still image for in memory access.

Unit
@MainThread
takePicture(
    outputFileOptions: ImageCapture.OutputFileOptions,
    executor: Executor,
    imageSavedCallback: ImageCapture.OnImageSavedCallback
)

Captures a new still image and saves to a file along with application specified metadata.

Public constructors

LifecycleCameraController

Added in 1.1.0
LifecycleCameraController(context: Context)

Public functions

bindToLifecycle

Added in 1.1.0
@MainThread
fun bindToLifecycle(lifecycleOwner: LifecycleOwner): Unit

Sets the LifecycleOwner to be bound with the controller.

The state of the lifecycle will determine when the cameras are open, started, stopped and closed. When the LifecycleOwner's state is start or greater, the controller receives camera data. It stops once the LifecycleOwner is destroyed.

Throws
java.lang.IllegalStateException

If the provided camera selector is unable to resolve a camera to be used for the given use cases.

unbind

Added in 1.1.0
@MainThread
fun unbind(): Unit

Clears the previously set LifecycleOwner and stops the camera.

See also
unbindAll