CameraView

class CameraView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr: Int = 0, @StyleRes defStyleRes: Int = 0) : FrameLayout

The CameraView provides a straightforward way to create and manage a camera. On creation, the view will display the camera's preview and provide access to preview frames through the previewFrames property, if video recording is enabled the video recording will start automatically. When the CameraView goes into Lifecycle.Event.ON_PAUSE the camera will be released and the video recording will stop and be discarded. Completed video recordings will be posted on the recordedVideo property.

The CameraView also provides a way to focus the camera by overriding the onTouchEvent method.

Samples

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.LiveData
import com.miteksystems.misnap.apputil.LicenseFetcher
import com.miteksystems.misnap.camera.frameproducers.FrameProducer.Event
import com.miteksystems.misnap.camera.view.CameraView
import com.miteksystems.misnap.controller.MiSnapController
import com.miteksystems.misnap.core.Frame
import com.miteksystems.misnap.core.MiSnapSettings
fun main() { 
   //sampleStart 
   /**
 * This example demonstrates how to programmatically use a [CameraView] to get access to the device's
 * hardware camera and interact with various camera APIs such as preview frames, picture frames, torch
 * controls, focus controls, video recording, etc.
 *
 * NOTE: Ensure that the provided license has all the necessary features enabled for the target
 *  MiSnap session.
 *
 * @see com.miteksystems.misnap.examples.camera.CameraViewXml for an example on how to use the [CameraView]
 * from an XML layout.
 */
class CameraViewCode : Fragment() {

    /**
     * Fetch the Misnap SDK license.
     * Good practice: Handle the license in a way that it is remotely updatable.
     */
    private val license by lazy {  
        LicenseFetcher.fetch()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        /**
         * The [CameraView] needs a [MiSnapSettings.Camera.Profile] to select the correct camera which
         * is defined based on the [MiSnapSettings.UseCase], you can also set it explicitly to match your
         * specific needs.
         */
        val settings = MiSnapSettings(MiSnapSettings.UseCase.ID_FRONT, license).apply {
            camera.profile = MiSnapSettings.Camera.Profile.DOCUMENT_BACK_CAMERA
            analysis.document.trigger = MiSnapSettings.Analysis.Document.Trigger.AUTO
        }

        /**
         * Instantiate a [CameraView] and observe the various [LiveData]s to get preview frames, picture
         * frames, recorded videos, camera events etc.
         */
        return CameraView(requireContext())
            .apply {
                /**
                 * Observe the [CameraView.previewFrames] [LiveData] to handle the camera preview [Frame]s,
                 * e.g. by analyzing them using the [MiSnapController.analyzeFrame] method.
                 *
                 * @see com.miteksystems.misnap.examples.science for examples on how to analyze [Frame]s
                 * with different sciences.
                 */
                previewFrames.observe(viewLifecycleOwner) {

                }

                /**
                 * Observe the [CameraView.pictureFrames] [LiveData] to handle the camera pictures
                 * produced by the [CameraView.takePicture] method.
                 */
                pictureFrames.observe(viewLifecycleOwner) {

                }

                /**
                 * Observe the [CameraView.cameraEvents] [LiveData] to handle the various camera
                 * related [Event]s.
                 *
                 * @see com.miteksystems.misnap.camera.frameproducers.FrameProducer.Event for the
                 * full list of emitted events.
                 */
                cameraEvents.observe(viewLifecycleOwner) { event ->
                    when (event) {
                        is Event.CameraInitialized -> {

                        }
                        is Event.CameraReady -> {
                            //Start to interact with the camera
                        }
                        is Event.InitializationError.InsufficientCamera -> {

                        }
                        is Event.InitializationError.CameraNotAvailable -> {

                        }
                        else -> {
                            //Nothing to do
                        }
                    }
                }

                /**
                 * Observe the [CameraView.recordedVideo] [LiveData] to receive a [ByteArray] with
                 * the contents of the recorded video if requested in [MiSnapSettings].
                 */
                recordedVideo.observe(viewLifecycleOwner) { videoBytes ->

                }
            }
            .also { cameraView ->
                /**
                 * Start the camera preview and register a callback to know when the preview has
                 * started.
                 */
                cameraView.startCamera(settings.camera, viewLifecycleOwner) {

                }
            }
    }
} 
   //sampleEnd
}

Constructors

Link copied to clipboard
constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr: Int = 0, @StyleRes defStyleRes: Int = 0)

Functions

Link copied to clipboard
Link copied to clipboard

Clears the list of boxes and colors to draw and removes the drawn boxes.

Link copied to clipboard
fun drawBoxes(boxesColorsMap: Map<Array<IntArray>, Int>)

Draws a set of quadrilateral shapes on the camera preview scaling the points to the area without black bars.

Link copied to clipboard
open override fun onTouchEvent(event: MotionEvent): Boolean
Link copied to clipboard
fun setTorchEnabled(enable: Boolean, torchChangedResultListener: (isSuccessful: Boolean) -> Unit? = null)

Sets the new state of the torch and optionally registers a listener to the operation result.

Link copied to clipboard
fun startAutoFocus(focusPoint: Point? = null)

Focuses the camera, either at the provided point or at the center of the screen if no point is specified The status of the focus operation will be updated on focusingEvents.

Link copied to clipboard
fun startCamera(cameraSettings: MiSnapSettings.Camera, lifecycleOwner: LifecycleOwner? = null, requireTakePictureCapability: Boolean = true, cameraStartedListener: () -> Unit = {})

Starts the camera with the provided configurations. These settings are equivalent to the following attributes in a layout xml file:

Link copied to clipboard

Starts the video recording, the video will be posted on recordedVideo when the recording has been finished with stopRecording.

Link copied to clipboard

Stops recording the camera's view, the resulting video will be posted to the recordedVideo property.

Link copied to clipboard

Takes a picture, the image will be posted on pictureFrames.

Properties

Link copied to clipboard

LiveData for camera status events and errors.

Link copied to clipboard

LiveData for tracking if the camera is focusing.

Link copied to clipboard

LiveData for pictures taken by the camera.

Link copied to clipboard

LiveData for preview images from the camera.

Link copied to clipboard

LiveData containing the video after stopRecording has been called.

Link copied to clipboard

LiveData object to emit the changes in the torch state, true for ON and false for OFF. Default Value: False.