Android 12 Developer Preview is here! Try it out, and give us your feedback!


public final class CameraView
extends FrameLayout

   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout

A View that displays a preview of the camera with methods takePicture(Executor, OnImageCapturedCallback), takePicture(ImageCapture.OutputFileOptions, 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.


Nested classes

enum CameraView.CaptureMode

The capture mode used by CameraView. 

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.

LiveData<PreviewView.StreamState> getPreviewStreamState()

Gets the LiveData of the underlying PreviewView's PreviewView.StreamState.

PreviewView.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(PreviewView.ScaleType scaleType)

Sets the view finder scale type.

void setZoomRatio(float zoomRatio)

Sets the current zoom ratio.

void startRecording(OutputFileOptions outputFileOptions, Executor executor, OnVideoSavedCallback callback)

Takes a video and calls the OnVideoSavedCallback when done.

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

Takes a video and calls the OnVideoSavedCallback when done.

void startRecording(ParcelFileDescriptor fd, Executor executor, OnVideoSavedCallback callback)

Takes a video and calls the OnVideoSavedCallback when done.

void stopRecording()

Stops an in progress video.

void takePicture(ImageCapture.OutputFileOptions outputFileOptions, Executor executor, ImageCapture.OnImageSavedCallback callback)

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

void takePicture(Executor executor, ImageCapture.OnImageCapturedCallback callback)

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

void toggleCamera()

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

Protected methods

FrameLayout.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