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, OnImageSavedListener), 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>(context: Context!)

<init>(context: Context!, attrs: AttributeSet!)

<init>(context: Context!, attrs: AttributeSet!, defStyle: Int)

<init>(context: Context!, attrs: AttributeSet!, defStyleAttr: Int, defStyleRes: Int)

Public methods

Unit
bindToLifecycle(lifecycleOwner: LifecycleOwner!)

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

Unit

Turns on/off torch.

CameraX.LensFacing?

Returns the currently selected LensFacing.

CameraView.CaptureMode!

Returns the scale type used to scale the preview.

FlashMode!

Gets the active flash strategy.

Float

Returns the maximum zoom level.

Float

Returns the minimum zoom level.

CameraView.ScaleType!

Returns the scale type used to scale the preview.

Float

Returns the current zoom level.

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

Boolean

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

Unit
setCameraLensFacing(@Nullable lensFacing: CameraX.LensFacing?)

Sets the desired camera by specifying desired lensFacing.

Unit

Sets the CameraView capture mode

Unit
setFlash(flashMode: FlashMode!)

Sets the active flash strategy.

Unit

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

Unit

Sets the view finder scale type.

Unit
setZoomLevel(zoomLevel: Float)

Sets the current zoom level.

Unit
startRecording(file: File!, listener: OnVideoSavedListener!)

Takes a video and calls the OnVideoSavedListener when done.

Unit

Stops an in progress video.

Unit

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

Unit

Takes a picture and calls OnImageSavedListener#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

Parcelable!

Public constructors

<init>

CameraView(context: Context!)

<init>

CameraView(context: Context!, attrs: AttributeSet!)

<init>

CameraView(context: Context!, attrs: AttributeSet!, defStyle: Int)

<init>

CameraView(context: Context!, attrs: AttributeSet!, defStyleAttr: Int, defStyleRes: Int)

Public methods

bindToLifecycle

fun bindToLifecycle(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(): CameraX.LensFacing?

Returns the currently selected LensFacing.

getCaptureMode

fun getCaptureMode(): CameraView.CaptureMode!

Returns the scale type used to scale the preview.

Return
CameraView.CaptureMode!: The current CaptureMode.

getFlash

fun getFlash(): FlashMode!

Gets the active flash strategy.

getMaxZoomLevel

fun getMaxZoomLevel(): Float

Returns the maximum zoom level.

The zoom level 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 level.

getMinZoomLevel

fun getMinZoomLevel(): Float

Returns the minimum zoom level.

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

Return
Float: The minimum zoom level.

getScaleType

fun getScaleType(): CameraView.ScaleType!

Returns the scale type used to scale the preview.

Return
CameraView.ScaleType!: The current ScaleType.

getZoomLevel

fun getZoomLevel(): Float

Returns the current zoom level.

Return
Float: The current zoom level.

hasCameraWithLensFacing

fun hasCameraWithLensFacing(lensFacing: CameraX.LensFacing!): 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(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: CameraX.LensFacing?): 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(LensFacing), 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 CameraX.LensFacing?: The desired camera lensFacing.

setCaptureMode

fun setCaptureMode(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: FlashMode!): 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(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.

setZoomLevel

fun setZoomLevel(zoomLevel: Float): Unit

Sets the current zoom level.

Valid zoom values range from 1 to getMaxZoomLevel().

Parameters
zoomLevel Float: The requested zoom level.

startRecording

fun startRecording(file: File!, listener: OnVideoSavedListener!): Unit

Takes a video and calls the OnVideoSavedListener when done.

Parameters
file File!: The destination.

stopRecording

fun stopRecording(): Unit

Stops an in progress video.

takePicture

fun takePicture(listener: ImageCapture.OnImageCapturedListener!): Unit

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

Parameters
listener ImageCapture.OnImageCapturedListener!: Listener which will receive success or failure callbacks.

takePicture

fun takePicture(file: File!, listener: ImageCapture.OnImageSavedListener!): Unit

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

Parameters
file File!: The destination.
listener File!: Listener which will receive success or failure callbacks.

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

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(savedState: Parcelable!): Unit

onSaveInstanceState

protected fun onSaveInstanceState(): Parcelable!