Camera Controls

To use the camera controls, you can use CameraControl class to access it's functionality.

Minimum Requirements

  • SDK version 2.5.7 or higher

The latest HMSVideoView SDK version is:

Getting the camera controls instance.

The CameraControl instance can only be found in HMSLocalVideoTrack (local video track), otherwise the getCameraControl() method will return null.

val videoTrack : HMSVideotrack = hmsPeer.videoTrack val CameraControl = (videoTrack as? HMSLocalVideoTrack)?.getCameraControl()

Focus

Check if tap to focus feature is available

Checks if tap to focus is supported by current facing camera.

cameraControl.isTapToFocusSupported()

Set tap to focus.

View's coordinate system (x, y) (0,0)----------------([viewWidth], 0) | | | | | | |------------------ | (0,[viewHeight]) ([viewWidth], [viewHeight]) @param viewX should be the point in view's X coordinate @param viewY should be the point in view's Y coordinate @param viewWidth should be the current view's width @param viewHeight should be the current view's height cameraControl.setTapToFocusAt( viewX: Float, viewY: Float, viewWidth: Int, viewHeight: Int, scalingType: ScalingType = ScalingType.SCALE_ASPECT_FILL )

Zoom

Check if zoom feature is available

Checks if zoom feature is supported by current facing camera.

cameraControl.isZoomSupported()

Minimum zoom level

Gets minimum zoom value in for current camera session. If not supported [ZOOM_DEFAULT_VALUE] is returned

cameraControl.getMinZoom()

Maximum zoom level

Gets maximum zoom value in for current camera session. If not supported [ZOOM_DEFAULT_VALUE] is returned

cameraControl.getMaxZoom()

Set zoom level

Sets the current zoom, will not zoom if its outside the zoom bounds

cameraControl.setZoom(zoomValue: Float)

Reset zoom level

Resets the current zoom

cameraControl.resetZoom()

💡 Tap to focus and zoom controls doesn't work well together. If you want to focus, you will have to reset zoom.

Flash

Check if flash feature is available

Checks if the current facing camera has a flash device.

cameraControl.isFlashSupported()

Is flash enabled

Checks if the camera flash is enabled.

cameraControl.isFlashEnabled()

Enables or disable camera flash

Enables or disable camera flash.

cameraControl.setFlash(enable: Boolean)

Capture at the highest resolution offered by the camera

Takes the highest quality picture from the local video stream in jpg or jpeg irrespective of publisher's resolution. You need to provide a file object where the desired image is to be saved. If it's on the external directory proper, app permission has to be taken before.

@param savePath Path where Image is to be save @param callback [Boolean.true] if image was successfully captured and saved in the file provided, [Boolean.false] otherwise. cameraControl.captureImageAtMaxSupportedResolution( savePath: File, callback: (isSuccess: Boolean) -> Unit )

🔑 Note: To get correct orientation of the captured image, ensure the image extension(jpg or jpeg) is correct while saving the file.

Wiring camera controls with gestures

Here's an example how you can wire camera controls with gestures.

  • Tap to focus is done on touch
  • zoom is done using pinch gesture
  • very high resolution image camera capture is done using doube tap gesture.
var lastZoom = cameraControl.getMinZoom() val gestureDetector = GestureDetectorCompat(context, object : GestureDetector.SimpleOnGestureListener() { override fun onDown(e: MotionEvent?) = true override fun onSingleTapUp(event: MotionEvent): Boolean { if (cameraControl.isTapToFocusSupported()) cameraControl.setTapToFocusAt( event.x, event.y, viewWidth = width, viewHeight = height ) return true } override fun onDoubleTap(e: MotionEvent): Boolean { val cachePath = File(context.cacheDir, "images") cachePath.mkdirs() // The image file can have jpeg or jpg as it's extension val imageSavePath = File(cachePath, "image.jpg") cameraControl.captureImageAtMaxSupportedResolution(imageSavePath) { isSuccess -> if (isSuccess) { val fileSaveUri = FileProvider.getUriForFile( context, [use your own provider], imageSavePath ) } } return true } }) val scaleGestureDetector = ScaleGestureDetector( context, object : ScaleGestureDetector.SimpleOnScaleGestureListener() { override fun onScale(detector: ScaleGestureDetector): Boolean { if (cameraControl.isZoomSupported()) { lastZoom *= detector.scaleFactor cameraControl.setZoom(lastZoom) return true } return false } }) videoView.setOnTouchListener { _, event -> var didConsume = scaleGestureDetector.onTouchEvent(event) if (!scaleGestureDetector.isInProgress) { didConsume = gestureDetector.onTouchEvent(event) } didConsume }

Have a suggestion? Recommend changes ->

Was this helpful?

1234