CameraController
abstract class CameraController
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 VideoCapture. Preview is required and always enabled. ImageCapture and ImageAnalysis are enabled by default. The video capture is disabled by default because it might affect 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.
There are two main approaches to configure the CameraController:
- Use
setEnabledUseCases: This is for basic usage scenarios. TheCameraControllerwill manage theUseCaselifecycles and configurations for you. You can enable or disable features like image capture, image analysis, and video capture as needed. - Use
setSessionConfig: This approach is for apps where the methods exposed from the CameraController can't fulfill their requirements. Apps can use this to configure the UseCases via SessionConfig and then they can access those methods via the UseCases directly. If apps need to access many advanced methods from UseCases that are not exposed from the CameraController, it is recommended to directly use UseCases, PreviewView and setViewPort to do the implementation. When using this approach, aPreviewuse case is mandatory.
Summary
Nested types |
|---|
|
This class is deprecated. Use |
Constants |
|
|---|---|
const Int |
This property is deprecated. Use |
const Int |
IMAGE_ANALYSIS = 2Bitmask option to enable |
const Int |
IMAGE_CAPTURE = 1Bitmask option to enable |
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 |
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 |
VIDEO_CAPTURE = 4Bitmask option to enable video capture use case. |
Public functions |
|
|---|---|
Unit |
Removes all effects. |
Unit |
Removes a previously set analyzer. |
ListenableFuture<Void!> |
@MainThreadEnable the torch or disable the torch. |
CameraControl? |
Gets the |
CameraInfo? |
Gets the |
CameraSelector |
Gets the |
Executor? |
Gets the default executor for |
Int |
Returns the mode with which images are acquired. |
Int |
Gets the image queue depth of |
Int |
Gets the output image format for |
ResolutionSelector? |
Returns the |
CameraController.OutputSize? |
This function is deprecated. Use |
Int |
Gets the flash mode for |
Executor? |
Gets the default executor for |
Int |
Returns the image capture mode. |
ResolutionSelector? |
Returns the |
CameraController.OutputSize? |
This function is deprecated. Use |
ListenableFuture<Void!> |
Gets a |
ResolutionSelector? |
Returns the |
CameraController.OutputSize? |
This function is deprecated. Use |
LiveData<TapToFocusInfo!> |
Returns a |
LiveData<Int!> |
This function is deprecated. Use |
LiveData<Int!> |
Returns a |
DynamicRange |
Gets the |
Int |
Gets the mirror mode for video capture. |
QualitySelector |
Returns the |
Range<Int!> |
Gets the target frame rate in frames per second for video capture. |
LiveData<ZoomState!> |
|
Boolean |
@MainThreadChecks if the given |
Boolean |
Returns whether auto-rotation is enabled. |
Boolean |
Checks if |
Boolean |
Checks if |
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 |
@MainThreadSets whether to enable auto-rotation. |
Unit |
@MainThreadSets the |
Unit |
@MainThreadSets |
Unit |
@MainThreadEnables or disables use cases. |
Unit |
@MainThreadSets an analyzer to receive and analyze images. |
Unit |
@MainThreadSets the executor that will be used for |
Unit |
@MainThreadSets 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 |
Unit |
@MainThreadSets the output image format for |
Unit |
@MainThreadSets the |
Unit |
@MainThreadThis function is deprecated. Use |
Unit |
@MainThreadSets the flash mode for |
Unit |
@MainThreadSets the default executor that will be used for |
Unit |
@MainThreadSets the image capture mode. |
Unit |
@MainThreadSets the |
Unit |
@MainThreadThis function is deprecated. Use |
ListenableFuture<Void!> |
@MainThreadSets current zoom by a linear zoom value ranging from 0f to 1.0f. |
Unit |
@MainThreadEnables/disables pinch-to-zoom. |
Unit |
@MainThreadSets the |
Unit |
@MainThreadThis function is deprecated. Use |
Unit |
@MainThreadSets the |
Unit |
@MainThreadSets the auto-cancel duration for tap-to-focus events. |
Unit |
@MainThreadEnables/disables tap-to-focus. |
Unit |
@MainThreadSets the |
Unit |
@MainThreadSets the mirror mode for video capture. |
Unit |
@MainThreadSets the |
Unit |
@MainThreadSets the target frame rate range in frames per second for video capture. |
ListenableFuture<Void!> |
@MainThreadSets current zoom by ratio. |
Recording |
@RequiresApi(value = 26)Takes a video to a given file descriptor. |
Recording |
@MainThreadTakes a video to a given file. |
Recording |
@MainThreadTakes a video to MediaStore. |
Unit |
@MainThreadCaptures a new still image for in memory access. |
Unit |
@MainThreadCaptures a new still image and saves to a file along with application specified metadata. |
Constants
const val COORDINATE_SYSTEM_VIEW_REFERENCED = 1: Int
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.
| See also | |
|---|---|
ImageAnalysis.Analyzer |
IMAGE_ANALYSIS
const val IMAGE_ANALYSIS = 2: Int
Bitmask option to enable ImageAnalysis. In setEnabledUseCases, if (enabledUseCases & IMAGE_ANALYSIS) != 0, then controller will enable image analysis features.
IMAGE_CAPTURE
const val IMAGE_CAPTURE = 1: Int
Bitmask option to enable ImageCapture. In setEnabledUseCases, if (enabledUseCases & IMAGE_CAPTURE) != 0, then controller will enable image capture features.
TAP_TO_FOCUS_FAILED
const val TAP_TO_FOCUS_FAILED = 4: Int
The previous tap-to-focus action was failed to complete. This is usually due to device limitations.
TAP_TO_FOCUS_FOCUSED
const val TAP_TO_FOCUS_FOCUSED = 2: Int
The previous tap-to-focus action was completed successfully and the camera is focused.
TAP_TO_FOCUS_NOT_FOCUSED
const val TAP_TO_FOCUS_NOT_FOCUSED = 3: 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. 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
const val TAP_TO_FOCUS_NOT_STARTED = 0: Int
No tap-to-focus action has been started by the end user.
TAP_TO_FOCUS_STARTED
const val TAP_TO_FOCUS_STARTED = 1: Int
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
const val VIDEO_CAPTURE = 4: Int
Bitmask option to enable video capture use case. In setEnabledUseCases, if (enabledUseCases & VIDEO_CAPTURE) != 0, then controller will enable video capture features.
Public functions
clearEffects
@MainThread
fun clearEffects(): Unit
Removes all effects.
Once called, CameraX will remove all the effects and rebind the UseCase.
| Throws | |
|---|---|
java.lang.IllegalStateException |
if a |
clearImageAnalysisAnalyzer
@MainThread
fun clearImageAnalysisAnalyzer(): Unit
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.
| Throws | |
|---|---|
java.lang.IllegalStateException |
if a |
| See also | |
|---|---|
clearAnalyzer |
enableTorch
@MainThread
fun enableTorch(torchEnabled: Boolean): ListenableFuture<Void!>
Enable the torch or disable the torch.
If the value is set before the camera is ready, CameraController waits for the camera to be ready and then enables the torch.
| Parameters | |
|---|---|
torchEnabled: Boolean |
|
| Returns | |
|---|---|
ListenableFuture<Void!> |
A |
| See also | |
|---|---|
enableTorch |
getCameraControl
@MainThread
fun getCameraControl(): CameraControl?
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 | |
|---|---|
CameraControl? |
The |
| See also | |
|---|---|
getCameraControl |
getCameraInfo
@MainThread
fun getCameraInfo(): CameraInfo?
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 | |
|---|---|
CameraInfo? |
The |
| See also | |
|---|---|
getCameraInfo |
getCameraSelector
@MainThread
fun getCameraSelector(): CameraSelector
Gets the CameraSelector.
The default value isDEFAULT_BACK_CAMERA.
| See also | |
|---|---|
CameraSelector |
getImageAnalysisBackgroundExecutor
@MainThread
fun getImageAnalysisBackgroundExecutor(): Executor?
Gets the default executor for ImageAnalysis background tasks.
| See also | |
|---|---|
setBackgroundExecutor |
getImageAnalysisBackpressureStrategy
@MainThread
fun getImageAnalysisBackpressureStrategy(): Int
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. |
| See also | |
|---|---|
getBackpressureStrategy |
getImageAnalysisImageQueueDepth
@MainThread
fun getImageAnalysisImageQueueDepth(): Int
Gets the image queue depth of ImageAnalysis.
| See also | |
|---|---|
getImageQueueDepth |
getImageAnalysisOutputImageFormat
@MainThread
fun getImageAnalysisOutputImageFormat(): Int
Gets the output image format for ImageAnalysis.
The returned image format can be either OUTPUT_IMAGE_FORMAT_YUV_420_888, OUTPUT_IMAGE_FORMAT_RGBA_8888 or OUTPUT_IMAGE_FORMAT_NV21.
getImageAnalysisResolutionSelector
@MainThread
fun getImageAnalysisResolutionSelector(): ResolutionSelector?
Returns the ResolutionSelector for ImageAnalysis.
This method returns the value set by setImageAnalysisResolutionSelector. It returns null if the value has not been set.
@MainThread
fungetImageAnalysisTargetSize(): CameraController.OutputSize?
Returns the intended output size for ImageAnalysis set by setImageAnalysisTargetSize, or null if not set.
getImageCaptureFlashMode
@MainThread
fun getImageCaptureFlashMode(): Int
Gets the flash mode for ImageCapture.
| Returns | |
|---|---|
Int |
the flashMode. Value is |
| See also | |
|---|---|
ImageCapture |
getImageCaptureIoExecutor
@MainThread
fun getImageCaptureIoExecutor(): Executor?
Gets the default executor for ImageCapture IO tasks.
getImageCaptureMode
@MainThread
fun getImageCaptureMode(): Int
Returns the image capture mode.
| See also | |
|---|---|
getCaptureMode |
getImageCaptureResolutionSelector
@MainThread
fun getImageCaptureResolutionSelector(): ResolutionSelector?
Returns the ResolutionSelector for ImageCapture.
This method returns the value set by setImageCaptureResolutionSelector (ResolutionSelector)}. It returns null if the value has not been set.
@MainThread
fungetImageCaptureTargetSize(): CameraController.OutputSize?
Returns the intended output size for ImageCapture set by setImageCaptureTargetSize, or null if not set.
getInitializationFuture
fun getInitializationFuture(): ListenableFuture<Void!>
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 |
getPreviewResolutionSelector
@MainThread
fun getPreviewResolutionSelector(): ResolutionSelector?
Returns the ResolutionSelector for Preview.
This method returns the value set by setPreviewResolutionSelector. It returns null if the value has not been set.
@MainThread
fungetPreviewTargetSize(): CameraController.OutputSize?
Returns the intended output size for Preview set by setPreviewTargetSize, or null if not set.
getTapToFocusInfoState
@MainThread
fun getTapToFocusInfoState(): LiveData<TapToFocusInfo!>
Returns a LiveData with a TapToFocusInfo containing the latest focus state and corresponding tap position.
When tap-to-focus feature is enabled, the LiveData will receive updates of focusing states. This usually 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_FOCUSED | User taps on PreviewView |
TAP_TO_FOCUS_STARTED |
| TAP_TO_FOCUS_NOT_FOCUSED | 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_FOCUSED |
| Focusing failed due to lighting and/or camera distance | TAP_TO_FOCUS_NOT_FOCUSED | |
| Focusing failed due to device constraints | TAP_TO_FOCUS_FAILED | |
| TAP_TO_FOCUS_FOCUSED | Auto-cancel duration elapses | TAP_TO_FOCUS_NOT_STARTED |
| TAP_TO_FOCUS_NOT_FOCUSED | Auto-cancel duration elapses | TAP_TO_FOCUS_NOT_STARTED |
| TAP_TO_FOCUS_FAILED | Auto-cancel duration elapses | TAP_TO_FOCUS_NOT_STARTED |
@MainThread
fungetTapToFocusState(): LiveData<Int!>
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_FOCUSED | User taps on PreviewView |
TAP_TO_FOCUS_STARTED |
| TAP_TO_FOCUS_NOT_FOCUSED | 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_FOCUSED |
| Focusing failed due to lighting and/or camera distance | TAP_TO_FOCUS_NOT_FOCUSED | |
| Focusing failed due to device constraints | TAP_TO_FOCUS_FAILED | |
| TAP_TO_FOCUS_FOCUSED | Auto-cancel duration elapses | TAP_TO_FOCUS_NOT_STARTED |
| TAP_TO_FOCUS_NOT_FOCUSED | Auto-cancel duration elapses | TAP_TO_FOCUS_NOT_STARTED |
| TAP_TO_FOCUS_FAILED | Auto-cancel duration elapses | TAP_TO_FOCUS_NOT_STARTED |
getTorchState
@MainThread
fun getTorchState(): LiveData<Int!>
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.
| See also | |
|---|---|
getTorchState |
getVideoCaptureDynamicRange
@MainThread
fun getVideoCaptureDynamicRange(): DynamicRange
Gets the DynamicRange for video capture.
getVideoCaptureMirrorMode
@MainThread
fun getVideoCaptureMirrorMode(): Int
Gets the mirror mode for video capture.
getVideoCaptureQualitySelector
@MainThread
fun getVideoCaptureQualitySelector(): QualitySelector
Returns the QualitySelector for VIDEO_CAPTURE.
| Returns | |
|---|---|
QualitySelector |
the |
getVideoCaptureTargetFrameRate
@MainThread
fun getVideoCaptureTargetFrameRate(): Range<Int!>
Gets the target frame rate in frames per second for video capture.
getZoomState
@MainThread
fun getZoomState(): LiveData<ZoomState!>
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
fun hasCamera(cameraSelector: CameraSelector): Boolean
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 |
|
| Throws | |
|---|---|
java.lang.IllegalStateException |
if the camera is not initialized. |
isAutoRotationEnabled
@MainThread
fun isAutoRotationEnabled(): Boolean
Returns whether auto-rotation is enabled.
| Returns | |
|---|---|
Boolean |
|
| See also | |
|---|---|
setAutoRotationEnabled |
isImageAnalysisEnabled
@MainThread
fun isImageAnalysisEnabled(): Boolean
Checks if ImageAnalysis is enabled.
| See also | |
|---|---|
ImageAnalysis |
isImageCaptureEnabled
@MainThread
fun isImageCaptureEnabled(): Boolean
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
fun isPinchToZoomEnabled(): Boolean
Returns whether pinch-to-zoom is enabled.
By default pinch-to-zoom is enabled.
| Returns | |
|---|---|
Boolean |
|
isRecording
@MainThread
fun isRecording(): Boolean
Returns whether there is an in-progress video recording.
isTapToFocusEnabled
@MainThread
fun isTapToFocusEnabled(): Boolean
Returns whether tap-to-focus is enabled.
By default tap-to-focus is enabled.
| Returns | |
|---|---|
Boolean |
|
isVideoCaptureEnabled
@MainThread
fun isVideoCaptureEnabled(): Boolean
Checks if video capture is enabled.
Video capture is disabled by default. It has to be enabled before startRecording can be called.
setAutoRotationEnabled
@MainThread
fun setAutoRotationEnabled(enabled: Boolean): Unit
Sets whether to enable auto-rotation.
When enabled, CameraController will monitor the device rotation changes and set the target rotation for non-preview use cases (e.g. image capture, video capture, and image analysis). This ensures the output images/videos are oriented correctly relative to the device.
Auto-rotation is enabled by default.
If a SessionConfig is set via setSessionConfig, the auto-rotation setting in the provided SessionConfig will be overridden by the state of this controller.
Changing the value will reconfigure the camera which will cause additional latency. To avoid this, set the value before controller is bound to the lifecycle.
| Parameters | |
|---|---|
enabled: Boolean |
|
setCameraSelector
@MainThread
fun setCameraSelector(cameraSelector: CameraSelector): Unit
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.
If a SessionConfig with advanced settings is set, it might only be supported on specific camera devices, it is recommended to check whether it can be supported via isSessionConfigSupported before switching the camera selector. It is also recommended to use setSessionConfig to switch camera with SessionConfig in one function call, in case the camera resolved by the new CameraSelector can't support current SessionConfig and then make the camera switching operation failed.
| Throws | |
|---|---|
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 |
setEffects
@MainThread
fun setEffects(effects: (Mutable)Set<CameraEffect!>): Unit
Sets CameraEffect.
Call this method to set a list of active effects. There is maximum one effect per UseCase. Adding effects with duplicate or invalid targets throws IllegalArgumentException. Once called, CameraX will rebind the UseCase with the effects applied. Effects not in the list are automatically removed.
The method throws IllegalArgumentException if the effects combination is not supported by CameraX. Please see the Javadoc of addEffect to see the supported effects combinations.
| Parameters | |
|---|---|
effects: (Mutable)Set<CameraEffect!> |
The effects applied to camera output. |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if the combination of effects is not supported by CameraX. |
java.lang.IllegalStateException |
if a |
| See also | |
|---|---|
addEffect |
setEnabledUseCases
@MainThread
fun setEnabledUseCases(enabledUseCases: Int): Unit
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 may affect the available resolutions for other use cases, especially on lower end devices.
To make sure getting the maximum resolution, only enable VIDEO_CAPTURE when shooting video. 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(...); // Switch back to image capture and image analysis before taking another picture. controller.setEnabledUseCases(IMAGE_CAPTURE|IMAGE_ANALYSIS); controller.takePicture(...);
| Parameters | |
|---|---|
enabledUseCases: Int |
one or more of the following use cases, bitwise-OR-ed together: |
| Throws | |
|---|---|
java.lang.IllegalStateException |
If the current camera selector is unable to resolve a camera to be used for the enabled use cases, or if a |
| See also | |
|---|---|
UseCase |
|
ImageCapture |
|
ImageAnalysis |
setImageAnalysisAnalyzer
@MainThread
fun setImageAnalysisAnalyzer(
executor: Executor,
analyzer: ImageAnalysis.Analyzer
): Unit
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 getTargetCoordinateSystem returns COORDINATE_SYSTEM_VIEW_REFERENCED, the analyzer will receive a transformation via updateTransform that converts coordinates from the ImageAnalysis's coordinate system to the PreviewView's coordinate system.
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 | |
|---|---|
executor: Executor |
The executor in which the |
analyzer: ImageAnalysis.Analyzer |
of the images. |
| Throws | |
|---|---|
java.lang.IllegalStateException |
if a |
| See also | |
|---|---|
setAnalyzer |
setImageAnalysisBackgroundExecutor
@MainThread
fun setImageAnalysisBackgroundExecutor(executor: Executor?): Unit
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 | |
|---|---|
executor: Executor? |
The executor for |
| Throws | |
|---|---|
java.lang.IllegalStateException |
if a |
| See also | |
|---|---|
setBackgroundExecutor |
setImageAnalysisBackpressureStrategy
@MainThread
fun setImageAnalysisBackpressureStrategy(strategy: Int): 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.
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 | |
|---|---|
strategy: Int |
The strategy to use. |
| Throws | |
|---|---|
java.lang.IllegalStateException |
if a |
| See also | |
|---|---|
setBackpressureStrategy |
setImageAnalysisImageQueueDepth
@MainThread
fun setImageAnalysisImageQueueDepth(depth: Int): Unit
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 | |
|---|---|
depth: Int |
The total number of images available. |
| Throws | |
|---|---|
java.lang.IllegalStateException |
if a |
| See also | |
|---|---|
setImageQueueDepth |
setImageAnalysisOutputImageFormat
@MainThread
fun setImageAnalysisOutputImageFormat(imageAnalysisOutputImageFormat: Int): Unit
Sets the output image format for ImageAnalysis.
The supported output image format are OUTPUT_IMAGE_FORMAT_YUV_420_888 and OUTPUT_IMAGE_FORMAT_RGBA_8888.
If not set, OUTPUT_IMAGE_FORMAT_YUV_420_888 will be used.
Requesting OUTPUT_IMAGE_FORMAT_RGBA_8888 or OUTPUT_IMAGE_FORMAT_NV21 causes extra overhead because format conversion takes time.
Changing the value will reconfigure the camera, which may cause additional latency. To avoid this, set the value before controller is bound to lifecycle. If the value is changed when the camera is active, check the getFormat value to determine when the new format takes effect.
| Throws | |
|---|---|
java.lang.IllegalStateException |
if a |
setImageAnalysisResolutionSelector
@MainThread
fun setImageAnalysisResolutionSelector(
resolutionSelector: ResolutionSelector?
): Unit
Sets the ResolutionSelector for ImageAnalysis.
CameraX uses this value as a hint to select the resolution for images. The actual output may differ from the requested value due to device constraints. When set to null, CameraX will use the default config of ImageAnalysis. ImageAnalysis has a default ResolutionStrategy with bound size as 640x480 and fallback rule of FALLBACK_RULE_CLOSEST_HIGHER_THEN_LOWER.
Changing the value will reconfigure the camera which will cause additional latency. To avoid this, set the value before controller is bound to lifecycle.
| Throws | |
|---|---|
java.lang.IllegalStateException |
if a |
| See also | |
|---|---|
setResolutionSelector |
@MainThread
funsetImageAnalysisTargetSize(targetSize: CameraController.OutputSize?): Unit
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 | |
|---|---|
targetSize: CameraController.OutputSize? |
The intended output size for |
| Throws | |
|---|---|
java.lang.IllegalStateException |
if a |
| See also | |
|---|---|
setTargetAspectRatio |
|
setTargetResolution |
setImageCaptureFlashMode
@MainThread
fun setImageCaptureFlashMode(flashMode: Int): Unit
Sets the flash mode for ImageCapture.
If not set, the flash mode will default to FLASH_MODE_OFF.
If FLASH_MODE_SCREEN is set, a valid android.view.Window instance must be set to a PreviewView or ScreenFlashView which this controller is set to. Trying to use FLASH_MODE_SCREEN with a non-front camera or without setting a non-null window will throw an exception. While switching the camera, it is the application's responsibility to change flash mode to the desired one if it leads to a no-op case (e.g. switching to rear camera while FLASH_MODE_SCREEN is still set). Otherwise, FLASH_MODE_OFF will be set.
| Parameters | |
|---|---|
flashMode: Int |
the flash mode for |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
If flash mode is invalid or |
| See also | |
|---|---|
setScreenFlashWindow |
|
setScreenFlashWindow |
setImageCaptureIoExecutor
@MainThread
fun setImageCaptureIoExecutor(executor: Executor?): Unit
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 | |
|---|---|
executor: Executor? |
The executor which will be used for IO tasks. |
| Throws | |
|---|---|
java.lang.IllegalStateException |
if a |
setImageCaptureMode
@MainThread
fun setImageCaptureMode(captureMode: Int): Unit
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 | |
|---|---|
captureMode: Int |
The requested image capture mode. |
| Throws | |
|---|---|
java.lang.IllegalStateException |
if a |
setImageCaptureResolutionSelector
@MainThread
fun setImageCaptureResolutionSelector(
resolutionSelector: ResolutionSelector?
): Unit
Sets the ResolutionSelector for ImageCapture.
CameraX uses this value as a hint to select the resolution for captured images. The actual output may differ from the requested value due to device constraints. When set to null, CameraX will use the default config of ImageCapture. The default resolution strategy for ImageCapture is HIGHEST_AVAILABLE_STRATEGY, which will select the largest available resolution to use.
Changing the value will reconfigure the camera which will cause additional latency. To avoid this, set the value before controller is bound to lifecycle.
| Throws | |
|---|---|
java.lang.IllegalStateException |
if a |
| See also | |
|---|---|
setResolutionSelector |
@MainThread
funsetImageCaptureTargetSize(targetSize: CameraController.OutputSize?): Unit
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 | |
|---|---|
targetSize: CameraController.OutputSize? |
The intended image size for |
| Throws | |
|---|---|
java.lang.IllegalStateException |
if a |
setLinearZoom
@MainThread
fun setLinearZoom(linearZoom: @FloatRange(from = 0.0, to = 1.0) Float): ListenableFuture<Void!>
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).
If the value is set before the camera is ready, CameraController waits for the camera to be ready and then sets the linear zoom.
| Returns | |
|---|---|
ListenableFuture<Void!> |
A |
| See also | |
|---|---|
setLinearZoom |
setPinchToZoomEnabled
@MainThread
fun setPinchToZoomEnabled(enabled: Boolean): Unit
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 | |
|---|---|
enabled: Boolean |
|
setPreviewResolutionSelector
@MainThread
fun setPreviewResolutionSelector(resolutionSelector: ResolutionSelector?): Unit
Sets the ResolutionSelector for Preview.
CameraX uses this value as a hint to select the resolution for preview. The actual output may differ from the requested value due to device constraints. When set to null, CameraX will use the default config of Preview. By default, the selected resolution will be limited by the PREVIEW size which is defined as the best size match to the device's screen resolution, or to 1080p (1920x1080), whichever is smaller.
Changing the value will reconfigure the camera which will cause additional latency. To avoid this, set the value before controller is bound to lifecycle.
| Throws | |
|---|---|
java.lang.IllegalStateException |
if a |
| See also | |
|---|---|
setResolutionSelector |
@MainThread
funsetPreviewTargetSize(targetSize: CameraController.OutputSize?): Unit
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 | |
|---|---|
targetSize: CameraController.OutputSize? |
the intended output size for |
| Throws | |
|---|---|
java.lang.IllegalStateException |
if a |
| See also | |
|---|---|
setTargetAspectRatio |
|
setTargetResolution |
setSessionConfig
@MainThread
fun setSessionConfig(
sessionConfig: SessionConfig,
cameraSelector: CameraSelector
): Unit
Sets the SessionConfig with the CameraSelector for the CameraController.
When a SessionConfig is provided, CameraController will connect to the UseCases in the session config. This is useful for accessing the methods that are not exposed from the CameraController directly.
The following code sample shows how to create a SessionConfig with a Preview and an ImageCapture use case and set it to the controller.
// Create a LifecycleCameraController instance. LifecycleCameraController controller = new LifecycleCameraController(context); // Create the UseCases. Preview preview = new Preview.Builder().build(); ImageCapture imageCapture = new ImageCapture.Builder().build(); // Create the SessionConfig. SessionConfig sessionConfig = new SessionConfig.Builder() .addUseCase(preview) .addUseCase(imageCapture) .build(); // Set the SessionConfig to the controller. controller.setSessionConfig(sessionConfig, CameraSelector.DEFAULT_BACK_CAMERA); // The controller can now be used with a PreviewView to display the preview. previewView.setController(controller); // Bind the controller to a LifecycleOwner. controller.bindToLifecycle(lifecycleOwner);
Requirements:
- The provided
SessionConfigmust contain aPreview. - The controller must be set on a
PreviewViewfor the preview to be displayed. ThePreviewView'sPreview.SurfaceProviderwill overwrite the one in thePreviewuse case within theSessionConfig. - If the
SessionConfigincludes aVideoCaptureuse case, it must be created with aRecorderinstance for video recording to work correctly.
Once a SessionConfig is set, most setter functions will result in an IllegalStateException because the UseCase configuration is considered immutable. The only exceptions are methods that do not modify the UseCase configuration. These methods are:
setImageCaptureFlashModesetCameraSelectorsetPinchToZoomEnabledsetTapToFocusEnabledsetTapToFocusAutoCancelDurationsetZoomRatiosetLinearZoomenableTorch
The ViewPort in the given SessionConfig will be overwritten by the value according to layout of the associated PreviewView. This is to maintain the WYSIWYG design purpose of CameraController.
For SessionConfig with advanced settings that might only be supported on specific camera devices, it is recommended to check whether it can be supported via isSessionConfigSupported before setting to the CameraController.
HighSpeedVideoSessionConfig is not supported because it is not compatible with ViewPort, which is always enabled by CameraController.
CameraController will override the autoRotationEnabled setting in the provided SessionConfig to match its current state. See setAutoRotationEnabled for more information.
| Parameters | |
|---|---|
sessionConfig: SessionConfig |
The |
cameraSelector: CameraSelector |
The |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if the provided |
setTapToFocusAutoCancelDuration
@MainThread
fun setTapToFocusAutoCancelDuration(
duration: @IntRange(from = 0) Long,
timeUnit: TimeUnit
): Unit
Sets the auto-cancel duration for tap-to-focus events.
By default, CameraX uses a value of 5 seconds.
| Parameters | |
|---|---|
duration: @IntRange(from = 0) Long |
The duration after which CameraX automatically cancels a tap-to-focus event. A value of 0 will disable the auto-cancellation behavior. |
timeUnit: TimeUnit |
The |
| See also | |
|---|---|
getTapToFocusInfoState |
setTapToFocusEnabled
@MainThread
fun setTapToFocusEnabled(enabled: Boolean): Unit
Enables/disables tap-to-focus.
Once enabled, end user can tap on the PreviewView to set focus point.
| Parameters | |
|---|---|
enabled: Boolean |
|
setVideoCaptureDynamicRange
@MainThread
fun setVideoCaptureDynamicRange(dynamicRange: DynamicRange): Unit
Sets the DynamicRange for video capture.
The dynamic range specifies how the range of colors, highlights and shadows that are captured by the video producer are displayed on a display. Some dynamic ranges will allow the video to make full use of the extended range of brightness of a display when the video is played back.
The supported dynamic ranges for video capture can be queried through the androidx.camera.video.VideoCapabilities returned by getVideoCapabilities via getSupportedDynamicRanges.
It is possible to choose a high dynamic range (HDR) with unspecified encoding by providing HDR_UNSPECIFIED_10_BIT.
If the dynamic range is not provided, the default value is SDR.
| Throws | |
|---|---|
java.lang.IllegalStateException |
if a |
| See also | |
|---|---|
setDynamicRange |
setVideoCaptureMirrorMode
@MainThread
fun setVideoCaptureMirrorMode(mirrorMode: Int): Unit
Sets the mirror mode for video capture.
Valid values include: MIRROR_MODE_OFF, MIRROR_MODE_ON and MIRROR_MODE_ON_FRONT_ONLY. If not set, it defaults to MIRROR_MODE_OFF.
| Throws | |
|---|---|
java.lang.IllegalStateException |
if a |
| See also | |
|---|---|
setMirrorMode |
setVideoCaptureQualitySelector
@MainThread
fun setVideoCaptureQualitySelector(qualitySelector: QualitySelector): Unit
Sets the QualitySelector for VIDEO_CAPTURE.
The provided quality selector is used to select the resolution of the recording depending on the resolutions supported by the camera and codec capabilities.
If no quality selector is provided, the default is DEFAULT_QUALITY_SELECTOR.
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 | |
|---|---|
qualitySelector: QualitySelector |
The quality selector for |
| Throws | |
|---|---|
java.lang.IllegalStateException |
if a |
| See also | |
|---|---|
QualitySelector |
setVideoCaptureTargetFrameRate
@MainThread
fun setVideoCaptureTargetFrameRate(targetFrameRate: Range<Int!>): Unit
Sets the target frame rate range in frames per second for video capture.
This target will be used as a part of the heuristics for the algorithm that determines the final frame rate range and resolution of all concurrently bound use cases.
It is not guaranteed that this target frame rate will be the final range, as other use cases as well as frame rate restrictions of the device may affect the outcome of the algorithm that chooses the actual frame rate.
By default, the value is FRAME_RATE_RANGE_UNSPECIFIED. For supported frame rates, see getSupportedFrameRateRanges.
| Throws | |
|---|---|
java.lang.IllegalStateException |
if a |
| See also | |
|---|---|
setTargetFrameRate |
setZoomRatio
@MainThread
fun setZoomRatio(zoomRatio: Float): ListenableFuture<Void!>
Sets current zoom by ratio.
Valid zoom values range from getMinZoomRatio to getMaxZoomRatio.
If the value is set before the camera is ready, CameraController waits for the camera to be ready and then sets the zoom ratio.
| Parameters | |
|---|---|
zoomRatio: Float |
The requested zoom ratio. |
| Returns | |
|---|---|
ListenableFuture<Void!> |
A |
| See also | |
|---|---|
getZoomState |
|
setZoomRatio |
startRecording
@RequiresApi(value = 26)
@MainThread
fun startRecording(
outputOptions: FileDescriptorOutputOptions,
audioConfig: AudioConfig,
executor: Executor,
listener: Consumer<VideoRecordEvent!>
): Recording
Takes a video to a given file descriptor.
Currently, file descriptors as output destinations are not supported on pre-Android O (API 26) devices.
Only a single recording can be active at a time, so if isRecording is true, this will throw an IllegalStateException.
Upon successfully starting the recording, a VideoRecordEvent.Start event will be the first event sent to the provided event listener.
If errors occur while starting the recording, a VideoRecordEvent.Finalize event will be the first event sent to the provided listener, and information about the error can be found in that event's getError method.
Recording with audio requires the RECORD_AUDIO permission; without it, starting a recording will fail with a SecurityException.
| Parameters | |
|---|---|
outputOptions: FileDescriptorOutputOptions |
The options to store the newly captured video. |
audioConfig: AudioConfig |
The configuration of audio. |
executor: Executor |
The executor that the event listener will be run on. |
listener: Consumer<VideoRecordEvent!> |
The event listener to handle video record events. |
| Throws | |
|---|---|
java.lang.IllegalStateException |
if there is an unfinished active recording. |
java.lang.SecurityException |
if the audio config specifies audio should be enabled but the |
startRecording
@MainThread
fun startRecording(
outputOptions: FileOutputOptions,
audioConfig: AudioConfig,
executor: Executor,
listener: Consumer<VideoRecordEvent!>
): Recording
Takes a video to a given file.
Only a single recording can be active at a time, so if isRecording is true, this will throw an IllegalStateException.
Upon successfully starting the recording, a VideoRecordEvent.Start event will be the first event sent to the provided event listener.
If errors occur while starting the recording, a VideoRecordEvent.Finalize event will be the first event sent to the provided listener, and information about the error can be found in that event's getError method.
Recording with audio requires the RECORD_AUDIO permission; without it, starting a recording will fail with a SecurityException.
| Parameters | |
|---|---|
outputOptions: FileOutputOptions |
The options to store the newly captured video. |
audioConfig: AudioConfig |
The configuration of audio. |
executor: Executor |
The executor that the event listener will be run on. |
listener: Consumer<VideoRecordEvent!> |
The event listener to handle video record events. |
| Throws | |
|---|---|
java.lang.IllegalStateException |
if there is an unfinished active recording. |
java.lang.SecurityException |
if the audio config specifies audio should be enabled but the |
startRecording
@MainThread
fun startRecording(
outputOptions: MediaStoreOutputOptions,
audioConfig: AudioConfig,
executor: Executor,
listener: Consumer<VideoRecordEvent!>
): Recording
Takes a video to MediaStore.
Only a single recording can be active at a time, so if isRecording is true, this will throw an IllegalStateException.
Upon successfully starting the recording, a VideoRecordEvent.Start event will be the first event sent to the provided event listener.
If errors occur while starting the recording, a VideoRecordEvent.Finalize event will be the first event sent to the provided listener, and information about the error can be found in that event's getError method.
Recording with audio requires the RECORD_AUDIO permission; without it, starting a recording will fail with a SecurityException.
| Parameters | |
|---|---|
outputOptions: MediaStoreOutputOptions |
The options to store the newly captured video. |
audioConfig: AudioConfig |
The configuration of audio. |
executor: Executor |
The executor that the event listener will be run on. |
listener: Consumer<VideoRecordEvent!> |
The event listener to handle video record events. |
| Throws | |
|---|---|
java.lang.IllegalStateException |
if there is an unfinished active recording. |
java.lang.SecurityException |
if the audio config specifies audio should be enabled but the |
takePicture
@MainThread
fun takePicture(
executor: Executor,
callback: ImageCapture.OnImageCapturedCallback
): Unit
Captures a new still image for in memory access.
The listener is responsible for calling close on the returned image.
| Parameters | |
|---|---|
executor: Executor |
The executor in which the callback methods will be run. |
callback: ImageCapture.OnImageCapturedCallback |
Callback to be invoked for the newly captured image |
| Throws | |
|---|---|
java.lang.IllegalStateException |
If |
| See also | |
|---|---|
takePicture |
takePicture
@MainThread
fun takePicture(
outputFileOptions: ImageCapture.OutputFileOptions,
executor: Executor,
imageSavedCallback: ImageCapture.OnImageSavedCallback
): Unit
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.
The saved image is cropped to match the aspect ratio of the PreviewView. To take a picture with the maximum available resolution, make sure that the PreviewView's aspect ratio matches the max JPEG resolution supported by the camera.
| Parameters | |
|---|---|
outputFileOptions: ImageCapture.OutputFileOptions |
Options to store the newly captured image. |
executor: Executor |
The executor in which the callback methods will be run. |
imageSavedCallback: ImageCapture.OnImageSavedCallback |
Callback to be called for the newly captured image. |
| Throws | |
|---|---|
java.lang.IllegalStateException |
If |
| See also | |
|---|---|
takePicture |