CameraView

public final class CameraView
extends ViewGroup

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ androidx.camera.view.CameraView


A View that displays a preview of the camera with methods takePicture(Executor, OnImageCapturedCallback), takePicture(File, Executor, OnImageSavedCallback), startRecording(File, Executor, OnVideoSavedCallback) and stopRecording().

Because the Camera is a limited resource and consumes a high amount of power, CameraView must be opened/closed. CameraView will handle opening/closing automatically through use of a LifecycleOwner. Use bindToLifecycle(LifecycleOwner) to start the camera.

Summary

Nested classes

enum CameraView.CaptureMode

The capture mode used by CameraView. 

enum CameraView.ScaleType

Options for scaling the bounds of the view finder to the bounds of this view. 

Inherited constants

Inherited fields

Public constructors

CameraView(Context context)
CameraView(Context context, AttributeSet attrs)
CameraView(Context context, AttributeSet attrs, int defStyle)
CameraView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Public methods

void bindToLifecycle(LifecycleOwner lifecycleOwner)

Binds control of the camera used by this view to the given lifecycle.

void enableTorch(boolean torch)

Turns on/off torch.

Integer getCameraLensFacing()

Returns the currently selected LensFacing.

CameraView.CaptureMode getCaptureMode()

Returns the scale type used to scale the preview.

int getFlash()

Gets the active flash strategy.

float getMaxZoomRatio()

Returns the maximum zoom ratio.

float getMinZoomRatio()

Returns the minimum zoom ratio.

CameraView.ScaleType getScaleType()

Returns the scale type used to scale the preview.

float getZoomRatio()

Returns the current zoom ratio.

boolean hasCameraWithLensFacing(int lensFacing)

Queries whether the current device has a camera with the specified direction.

boolean isPinchToZoomEnabled()

Returns whether the view allows pinch-to-zoom.

boolean isRecording()
boolean isTorchOn()

Returns current torch status.

boolean isZoomSupported()

Returns whether the bound camera supports zooming.

boolean onTouchEvent(MotionEvent event)
boolean performClick()

Focus the position of the touch event, or focus the center of the preview for accessibility events

void setCameraLensFacing(Integer lensFacing)

Sets the desired camera by specifying desired lensFacing.

void setCaptureMode(CameraView.CaptureMode captureMode)

Sets the CameraView capture mode

This controls only image or video capture function is enabled or both are enabled.

void setFlash(int flashMode)

Sets the active flash strategy.

void setPinchToZoomEnabled(boolean enabled)

Sets whether the view should allow pinch-to-zoom.

void setScaleType(CameraView.ScaleType scaleType)

Sets the view finder scale type.

void setZoomRatio(float zoomRatio)

Sets the current zoom ratio.

void startRecording(File file, Executor executor, VideoCapture.OnVideoSavedCallback callback)

Takes a video and calls the OnVideoSavedCallback when done.

void stopRecording()

Stops an in progress video.

void takePicture(File file, Executor executor, ImageCapture.OnImageSavedCallback callback)

Takes a picture and calls ImageCapture.OnImageSavedCallback.onImageSaved(File) when done.

void takePicture(Executor executor, ImageCapture.OnImageCapturedCallback callback)

Takes a picture, and calls ImageCapture.OnImageCapturedCallback.onCaptureSuccess(ImageProxy, int) once when done.

void toggleCamera()

Toggles between the primary front facing camera and the primary back facing camera.

Protected methods

ViewGroup.LayoutParams generateDefaultLayoutParams()
void onAttachedToWindow()
void onDetachedFromWindow()
void onLayout(boolean changed, int left, int top, int right, int bottom)
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
void onRestoreInstanceState(Parcelable savedState)
Parcelable onSaveInstanceState()

Inherited methods

Public constructors

CameraView

public CameraView (Context context)

Parameters
context Context

CameraView

public CameraView (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

CameraView

public CameraView (Context context, 
                AttributeSet attrs, 
                int defStyle)

Parameters
context Context

attrs AttributeSet

defStyle int

CameraView

public CameraView (Context context, 
                AttributeSet attrs, 
                int defStyleAttr, 
                int defStyleRes)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

defStyleRes int

Public methods

bindToLifecycle

public void bindToLifecycle (LifecycleOwner lifecycleOwner)

Binds control of the camera used by this view to the given lifecycle.

This links opening/closing the camera to the given lifecycle. The camera will not operate unless this method is called with a valid LifecycleOwner that is not in the Lifecycle.State.DESTROYED state. Call this method only once camera permissions have been obtained.

Once the provided lifecycle has transitioned to a Lifecycle.State.DESTROYED state, CameraView must be bound to a new lifecycle through this method in order to operate the camera.

Requires the CAMERA permission.

Parameters
lifecycleOwner LifecycleOwner: The lifecycle that will control this view's camera

Throws
IllegalArgumentException if provided lifecycle is in a Lifecycle.State.DESTROYED state.
IllegalStateException if camera permissions are not granted.

enableTorch

public void enableTorch (boolean torch)

Turns on/off torch.

Parameters
torch boolean: True to turn on torch, false to turn off torch.

getCameraLensFacing

public Integer getCameraLensFacing ()

Returns the currently selected LensFacing.

Returns
Integer

getCaptureMode

public CameraView.CaptureMode getCaptureMode ()

Returns the scale type used to scale the preview.

Returns
CameraView.CaptureMode The current CameraView.CaptureMode.

getFlash

public int getFlash ()

Gets the active flash strategy.

Returns
int

getMaxZoomRatio

public float getMaxZoomRatio ()

Returns the maximum zoom ratio.

The zoom ratio corresponds to the ratio between both the widths and heights of a non-zoomed image and a maximally zoomed image for the selected camera.

Returns
float The maximum zoom ratio.

getMinZoomRatio

public float getMinZoomRatio ()

Returns the minimum zoom ratio.

For most cameras this should return a zoom ratio of 1. A zoom ratio of 1 corresponds to a non-zoomed image.

Returns
float The minimum zoom ratio.

getScaleType

public CameraView.ScaleType getScaleType ()

Returns the scale type used to scale the preview.

Returns
CameraView.ScaleType The current CameraView.ScaleType.

getZoomRatio

public float getZoomRatio ()

Returns the current zoom ratio.

Returns
float The current zoom ratio.

hasCameraWithLensFacing

public boolean hasCameraWithLensFacing (int lensFacing)

Queries whether the current device has a camera with the specified direction.

Requires the CAMERA permission.

Parameters
lensFacing int

Returns
boolean True if the device supports the direction.

Throws
IllegalStateException if the CAMERA permission is not currently granted.

isPinchToZoomEnabled

public boolean isPinchToZoomEnabled ()

Returns whether the view allows pinch-to-zoom.

Returns
boolean True if pinch to zoom is enabled.

isRecording

public boolean isRecording ()

Returns
boolean True if currently recording.

isTorchOn

public boolean isTorchOn ()

Returns current torch status.

Returns
boolean true if torch is on , otherwise false

isZoomSupported

public boolean isZoomSupported ()

Returns whether the bound camera supports zooming.

Returns
boolean True if the camera supports zooming.

onTouchEvent

public boolean onTouchEvent (MotionEvent event)

Parameters
event MotionEvent

Returns
boolean

performClick

public boolean performClick ()

Focus the position of the touch event, or focus the center of the preview for accessibility events

Returns
boolean

setCameraLensFacing

public void setCameraLensFacing (Integer lensFacing)

Sets the desired camera by specifying desired lensFacing.

This will choose the primary camera with the specified camera lensFacing.

If called before bindToLifecycle(LifecycleOwner), this will set the camera to be used when first bound to the lifecycle. If the specified lensFacing is not supported by the device, as determined by hasCameraWithLensFacing(int), the first supported lensFacing will be chosen when bindToLifecycle(LifecycleOwner) is called.

If called with null AFTER binding to the lifecycle, the behavior would be equivalent to unbind the use cases without the lifecycle having to be destroyed.

Parameters
lensFacing Integer: The desired camera lensFacing.

setCaptureMode

public void setCaptureMode (CameraView.CaptureMode captureMode)

Sets the CameraView capture mode

This controls only image or video capture function is enabled or both are enabled.

Parameters
captureMode CameraView.CaptureMode: The desired CameraView.CaptureMode.

setFlash

public void setFlash (int flashMode)

Sets the active flash strategy.

Parameters
flashMode int

setPinchToZoomEnabled

public void setPinchToZoomEnabled (boolean enabled)

Sets whether the view should allow pinch-to-zoom.

When enabled, the user can pinch the camera to zoom in/out. This only has an effect if the bound camera supports zoom.

Parameters
enabled boolean: True to enable pinch-to-zoom.

setScaleType

public void setScaleType (CameraView.ScaleType scaleType)

Sets the view finder scale type.

This controls how the view finder should be scaled and positioned within the view.

Parameters
scaleType CameraView.ScaleType: The desired CameraView.ScaleType.

setZoomRatio

public void setZoomRatio (float zoomRatio)

Sets the current zoom ratio.

Valid zoom values range from getMinZoomRatio() to getMaxZoomRatio().

Parameters
zoomRatio float: The requested zoom ratio.

startRecording

public void startRecording (File file, 
                Executor executor, 
                VideoCapture.OnVideoSavedCallback callback)

Takes a video and calls the OnVideoSavedCallback when done.

Parameters
file File: The destination.

executor Executor: The executor in which the callback methods will be run.

callback VideoCapture.OnVideoSavedCallback: Callback which will receive success or failure.

stopRecording

public void stopRecording ()

Stops an in progress video.

takePicture

public void takePicture (File file, 
                Executor executor, 
                ImageCapture.OnImageSavedCallback callback)

Takes a picture and calls ImageCapture.OnImageSavedCallback.onImageSaved(File) when done.

Parameters
file File: The destination.

executor Executor: The executor in which the callback methods will be run.

callback ImageCapture.OnImageSavedCallback: Callback which will receive success or failure.

takePicture

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

Takes a picture, and calls ImageCapture.OnImageCapturedCallback.onCaptureSuccess(ImageProxy, int) once when done.

Parameters
executor Executor: The executor in which the callback methods will be run.

callback ImageCapture.OnImageCapturedCallback: Callback which will receive success or failure callbacks.

toggleCamera

public void toggleCamera ()

Toggles between the primary front facing camera and the primary back facing camera.

This will have no effect if not already bound to a lifecycle via bindToLifecycle(LifecycleOwner).

Protected methods

generateDefaultLayoutParams

protected ViewGroup.LayoutParams generateDefaultLayoutParams ()

Returns
ViewGroup.LayoutParams

onAttachedToWindow

protected void onAttachedToWindow ()

onDetachedFromWindow

protected void onDetachedFromWindow ()

onLayout

protected void onLayout (boolean changed, 
                int left, 
                int top, 
                int right, 
                int bottom)

Parameters
changed boolean

left int

top int

right int

bottom int

onMeasure

protected void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Parameters
widthMeasureSpec int

heightMeasureSpec int

onRestoreInstanceState

protected void onRestoreInstanceState (Parcelable savedState)

Parameters
savedState Parcelable

onSaveInstanceState

protected Parcelable onSaveInstanceState ()

Returns
Parcelable