CameraController
public
abstract
class
CameraController
extends Object
| java.lang.Object | |
| ↳ | androidx.camera.view.CameraController |
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 classes | |
|---|---|
class |
CameraController.OutputSize
Represents the output size of a |
Constants | |
|---|---|
int |
IMAGE_ANALYSIS
Bitmask option to enable |
int |
IMAGE_CAPTURE
Bitmask option to enable |
int |
TAP_TO_FOCUS_FAILED
The previous tap-to-focus action was failed to complete. |
int |
TAP_TO_FOCUS_FOCUSED
The previous tap-to-focus action was completed successfully and the camera is focused. |
int |
TAP_TO_FOCUS_NOT_FOCUSED
The previous tap-to-focus action was completed successfully but the camera is still
unfocused, similar to the |
int |
TAP_TO_FOCUS_NOT_STARTED
No tap-to-focus action has been started by the end user. |
int |
TAP_TO_FOCUS_STARTED
A tap-to-focus action has started but not completed. |
int |
VIDEO_CAPTURE
Bitmask option to enable video capture use case. |
Public methods | |
|---|---|
void
|
clearImageAnalysisAnalyzer()
Removes a previously set analyzer. |
ListenableFuture<Void>
|
enableTorch(boolean torchEnabled)
Enable the torch or disable the torch. |
CameraControl
|
getCameraControl()
Gets the |
CameraInfo
|
getCameraInfo()
Gets the |
CameraSelector
|
getCameraSelector()
Gets the |
Executor
|
getImageAnalysisBackgroundExecutor()
Gets the default executor for |
int
|
getImageAnalysisBackpressureStrategy()
Returns the mode with which images are acquired. |
int
|
getImageAnalysisImageQueueDepth()
Gets the image queue depth of |
CameraController.OutputSize
|
getImageAnalysisTargetSize()
Returns the intended output size for |
int
|
getImageCaptureFlashMode()
Gets the flash mode for |
Executor
|
getImageCaptureIoExecutor()
Gets the default executor for |
int
|
getImageCaptureMode()
Returns the image capture mode. |
CameraController.OutputSize
|
getImageCaptureTargetSize()
Returns the intended output size for |
ListenableFuture<Void>
|
getInitializationFuture()
Gets a |
CameraController.OutputSize
|
getPreviewTargetSize()
Returns the intended output size for |
LiveData<Integer>
|
getTapToFocusState()
Returns a |
LiveData<Integer>
|
getTorchState()
Returns a |
CameraController.OutputSize
|
getVideoCaptureTargetSize()
Returns the intended output size for |
LiveData<ZoomState>
|
getZoomState()
|
boolean
|
hasCamera(CameraSelector cameraSelector)
Checks if the given |
boolean
|
isImageAnalysisEnabled()
Checks if |
boolean
|
isImageCaptureEnabled()
Checks if |
boolean
|
isPinchToZoomEnabled()
Returns whether pinch-to-zoom is enabled. |
boolean
|
isRecording()
Returns whether there is a in progress video recording. |
boolean
|
isTapToFocusEnabled()
Returns whether tap-to-focus is enabled. |
boolean
|
isVideoCaptureEnabled()
Checks if video capture is enabled. |
void
|
setCameraSelector(CameraSelector cameraSelector)
Sets the |
void
|
setEnabledUseCases(int enabledUseCases)
Enables or disables use cases. |
void
|
setImageAnalysisAnalyzer(Executor executor, ImageAnalysis.Analyzer analyzer)
Sets an analyzer to receive and analyze images. |
void
|
setImageAnalysisBackgroundExecutor(Executor executor)
Sets the executor that will be used for |
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. |
void
|
setImageAnalysisImageQueueDepth(int depth)
Sets the image queue depth of |
void
|
setImageAnalysisTargetSize(CameraController.OutputSize targetSize)
Sets the intended output size for |
void
|
setImageCaptureFlashMode(int flashMode)
Sets the flash mode for |
void
|
setImageCaptureIoExecutor(Executor executor)
Sets the default executor that will be used for |
void
|
setImageCaptureMode(int captureMode)
Sets the image capture mode. |
void
|
setImageCaptureTargetSize(CameraController.OutputSize targetSize)
Sets the intended image size for |
ListenableFuture<Void>
|
setLinearZoom(float linearZoom)
Sets current zoom by a linear zoom value ranging from 0f to 1.0f. |
void
|
setPinchToZoomEnabled(boolean enabled)
Enables/disables pinch-to-zoom. |
void
|
setPreviewTargetSize(CameraController.OutputSize targetSize)
Sets the intended output size for |
void
|
setTapToFocusEnabled(boolean enabled)
Enables/disables tap-to-focus. |
void
|
setVideoCaptureTargetSize(CameraController.OutputSize targetSize)
Sets the intended video size for |
ListenableFuture<Void>
|
setZoomRatio(float zoomRatio)
Sets current zoom by ratio. |
void
|
startRecording(OutputFileOptions outputFileOptions, Executor executor, OnVideoSavedCallback callback)
Takes a video and calls the OnVideoSavedCallback when done. |
void
|
stopRecording()
Stops a in progress video recording. |
void
|
takePicture(ImageCapture.OutputFileOptions outputFileOptions, Executor executor, ImageCapture.OnImageSavedCallback imageSavedCallback)
Captures a new still image and saves to a file along with application specified metadata. |
void
|
takePicture(Executor executor, ImageCapture.OnImageCapturedCallback callback)
Captures a new still image for in memory access. |
Inherited methods | |
|---|---|
Constants
IMAGE_ANALYSIS
public static final int IMAGE_ANALYSIS
Bitmask option to enable ImageAnalysis. In setEnabledUseCases(int), if
(enabledUseCases & IMAGE_ANALYSIS) != 0, then controller will enable image analysis features.
Constant Value: 2 (0x00000002)
IMAGE_CAPTURE
public static final int IMAGE_CAPTURE
Bitmask option to enable ImageCapture. In setEnabledUseCases(int), if
(enabledUseCases & IMAGE_CAPTURE) != 0, then controller will enable image capture features.
Constant Value: 1 (0x00000001)
TAP_TO_FOCUS_FAILED
public static final int TAP_TO_FOCUS_FAILED
The previous tap-to-focus action was failed to complete. This is usually due to device limitations.
Constant Value: 4 (0x00000004)
TAP_TO_FOCUS_FOCUSED
public static final int TAP_TO_FOCUS_FOCUSED
The previous tap-to-focus action was completed successfully and the camera is focused.
Constant Value: 2 (0x00000002)
TAP_TO_FOCUS_NOT_FOCUSED
public static final int TAP_TO_FOCUS_NOT_FOCUSED
The previous tap-to-focus action was completed successfully but the camera is still
unfocused, similar to the CameraMetadata.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.
Constant Value: 3 (0x00000003)
TAP_TO_FOCUS_NOT_STARTED
public static final int TAP_TO_FOCUS_NOT_STARTED
No tap-to-focus action has been started by the end user.
Constant Value: 0 (0x00000000)
TAP_TO_FOCUS_STARTED
public static final int TAP_TO_FOCUS_STARTED
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.
Constant Value: 1 (0x00000001)
VIDEO_CAPTURE
public static final int VIDEO_CAPTURE
Bitmask option to enable video capture use case. In setEnabledUseCases(int), if
(enabledUseCases & VIDEO_CAPTURE) != 0, then controller will enable video capture features.
Constant Value: 4 (0x00000004)
Public methods
clearImageAnalysisAnalyzer
public void clearImageAnalysisAnalyzer ()
Removes a previously set analyzer.
This will stop data from streaming to the ImageAnalysis.
See also:
enableTorch
public ListenableFuture<Void> enableTorch (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 | |
|---|---|
torchEnabled |
boolean: true to turn on the torch, false to turn it off. |
| Returns | |
|---|---|
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:
getCameraControl
public 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(float) v.s. CameraControl.setLinearZoom(float).
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 CameraControl of the current camera. Returns null if camera is not ready. |
See also:
getCameraInfo
public 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.
CameraInfo.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 CameraInfo of the current camera. Returns null if camera is not ready. |
See also:
getCameraSelector
public CameraSelector getCameraSelector ()
Gets the CameraSelector.
The default value isCameraSelector.DEFAULT_BACK_CAMERA.
| Returns | |
|---|---|
CameraSelector |
|
See also:
getImageAnalysisBackgroundExecutor
public Executor getImageAnalysisBackgroundExecutor ()
Gets the default executor for ImageAnalysis background tasks.
| Returns | |
|---|---|
Executor |
|
getImageAnalysisBackpressureStrategy
public int getImageAnalysisBackpressureStrategy ()
Returns the mode with which images are acquired.
If not set, it defaults to ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST.
| Returns | |
|---|---|
int |
The backpressure strategy applied to the image producer. |
See also:
getImageAnalysisImageQueueDepth
public int getImageAnalysisImageQueueDepth ()
Gets the image queue depth of ImageAnalysis.
| Returns | |
|---|---|
int |
|
See also:
getImageAnalysisTargetSize
public CameraController.OutputSize getImageAnalysisTargetSize ()
Returns the intended output size for ImageAnalysis set by
setImageAnalysisTargetSize(OutputSize), or null if not set.
| Returns | |
|---|---|
CameraController.OutputSize |
|
getImageCaptureFlashMode
public int getImageCaptureFlashMode ()
Gets the flash mode for ImageCapture.
| Returns | |
|---|---|
int |
the flashMode. Value is ImageCapture.FLASH_MODE_AUTO,
ImageCapture.FLASH_MODE_ON, or ImageCapture.FLASH_MODE_OFF. |