Skip to content
On this page

Creating a User Preview

As part of your application, it is recommended to present the user with a camera preview, as it helps the user to center his/her face on the camera screen. It also allows for the presentation of a face detection bounding graphic on the screen.

The following code can be used to create a preview and present a face detection bounding graphic to the user.

1. Create a UIImageView Element in your Storyboard

Note that the aspect ratio of the images received from the SDK is 4:3.

2. Receive Images from the SDK

Images are received from the SDK on a background thread. The application must switch to the UI thread in order to perform UI updates.

Swift
func onImage(imageData: ImageData) {
    DispatchQueue.main.async {
        // Show imageData.image on the screen
    }
}

3. Draw the Image and the Face Detection Graphics

Swift
private var faceDetection = UIImageView(image: UIImage(named: "face_detection"))

@IBOutlet weak var cameraPreview: UIImageView!

fun handleImageData(imageData: ImageData) {
    // Setting the received UIImage to the camera preview UIImageView
    self.cameraPreview.image = imageData.image            

    self.faceDetection.isHidden = imageData.roi.isNull

    if (!imageData.roi.isNull) {
        // If the roi is not null, the face detection graphics is added to the view
        self.faceDetection.frame = imageData.roi.adjusted(from: imageData.image, to: self.cameraPreview)
        self.faceDetection.isHidden = false
        self.faceDetection.backgroundColor = .clear
    }
}

Info

For a complete example of a camera preview, please refer to the sample app code.