Skip to content

Usage

PreviewView

If you need to present a preview to a user, you need to initiate a PreviewView in an Activity/Fragment's layout.

<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_height="match_parent"
        android:layout_width="match_parent">

    <androidx.camera.view.PreviewView
            android:id="@+id/previewView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

LocalFaceDetector

A facial recognizer that detects a face and its properties (rotation, size, etc.).

The LocalFaceDetector constructor has two parameters:

  • context: Android context
  • onDetectionResultListener: a detection result listener

Create a LocalFaceDetector instance in the onCreate of your Activity and close it in onDestroy.

override fun onCreate() {
    onDetectionResultListener = OnDetectionResultListener { result ->
        // Show a hint to user about the conditions
    }
    localFaceDetector = LocalFaceDetector(context, onDetectionResultListener)
}

override fun onDestroy() {
    localFaceDetector.close()
}

You don't need to use it in a straightforward way. The right way is to use its functionality through the IadCameraController API.

DetectionShortDescription

During a face recognition you may receive a next descriptions of face recognition work.

enum class DetectionShortDescription {
    /**
     * The photo is acceptable.
     */
    Ok,

    /**
     * The face detector found more than one face in the image.
     */
    TooManyFaces,

    /**
     * The face detector can't find a face in the image. Face detection has a sensitivity level and may ignore very small faces.
     */
    FaceNotFound,

    /**
     * The facial area is not large enough for analysis. The face can be considered small for several reasons:
     * - Face size in pixels is too small.
     * - Interpupillary distance in pixels is too small.
     * - The difference between the image size and the face size is too large.
     */
    FaceTooSmall,

    /**
     * The face is cropped.
     */
    FaceCropped,

    /**
     * The out-of-plane rotation angle (face pitch and yaw) is too large.
     */
    FaceAngled,

    /**
     * There is occlusion of the face. This detection is designed to work with medical masks that cover the nose and mouth; other occlusions are not guaranteed. Occlusion detection has a probabilistic nature and may have errors.
     */
    FaceOccluded,

    /**
     * A face is too close.
     */
    FaceTooClose,

    /**
     * A face is too close to the border.
     */
    FaceCloseToBorder,

    /**
     * A face is moving.
     */
    FaceMoving,

    /**
     * We can't make any statements about the face because we don't have enough information.
     */
    Undefined,

    /**
     * Eyes are closed.
     */
    EyesClosed,

    /**
     * Dark image.
     */
    DarkImage,

    /**
     * Face is blurry.
     */
    FaceBlurry,

    /**
     * Mouth is open.
     */
    MouthOpen,

    /**
     * Sunglasses are detected.
     */
    SunglassesDetected,
}

IadCameraController

A class that provides an API for camera control.

Create an instance with:

  • PreviewView: a view from the CameraX package that implements a preview feature
  • LocalFaceDetector: the face detector
  • lifecycleOwner: typically an instance of an Activity

Take into consideration that IadCameraController cannot run concurrently, meaning that only one instance can run at any given time.

Instantiate it in the onCreate of your Activity and close it in the onDestroy method.

override fun onCreate() {
    cameraController = IadCameraController(localFaceDetector, peviewView,  lifecycleOwner)
}

override fun onDestroy() {
    cameraController.close()
}

Connect the lifecycle of IadCameraController with the lifecycle of Activity.

override fun onStart() {
    cameraController.openCamera()
}

override fun onResume() {
    cameraController.startPreview()
}

override fun onPause() {
    cameraController.stopPreview()
}

override fun onStop() {
    cameraController.closeCamera()
}

Add data processors to IadCameraController.

cameraController.onCameraErrorListener = OnCameraErrorListener { error ->
    // Handle the exception.
}

cameraController.onDetectionErrorListener = OnDetectionErrorListener { exception ->
    // Handle the exception.
}

cameraController.photoProcessor = object : ImageProcessor {
    override fun process(data: ByteArray, size: Size, format: Int, rotationAngle: Int) {
        // Photo processing: You can display the photo as a preview for the user.
        // Additionally, this event is an appropriate time to show a progress bar.
    }
}

cameraController.bundleProcessor = object : BundleProcessor {
    override fun process(bundle: ByteArray) {
        // Pass this bundle to IDLiveFace Plus server for processing.
        // The server returns an injection attack probability.
    }
}

Turn on face recognition. A photo will be taken automatically when the conditions are favorable.

cameraController.startDetection()

Call takePhoto() if you need a photo immediately without the condition check.

cameraController.takePhoto()

Example

Additional information on how to use the library can be found in the folder within the release archive at the path idlive-face-capture-android-X.X.X-release/iad-example.