Stay organized with collections Save and categorize content based on your preferences.

CameraController

@RequiresApi(value = 21)
public abstract class CameraController

Known direct subclasses
LifecycleCameraController

A controller that provides most of the CameraX features.


The abstract base camera controller class.

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. It also listens to device motion sensor and set the target rotation for the use cases.

The controller is required to be used with a PreviewView. PreviewView provides the UI elements to display camera preview. The layout of the PreviewView is used to set the crop rect so the output from other use cases matches the preview display in a WYSIWYG way. The controller also listens to PreviewView's touch events to handle tap-to-focus and pinch-to-zoom features.

This class provides features of 4 UseCases: Preview, ImageCapture, ImageAnalysis and an experimental video capture. Preview is required and always enabled. ImageCapture and ImageAnalysis are enabled by default. The video capture feature is experimental. It's disabled by default because it might conflict with other use cases, especially on lower end devices. It might be necessary to disable ImageCapture and/or ImageAnalysis before the video capture feature can be enabled. Disabling/enabling UseCases freezes the preview for a short period of time. To avoid the glitch, the UseCases need to be enabled/disabled before the controller is set on PreviewView.

Summary

Nested types

@RequiresApi(value = 21)CameraController.OutputSize

Represents the output size of a UseCase.

Constants

static final int

ImageAnalysis.Analyzer option for returning PreviewView coordinates.

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.

Public constructors

Public methods

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.

@Nullable CameraController.OutputSize

Returns the intended output size for ImageAnalysis set by setImageAnalysisTargetSize, or null if not set.

int

Gets the flash mode for ImageCapture.

@Nullable Executor

Gets the default executor for ImageCapture IO tasks.

int

Returns the image capture mode.

@Nullable CameraController.OutputSize

Returns the intended output size for ImageCapture set by setImageCaptureTargetSize, or null if not set.

@NonNull ListenableFuture<Void>

Gets a ListenableFuture that completes when camera initialization completes.

@Nullable CameraController.OutputSize

Returns the intended output size for Preview set by setPreviewTargetSize, or null if not set.

@NonNull LiveData<Integer>

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

@NonNull LiveData<Integer>

Returns a LiveData of current TorchState.

@Nullable CameraController.OutputSize

Returns the intended output size for VideoCapture set by setVideoCaptureTargetSize, or null if not set.

@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 a in progress video recording.

boolean

Returns whether tap-to-focus is enabled.

boolean

Checks if video capture is enabled.

void

Sets the CameraSelector.

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

Sets the intended output size for ImageAnalysis.

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 intended image size for ImageCapture.

@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 intended output size for Preview.

void

Enables/disables tap-to-focus.

void

Sets the intended video size for VideoCapture.

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

Sets current zoom by ratio.

void
@ExperimentalVideo
@MainThread
startRecording(
    @NonNull OutputFileOptions outputFileOptions,
    @NonNull Executor executor,
    @NonNull OnVideoSavedCallback callback
)

Takes a video and calls the OnVideoSavedCallback when done.

void

Stops a in progress video recording.

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.

void

Captures a new still image for in memory access.

Constants

COORDINATE_SYSTEM_VIEW_REFERENCED

public static final int COORDINATE_SYSTEM_VIEW_REFERENCED = 1

ImageAnalysis.Analyzer option for returning PreviewView coordinates.

When the ImageAnalysis.Analyzer is configured with this option, it will receive a Matrix that will receive a value that represents the transformation from camera sensor to the PreviewView, which can be used for highlighting detected result in PreviewView. For example, laying over a bounding box on top of the detected face.

Note this option only works if the ImageAnalysis.Analyzer is set via setImageAnalysisAnalyzer. It will not be effective when used with camera-core directly.

IMAGE_ANALYSIS

public static final int IMAGE_ANALYSIS = 2

Bitmask option to enable ImageAnalysis. In setEnabledUseCases, if (enabledUseCases &IMAGE;_ANALYSIS) != 0, then controller will enable image analysis features.

IMAGE_CAPTURE

public static final int IMAGE_CAPTURE = 1

Bitmask option to enable ImageCapture. In setEnabledUseCases, if (enabledUseCases &IMAGE;_CAPTURE) != 0, then controller will enable image capture features.

TAP_TO_FOCUS_FAILED

public static final int TAP_TO_FOCUS_FAILED = 4

The previous tap-to-focus action was failed to complete. This is usually due to device limitations.

TAP_TO_FOCUS_FOCUSED

public static final int TAP_TO_FOCUS_FOCUSED = 2

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

TAP_TO_FOCUS_NOT_FOCUSED

public static final int TAP_TO_FOCUS_NOT_FOCUSED = 3

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. The end user might be able to get a better result by trying again with different camera distances and/or lighting.

TAP_TO_FOCUS_NOT_STARTED

public static final int TAP_TO_FOCUS_NOT_STARTED = 0

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

TAP_TO_FOCUS_STARTED

public static final int TAP_TO_FOCUS_STARTED = 1

A tap-to-focus action has started but not completed. The app also gets notified with this state if a new action happens before the previous one could finish.

VIDEO_CAPTURE

@ExperimentalVideo
public static final int VIDEO_CAPTURE = 4

Bitmask option to enable video capture use case. In setEnabledUseCases, if (enabledUseCases &VIDEO;_CAPTURE) != 0, then controller will enable video capture features.

Public constructors

CameraController

public final CameraController()

Public methods

clearImageAnalysisAnalyzer

@MainThread
public void clearImageAnalysisAnalyzer()

Removes a previously set analyzer.

This will stop data from streaming to the ImageAnalysis.

If the current getDefaultTargetResolution returns non-null value, calling this method will reconfigure the camera which might cause additional latency. To avoid this, call this method when the lifecycle is not active.

See also
clearAnalyzer

enableTorch

@MainThread
public @NonNull ListenableFuture<VoidenableTorch(boolean torchEnabled)

Enable the torch or disable the torch.

No-ops if the camera is not ready. The ListenableFuture completes successfully in this case.

Parameters
boolean torchEnabled

true to turn on the torch, false to turn it off.

Returns
@NonNull ListenableFuture<Void>

A ListenableFuture which is successful when the torch was changed to the value specified. It fails when it is unable to change the torch state. Cancellation of this future is a no-op.

See also
enableTorch

getCameraControl

@MainThread
public @Nullable CameraControl getCameraControl()

Gets the CameraControl of the currently attached camera.

For controls available directly through CameraController as well as CameraControl, it's recommended to use the ones with CameraController, e.g. setLinearZoom v.s. setLinearZoom. CameraControl is a lower-layer API and may require more steps to achieve the same effect, and will not maintain control values when switching between cameras.

Returns
@Nullable CameraControl

the CameraControl of the current camera. Returns null if camera is not ready.

See also
getCameraControl

getCameraInfo

@MainThread
public @Nullable CameraInfo getCameraInfo()

Gets the CameraInfo of the currently attached camera.

For info available directly through CameraController as well as CameraInfo, it's recommended to use the ones with CameraController, e.g. getTorchState v.s. getTorchState. CameraInfo is a lower-layer API and may require more steps to achieve the same effect, and will not maintain values when switching between cameras.

Returns
@Nullable CameraInfo

the CameraInfo of the current camera. Returns null if camera is not ready.

See also
getCameraInfo

getCameraSelector

@MainThread
public @NonNull CameraSelector getCameraSelector()

Gets the CameraSelector.

The default value isDEFAULT_BACK_CAMERA.

See also
CameraSelector

getImageAnalysisBackgroundExecutor

@MainThread
public @Nullable Executor getImageAnalysisBackgroundExecutor()

Gets the default executor for ImageAnalysis background tasks.

getImageAnalysisBackpressureStrategy

@MainThread
public int getImageAnalysisBackpressureStrategy()

Returns the mode with which images are acquired.

If not set, it defaults to STRATEGY_KEEP_ONLY_LATEST.

Returns
int

The backpressure strategy applied to the image producer.

getImageAnalysisImageQueueDepth

@MainThread
public int getImageAnalysisImageQueueDepth()

Gets the image queue depth of ImageAnalysis.

getImageAnalysisTargetSize

@MainThread
public @Nullable CameraController.OutputSize getImageAnalysisTargetSize()

Returns the intended output size for ImageAnalysis set by setImageAnalysisTargetSize, or null if not set.

getImageCaptureFlashMode

@MainThread
public int getImageCaptureFlashMode()

Gets the flash mode for ImageCapture.

Returns
int

the flashMode. Value is FLASH_MODE_AUTO, FLASH_MODE_ON, or FLASH_MODE_OFF.

See also
ImageCapture

getImageCaptureIoExecutor

@MainThread
public @Nullable Executor getImageCaptureIoExecutor()

Gets the default executor for ImageCapture IO tasks.

getImageCaptureMode

@MainThread
public int getImageCaptureMode()

Returns the image capture mode.

See also
getCaptureMode

getImageCaptureTargetSize

@MainThread
public @Nullable CameraController.OutputSize getImageCaptureTargetSize()

Returns the intended output size for ImageCapture set by setImageCaptureTargetSize, or null if not set.

getInitializationFuture

public @NonNull ListenableFuture<VoidgetInitializationFuture()

Gets a ListenableFuture that completes when camera initialization completes.

This future may fail with an InitializationException and associated cause that can be retrieved by getCause. The cause will be a CameraUnavailableException if it fails to access any camera during initialization.

In the rare case that the future fails with CameraUnavailableException, the camera will become unusable. This could happen for various reasons, for example hardware failure or the camera being held by another process. If the failure is temporary, killing and restarting the app might fix the issue.

The initialization also try to bind use cases before completing the ListenableFuture. The ListenableFuture will complete successfully regardless of whether the use cases are ready to be bound, e.g. it will complete successfully even if the controller is not set on a PreviewView. However the ListenableFuture will fail if the enabled use cases are not supported by the current camera.

See also
getInstance

getPreviewTargetSize

@MainThread
public @Nullable CameraController.OutputSize getPreviewTargetSize()

Returns the intended output size for Preview set by setPreviewTargetSize, or null if not set.

getTapToFocusState

@MainThread
public @NonNull LiveData<IntegergetTapToFocusState()

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

When tap-to-focus feature is enabled, the LiveData will receive updates of focusing states. This happens when the end user taps on PreviewView, and then again when focusing is finished either successfully or unsuccessfully. The following table displays the states the LiveData can be in, and the possible transitions between them.

State Transition cause New State
TAP_TO_FOCUS_NOT_STARTED User taps on PreviewView TAP_TO_FOCUS_STARTED
TAP_TO_FOCUS_SUCCESSFUL User taps on PreviewView TAP_TO_FOCUS_STARTED
TAP_TO_FOCUS_UNSUCCESSFUL User taps on PreviewView TAP_TO_FOCUS_STARTED
TAP_TO_FOCUS_FAILED User taps on PreviewView TAP_TO_FOCUS_STARTED
TAP_TO_FOCUS_STARTED Focusing succeeded TAP_TO_FOCUS_SUCCESSFUL
Focusing failed due to lighting and/or camera distance TAP_TO_FOCUS_UNSUCCESSFUL
Focusing failed due to device constraints TAP_TO_FOCUS_FAILED

getTorchState

@MainThread
public @NonNull LiveData<IntegergetTorchState()

Returns a LiveData of current TorchState.

The torch can be turned on and off via enableTorch which will trigger the change event to the returned LiveData.

Returns
@NonNull LiveData<Integer>

a LiveData containing current torch state.

See also
getTorchState

getVideoCaptureTargetSize

@ExperimentalVideo
@MainThread
public @Nullable CameraController.OutputSize getVideoCaptureTargetSize()

Returns the intended output size for VideoCapture set by setVideoCaptureTargetSize, or null if not set.

getZoomState

@MainThread
public @NonNull LiveData<ZoomStategetZoomState()

Returns a LiveData of ZoomState.

The LiveData will be updated whenever the set zoom state has been changed. This can occur when the application updates the zoom via setZoomRatio or setLinearZoom. The zoom state can also change anytime a camera starts up, for example when setCameraSelector is called.

See also
getZoomState

hasCamera

@MainThread
public boolean hasCamera(@NonNull CameraSelector cameraSelector)

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

Use this method to check if the device has the given camera.

Only call this method after camera is initialized. e.g. after the ListenableFuture from getInitializationFuture is finished. Calling it prematurely throws IllegalStateException. Example:

controller.getInitializationFuture().addListener(() -> {
    if (controller.hasCamera(cameraSelector)) {
        controller.setCameraSelector(cameraSelector);
    } else {
        // Update UI if the camera is not available.
    }
    // Attach PreviewView after we know the camera is available.
    previewView.setController(controller);
}, ContextCompat.getMainExecutor(requireContext()));
Returns
boolean

true if the CameraSelector can be resolved to a camera.

Throws
java.lang.IllegalStateException java.lang.IllegalStateException

if the camera is not initialized.

isImageAnalysisEnabled

@MainThread
public boolean isImageAnalysisEnabled()

Checks if ImageAnalysis is enabled.

See also
ImageAnalysis

isImageCaptureEnabled

@MainThread
public boolean isImageCaptureEnabled()

Checks if ImageCapture is enabled.

ImageCapture is enabled by default. It has to be enabled before takePicture can be called.

See also
ImageCapture

isPinchToZoomEnabled

@MainThread
public boolean isPinchToZoomEnabled()

Returns whether pinch-to-zoom is enabled.

By default pinch-to-zoom is enabled.

Returns
boolean

True if pinch-to-zoom is enabled.

isRecording

@ExperimentalVideo
@MainThread
public boolean isRecording()

Returns whether there is a in progress video recording.

isTapToFocusEnabled

@MainThread
public boolean isTapToFocusEnabled()

Returns whether tap-to-focus is enabled.

By default tap-to-focus is enabled.

Returns
boolean

True if tap-to-focus is enabled.

isVideoCaptureEnabled

@ExperimentalVideo
@MainThread
public boolean isVideoCaptureEnabled()

Checks if video capture is enabled.

Video capture is disabled by default. It has to be enabled before startRecording can be called.

setCameraSelector

@MainThread
public void setCameraSelector(@NonNull CameraSelector cameraSelector)

Sets the CameraSelector.

Calling this method with a CameraSelector that resolves to a different camera will change the camera being used by the controller. If camera initialization is complete, the controller will immediately rebind use cases with the new CameraSelector; otherwise, the new CameraSelector will be used when the camera becomes ready.

The default value is DEFAULT_BACK_CAMERA.

Throws
java.lang.IllegalStateException java.lang.IllegalStateException

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

See also
CameraSelector

setEnabledUseCases

@MainThread
public void setEnabledUseCases(int enabledUseCases)

Enables or disables use cases.

Use cases need to be enabled before they can be used. By default, IMAGE_CAPTURE and IMAGE_ANALYSIS are enabled, and VIDEO_CAPTURE is disabled. This is necessary because VIDEO_CAPTURE is an experimental feature that might not work with other use cases, especially on lower end devices. When that happens, this method will fail with an IllegalStateException.

To make sure VIDEO_CAPTURE works, IMAGE_CAPTURE and IMAGE_ANALYSIS needs to be disabled when enabling VIDEO_CAPTURE. For example:

// By default, image capture is enabled. Taking picture works.
controller.takePicture(...);

// Switch to video capture to shoot video.
controller.setEnabledUseCases(VIDEO_CAPTURE);
controller.startRecording(...);
controller.stopRecording(...);

// Switch back to image capture and image analysis before taking another picture.
controller.setEnabledUseCases(IMAGE_CAPTURE|IMAGE_ANALYSIS);
controller.takePicture(...);

Parameters
int enabledUseCases

one or more of the following use cases, bitwise-OR-ed together: IMAGE_CAPTURE, IMAGE_ANALYSIS and/or VIDEO_CAPTURE.

Throws
java.lang.IllegalStateException java.lang.IllegalStateException

If the current camera selector is unable to resolve a camera to be used for the enabled use cases.

setImageAnalysisAnalyzer

@MainThread
public void setImageAnalysisAnalyzer(
    @NonNull Executor executor,
    @NonNull ImageAnalysis.Analyzer analyzer
)

Sets an analyzer to receive and analyze images.

Applications can process or copy the image by implementing the ImageAnalysis.Analyzer. The image needs to be closed by calling close when the analyzing is done.

Setting an analyzer function replaces any previous analyzer. Only one analyzer can be set at any time.

If the getDefaultTargetResolution returns a non-null value, calling this method will reconfigure the camera which might cause additional latency. To avoid this, set the value before controller is bound to the lifecycle.

Parameters
@NonNull Executor executor

The executor in which the analyze will be run.

@NonNull ImageAnalysis.Analyzer analyzer

of the images.

See also
setAnalyzer

setImageAnalysisBackgroundExecutor

@MainThread
public void setImageAnalysisBackgroundExecutor(@Nullable Executor executor)

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

If not set, the background executor will default to an automatically generated Executor.

Changing the value will reconfigure the camera, which will cause additional latency. To avoid this, set the value before controller is bound to lifecycle.

Parameters
@Nullable Executor executor

the executor for ImageAnalysis background tasks.

setImageAnalysisBackpressureStrategy

@MainThread
public void setImageAnalysisBackpressureStrategy(int strategy)

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.

The available values are STRATEGY_BLOCK_PRODUCER and STRATEGY_KEEP_ONLY_LATEST. If not set, the backpressure strategy will default to STRATEGY_KEEP_ONLY_LATEST.

Changing the value will reconfigure the camera which will cause additional latency. To avoid this, set the value before controller is bound to lifecycle.

Parameters
int strategy

The strategy to use.

setImageAnalysisImageQueueDepth

@MainThread
public void setImageAnalysisImageQueueDepth(int depth)

Sets the image queue depth of ImageAnalysis.

This sets the number of images available in parallel to ImageAnalysis.Analyzer . The value is only used if the backpressure strategy is STRATEGY_BLOCK_PRODUCER.

Changing the value will reconfigure the camera which will cause additional latency. To avoid this, set the value before controller is bound to lifecycle.

Parameters
int depth

The total number of images available.

setImageAnalysisTargetSize

@MainThread
public void setImageAnalysisTargetSize(
    @Nullable CameraController.OutputSize targetSize
)

Sets the intended output size for ImageAnalysis.

The value is used as a hint when determining the resolution and aspect ratio of the output buffer. The actual output may differ from the requested value due to device constraints.

When set to null, the output will be based on the default config of ImageAnalysis.

Changing the value will reconfigure the camera which will cause additional latency. To avoid this, set the value before controller is bound to lifecycle.

Parameters
@Nullable CameraController.OutputSize targetSize

the intended output size for ImageAnalysis.

setImageCaptureFlashMode

@MainThread
public void setImageCaptureFlashMode(int flashMode)

Sets the flash mode for ImageCapture.

If not set, the flash mode will default to FLASH_MODE_OFF.

Parameters
int flashMode

the flash mode for ImageCapture.

setImageCaptureIoExecutor

@MainThread
public void setImageCaptureIoExecutor(@Nullable Executor executor)

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

This executor will be used for any IO tasks specifically for ImageCapture, such as takePicture. If no executor is set, then a default Executor specifically for IO will be used instead.

Changing the value will reconfigure the camera which will cause additional latency. To avoid this, set the value before controller is bound to lifecycle.

Parameters
@Nullable Executor executor

The executor which will be used for IO tasks.

setImageCaptureMode

@MainThread
public void setImageCaptureMode(int captureMode)

Sets the image capture mode.

Valid capture modes are CAPTURE_MODE_MINIMIZE_LATENCY, which prioritizes latency over image quality, or CAPTURE_MODE_MAXIMIZE_QUALITY, which prioritizes image quality over latency.

Changing the value will reconfigure the camera which will cause additional latency. To avoid this, set the value before controller is bound to lifecycle.

Parameters
int captureMode

the requested image capture mode.

setImageCaptureTargetSize

@MainThread
public void setImageCaptureTargetSize(
    @Nullable CameraController.OutputSize targetSize
)

Sets the intended image size for ImageCapture.

The value is used as a hint when determining the resolution and aspect ratio of the captured image. The actual output may differ from the requested value due to device constraints.

When set to null, the output will be based on the default config of ImageCapture.

Changing the value will reconfigure the camera which will cause additional latency. To avoid this, set the value before controller is bound to lifecycle.

Parameters
@Nullable CameraController.OutputSize targetSize

the intended image size for ImageCapture.

setLinearZoom

@MainThread
public @NonNull ListenableFuture<VoidsetLinearZoom(@FloatRange(from = 0.0, to = 1.0) float linearZoom)

Sets current zoom by a linear zoom value ranging from 0f to 1.0f. LinearZoom 0f represents the minimum zoom while linearZoom 1.0f represents the maximum zoom. The advantage of linearZoom is that it ensures the field of view (FOV) varies linearly with the linearZoom value, for use with slider UI elements (while setZoomRatio works well for pinch-zoom gestures).

No-ops if the camera is not ready. The ListenableFuture completes successfully in this case.

Returns
@NonNull ListenableFuture<Void>

a ListenableFuture which is finished when camera is set to the given ratio. It fails with CameraControl.OperationCanceledException if there is newer value being set or camera is closed. If the ratio is out of range, it fails with IllegalArgumentException. Cancellation of this future is a no-op.

See also
setLinearZoom

setPinchToZoomEnabled

@MainThread
public void setPinchToZoomEnabled(boolean enabled)

Enables/disables pinch-to-zoom.

Once enabled, end user can pinch on the PreviewView to zoom in/out if the bound camera supports zooming.

Parameters
boolean enabled

True to enable pinch-to-zoom.

setPreviewTargetSize

@MainThread
public void setPreviewTargetSize(@Nullable CameraController.OutputSize targetSize)

Sets the intended output size for Preview.

The value is used as a hint when determining the resolution and aspect ratio of the preview. The actual output may differ from the requested value due to device constraints.

When set to null, the output will be based on the default config of Preview.

Changing the value will reconfigure the camera which will cause additional latency. To avoid this, set the value before controller is bound to lifecycle.

Parameters
@Nullable CameraController.OutputSize targetSize

the intended output size for Preview.

setTapToFocusEnabled

@MainThread
public void setTapToFocusEnabled(boolean enabled)

Enables/disables tap-to-focus.

Once enabled, end user can tap on the PreviewView to set focus point.

Parameters
boolean enabled

True to enable tap-to-focus.

setVideoCaptureTargetSize

@ExperimentalVideo
@MainThread
public void setVideoCaptureTargetSize(
    @Nullable CameraController.OutputSize targetSize
)

Sets the intended video size for VideoCapture.

The value is used as a hint when determining the resolution and aspect ratio of the video. The actual output may differ from the requested value due to device constraints.

When set to null, the output will be based on the default config of VideoCapture.

Changing the value will reconfigure the camera which will cause video capture to stop. To avoid this, set the value before controller is bound to lifecycle.

Parameters
@Nullable CameraController.OutputSize targetSize

the intended video size for VideoCapture.

setZoomRatio

@MainThread
public @NonNull ListenableFuture<VoidsetZoomRatio(float zoomRatio)

Sets current zoom by ratio.

Valid zoom values range from getMinZoomRatio to getMaxZoomRatio.

No-ops if the camera is not ready. The ListenableFuture completes successfully in this case.

Parameters
float zoomRatio

The requested zoom ratio.

Returns
@NonNull ListenableFuture<Void>

a ListenableFuture which is finished when camera is set to the given ratio. It fails with CameraControl.OperationCanceledException if there is newer value being set or camera is closed. If the ratio is out of range, it fails with IllegalArgumentException. Cancellation of this future is a no-op.

startRecording

@ExperimentalVideo
@MainThread
public void startRecording(
    @NonNull OutputFileOptions outputFileOptions,
    @NonNull Executor executor,
    @NonNull OnVideoSavedCallback callback
)

Takes a video and calls the OnVideoSavedCallback when done.

Parameters
@NonNull OutputFileOptions outputFileOptions

Options to store the newly captured video.

@NonNull Executor executor

The executor in which the callback methods will be run.

@NonNull OnVideoSavedCallback callback

Callback which will receive success or failure.

stopRecording

@ExperimentalVideo
@MainThread
public void stopRecording()

Stops a in progress video recording.

takePicture

@MainThread
public void 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.

The callback will be called only once for every invocation of this method.

By default, the saved image is mirrored to match the output of the preview if front camera is used. To override this behavior, the app needs to explicitly set the flag to false using setReversedHorizontal and setMetadata.

Parameters
@NonNull ImageCapture.OutputFileOptions outputFileOptions

Options to store the newly captured image.

@NonNull Executor executor

The executor in which the callback methods will be run.

@NonNull ImageCapture.OnImageSavedCallback imageSavedCallback

Callback to be called for the newly captured image.

See also
takePicture

takePicture

@MainThread
public void takePicture(
    @NonNull Executor executor,
    @NonNull ImageCapture.OnImageCapturedCallback callback
)

Captures a new still image for in memory access.

The listener is responsible for calling close on the returned image.

Parameters
@NonNull Executor executor

The executor in which the callback methods will be run.

@NonNull ImageCapture.OnImageCapturedCallback callback

Callback to be invoked for the newly captured image

See also
takePicture