LifecycleCameraController


public final class LifecycleCameraController extends 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 methods

void

Sets the LifecycleOwner to be bound with the controller.

void

Clears the previously set LifecycleOwner and stops the camera.

Inherited Constants

From androidx.camera.view.CameraController
static final int

This field is deprecated.

Use COORDINATE_SYSTEM_VIEW_REFERENCED instead.

static final int

Bitmask option to enable ImageAnalysis.

static final int

Bitmask option to enable ImageCapture.

static final int

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

static final int

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

static final 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.

static final int

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

static final int

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

static final int

Bitmask option to enable video capture use case.

Inherited methods

From androidx.camera.view.CameraController
void

Removes all effects.

void

Removes a previously set analyzer.

@NonNull ListenableFuture<Void>
@MainThread
enableTorch(boolean torchEnabled)

Enable the torch or disable the torch.

@Nullable CameraControl

Gets the CameraControl of the currently attached camera.

@Nullable CameraInfo

Gets the CameraInfo of the currently attached camera.

@NonNull CameraSelector

Gets the CameraSelector.

@Nullable 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.

@Nullable ResolutionSelector

Returns the ResolutionSelector for ImageAnalysis.

@Nullable CameraController.OutputSize

This method is deprecated.

Use getImageAnalysisResolutionSelector instead.

int

Gets the flash mode for ImageCapture.

@Nullable Executor

Gets the default executor for ImageCapture IO tasks.

int

Returns the image capture mode.

@Nullable ResolutionSelector

Returns the ResolutionSelector for ImageCapture.

@Nullable CameraController.OutputSize

This method is deprecated.

Use getImageCaptureResolutionSelector instead.

@NonNull ListenableFuture<Void>

Gets a ListenableFuture that completes when camera initialization completes.

@Nullable ResolutionSelector

Returns the ResolutionSelector for Preview.

@Nullable CameraController.OutputSize

This method is deprecated.

Use getPreviewResolutionSelector instead.

@NonNull LiveData<Integer>

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

@NonNull LiveData<Integer>

Returns a LiveData of current TorchState.

@NonNull DynamicRange

Gets the DynamicRange for video capture.

int

Gets the mirror mode for video capture.

@NonNull QualitySelector

Returns the QualitySelector for VIDEO_CAPTURE.

@NonNull Range<Integer>

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

@NonNull 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.

void

Sets the CameraSelector.

void

Sets CameraEffect.

void
@MainThread
setEnabledUseCases(int enabledUseCases)

Enables or disables use cases.

void

Sets an analyzer to receive and analyze images.

void

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

void

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.

void

Sets the image queue depth of ImageAnalysis.

void
@MainThread
setImageAnalysisOutputImageFormat(int imageAnalysisOutputImageFormat)

Sets the output image format for ImageAnalysis.

void

Sets the ResolutionSelector for ImageAnalysis.

void

This method is deprecated.

Use setImageAnalysisResolutionSelector instead.

void

Sets the flash mode for ImageCapture.

void

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

void

Sets the image capture mode.

void

Sets the ResolutionSelector for ImageCapture.

void

This method is deprecated.

Use setImageCaptureResolutionSelector instead.

@NonNull ListenableFuture<Void>
@MainThread
setLinearZoom(@FloatRange(from = 0.0, to = 1.0) float linearZoom)

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

void

Enables/disables pinch-to-zoom.

void

Sets the ResolutionSelector for Preview.

void

This method is deprecated.

Use setPreviewResolutionSelector instead.

void

Enables/disables tap-to-focus.

void

Sets the DynamicRange for video capture.

void

Sets the mirror mode for video capture.

void

Sets the QualitySelector for VIDEO_CAPTURE.

void

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

@NonNull ListenableFuture<Void>
@MainThread
setZoomRatio(float zoomRatio)

Sets current zoom by ratio.

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

Takes a video to a given file descriptor.

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

Takes a video to a given file.

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

Takes a video to MediaStore.

void

Captures a new still image for in memory access.

void
@MainThread
takePicture(
    @NonNull ImageCapture.OutputFileOptions outputFileOptions,
    @NonNull Executor executor,
    @NonNull ImageCapture.OnImageSavedCallback imageSavedCallback
)

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

Public constructors

LifecycleCameraController

Added in 1.1.0
public LifecycleCameraController(@NonNull Context context)

Public methods

bindToLifecycle

Added in 1.1.0
@MainThread
public void bindToLifecycle(@NonNull LifecycleOwner lifecycleOwner)

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
public void unbind()

Clears the previously set LifecycleOwner and stops the camera.

See also
unbindAll