CameraView

class CameraView : ViewGroup
kotlin.Any
   ↳ android.view.View
   ↳ android.view.ViewGroup
   ↳ androidx.camera.view.CameraView

A View that displays a preview of the camera with methods , 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 . Use bindToLifecycle(LifecycleOwner) to start the camera.

Summary

Nested classes

The capture mode used by CameraView.

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

Public constructors

<init>(@NonNull context: Context)

<init>(@NonNull context: Context, @Nullable attrs: AttributeSet?)

<init>(@NonNull context: Context, @Nullable attrs: AttributeSet?, defStyle: Int)

<init>(@NonNull context: Context, @Nullable attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int)

Public methods

Unit
bindToLifecycle(@NonNull lifecycleOwner: LifecycleOwner)

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

Unit

Turns on/off torch.

Int?

Returns the currently selected LensFacing.

CameraView.CaptureMode

Returns the scale type used to scale the preview.

Int

Gets the active flash strategy.

Float

Returns the maximum zoom ratio.

Float

Returns the minimum zoom ratio.

CameraView.ScaleType

Returns the scale type used to scale the preview.

Float

Returns the current zoom ratio.

Boolean

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

Boolean

Returns whether the view allows pinch-to-zoom.

Boolean

Boolean

Returns current torch status.

Boolean

Returns whether the bound camera supports zooming.

Boolean
onTouchEvent(@NonNull event: MotionEvent)

Boolean

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

Unit
setCameraLensFacing(@Nullable lensFacing: Int?)

Sets the desired camera by specifying desired lensFacing.

Unit
setCaptureMode(@NonNull captureMode: CameraView.CaptureMode)

Sets the CameraView capture mode

Unit
setFlash(flashMode: Int)

Sets the active flash strategy.

Unit

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

Unit
setScaleType(@NonNull scaleType: CameraView.ScaleType)

Sets the view finder scale type.

Unit
setZoomRatio(zoomRatio: Float)

Sets the current zoom ratio.

Unit
startRecording(@NonNull file: File, @NonNull executor: Executor, @NonNull callback: OnVideoSavedCallback)

Takes a video and calls the OnVideoSavedCallback when done.

Unit

Stops an in progress video.

Unit
takePicture(@NonNull executor: Executor, @NonNull callback: ImageCapture.OnImageCapturedCallback)

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

Unit
takePicture(@NonNull file: File, @NonNull executor: Executor, @NonNull callback: ImageCapture.OnImageSavedCallback)

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

Unit

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

Protected methods

LayoutParams

Unit

Unit

Unit
onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int)

Unit
onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)

Unit
onRestoreInstanceState(@Nullable savedState: Parcelable?)

Parcelable

Public constructors

<init>

CameraView(@NonNull context: Context)

<init>

CameraView(@NonNull context: Context, @Nullable attrs: AttributeSet?)

<init>

CameraView(@NonNull context: Context, @Nullable attrs: AttributeSet?, defStyle: Int)

<init>

CameraView(@NonNull context: Context, @Nullable attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int)

Public methods

bindToLifecycle

fun bindToLifecycle(@NonNull lifecycleOwner: LifecycleOwner): Unit

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 state. Call this method only once camera permissions have been obtained.

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

Parameters
lifecycleOwner LifecycleOwner: The lifecycle that will control this view's camera
Exceptions
IllegalArgumentException if provided lifecycle is in a state.
IllegalStateException if camera permissions are not granted.

enableTorch

fun enableTorch(torch: Boolean): Unit

Turns on/off torch.

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

getCameraLensFacing

@Nullable fun getCameraLensFacing(): Int?

Returns the currently selected LensFacing.

getCaptureMode

@NonNull fun getCaptureMode(): CameraView.CaptureMode

Returns the scale type used to scale the preview.

Return
CameraView.CaptureMode: The current CaptureMode.

getFlash

fun getFlash(): Int

Gets the active flash strategy.

getMaxZoomRatio

fun getMaxZoomRatio(): Float

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.

Return
Float: The maximum zoom ratio.

getMinZoomRatio

fun getMinZoomRatio(): Float

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.

Return
Float: The minimum zoom ratio.

getScaleType

@NonNull fun getScaleType(): CameraView.ScaleType

Returns the scale type used to scale the preview.

Return
CameraView.ScaleType: The current ScaleType.

getZoomRatio

fun getZoomRatio(): Float

Returns the current zoom ratio.

Return
Float: The current zoom ratio.

hasCameraWithLensFacing

fun hasCameraWithLensFacing(lensFacing: Int): Boolean

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

Return
Boolean: True if the device supports the direction.
Exceptions
IllegalStateException if the CAMERA permission is not currently granted.

isPinchToZoomEnabled

fun isPinchToZoomEnabled(): Boolean

Returns whether the view allows pinch-to-zoom.

Return
Boolean: True if pinch to zoom is enabled.

isRecording

fun isRecording(): Boolean
Return
Boolean: True if currently recording.

isTorchOn

fun isTorchOn(): Boolean

Returns current torch status.

Return
Boolean: true if torch is on , otherwise false

isZoomSupported

fun isZoomSupported(): Boolean

Returns whether the bound camera supports zooming.

Return
Boolean: True if the camera supports zooming.

onTouchEvent

fun onTouchEvent(@NonNull event: MotionEvent): Boolean

performClick

fun performClick(): Boolean

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

setCameraLensFacing

fun setCameraLensFacing(@Nullable lensFacing: Int?): Unit

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 Int?: The desired camera lensFacing.

setCaptureMode

fun setCaptureMode(@NonNull captureMode: CameraView.CaptureMode): Unit

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

setFlash

fun setFlash(flashMode: Int): Unit

Sets the active flash strategy.

setPinchToZoomEnabled

fun setPinchToZoomEnabled(enabled: Boolean): Unit

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

fun setScaleType(@NonNull scaleType: CameraView.ScaleType): Unit

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

setZoomRatio

fun setZoomRatio(zoomRatio: Float): Unit

Sets the current zoom ratio.

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

Parameters
zoomRatio Float: The requested zoom ratio.

startRecording

fun startRecording(@NonNull file: File, @NonNull executor: Executor, @NonNull callback: OnVideoSavedCallback): Unit

Takes a video and calls the OnVideoSavedCallback when done.

Parameters
file File: The destination.
executor File: The executor in which the callback methods will be run.
callback File: Callback which will receive success or failure.

stopRecording

fun stopRecording(): Unit

Stops an in progress video.

takePicture

fun takePicture(@NonNull executor: Executor, @NonNull callback: ImageCapture.OnImageCapturedCallback): Unit

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

Parameters
executor Executor: The executor in which the callback methods will be run.
callback Executor: Callback which will receive success or failure callbacks.

takePicture

fun takePicture(@NonNull file: File, @NonNull executor: Executor, @NonNull callback: ImageCapture.OnImageSavedCallback): Unit

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

Parameters
file File: The destination.
executor File: The executor in which the callback methods will be run.
callback File: Callback which will receive success or failure.

toggleCamera

fun toggleCamera(): Unit

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 .

Protected methods

generateDefaultLayoutParams

@NonNull protected fun generateDefaultLayoutParams(): LayoutParams

onAttachedToWindow

protected fun onAttachedToWindow(): Unit

onDetachedFromWindow

protected fun onDetachedFromWindow(): Unit

onLayout

protected fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int): Unit

onMeasure

protected fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int): Unit

onRestoreInstanceState

protected fun onRestoreInstanceState(@Nullable savedState: Parcelable?): Unit

onSaveInstanceState

@NonNull protected fun onSaveInstanceState(): Parcelable