Skip to main content

Biometric Capture

Implementing the biometric capture session in a view-based UI

This example shows how to integrate the Biometric Capture Session into your project. The Biometric Capture Session extracts a frame collection from the camera preview.

Add the Capture Session .xcframework inside the project. Link to guide on how to integrate the library inside the project https://www.simpleswiftguide.com/how-to-add-xcframework-to-xcode-project

Integration of Biometric capture

Capture session can be configured by changing values for MBCaptureSessionOptions

Set auto capturing

  • Tells whether the capture is automatic or manual:
    • Automatic
      • Automatically take an image with a specific time that you can configure with timeBeforeAutomaticCapture
    • Manual
      • Manually take an image with the help of a button that the library shows

Set number of frame to collect

  • Describes the number of frames to collect during the capture session.

Set time before automaticCapture

  • timeBeforeAutomaticCapture
    • Number of seconds that the user needs to wait in automatic capture

Set face quality enabled

  • Flag to change capture quality

Set camera quality

  • Set the options for the camera.

Enable debugging

  • If it is set to false will just display the overlay on the top of the camera. If it is set to true will display all components available ont the top of the camera(timer text, progress bar and face status text).

Enable payload optimization

  • Used to enable or disable optimization of the payload size generated by the capture session.

Set capture constraints type

Capture constraints are used to ensure to capture high quality data during the capture process.

  • You can select between to types of constraints: V1, V2
    • V1 uses a limited set of constants
    • V2 is more accurate and will give the best performance of capturing data

The listener to use for validation events changes depending on the selected constraint type. See Validating events for more details

In this version we have two options but in the future V1 will be deprecated, and we will recommend to use V2

Change target resolution

  • Option to change the resolution of the camera:
    • hd1280x720(default option)
    • hd1920x1080
    • hd4K3840x2160
    • qHD960x540

Change scale type

  • Fill: Resize the video preview to fill the layer and keep the aspect ratio.
  • Fit: Resize the video preview to fit inside the layer and keep the aspect ratio.

Change camera position

  • Option to select the side of the camera:
    • Front
    • Back

Device orientation

Our SDK supports capturing in landscape and portrait. To use the capture on multi orientations the client should enable respective device orientation on target app.

Setup user interface

Create your own UI

After finishing the import of .xcframework inside the project, you can import the library inside the swift file.

import MobaiBiometric

In the UI for this example app, we use the following:

Create view.

private var mbCaptureSessionView: MBCaptureSessionView

Initialisation view with MBCaptureSessionOptions.

public init() {
self.options = MBCaptureSessionOptions()
self.mbCaptureSessionView = MBCaptureSessionView(options: self.options)
}

Setup View Constraints.

view.addSubview(takePictureButton)

NSLayoutConstraint.activate([
mbCaptureSessionView.topAnchor.constraint(equalTo: view.topAnchor),
mbCaptureSessionView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
mbCaptureSessionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
mbCaptureSessionView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])

Starts Camera if camera permissions are granted.

public override func viewWillAppear(_ animated: Bool) {
mbCaptureSessionView.onStartCapturing()
}

Stop Capturing.

public override func viewWillDisappear(_ animated: Bool) {
mbCaptureSessionView.onStopCapturing()
}

Adding delegate and implementing the delegate inside View Controller.

mbCaptureSessionView.captureSessionDelegate = self
mbCaptureSessionView.countDownDelegate = self
mbCaptureSessionView.validatingDelegate = self
mbCaptureSessionView.progressDelegate = self

extension ExampleViewController: MBCaptureSessionDelegate, MBOnValidatingDelegate, MBCountDownDelegate, MBCaptureProgressDelegate { }

Use our UI

Initialization and calling present of MBCaptureSessionViewController:

let viewController = MBCaptureSessionViewController(options: .init())
viewController.delegate = self
self.present(viewController, animated: true, completion: nil)

Configure

Enable to show progress bar in UI

  • Is used to show or hide the progress bar. The progress bar indicates to the user how far in the capture process we are.

Enable to show face status in UI

  • Is used to show or hide the face status label. The face status label is used to indicate to the user if the face is in the correct position or not.

Enable to show count down timer in UI

  • Is used to show or hide the label displayed during countdown before capture starts. The text for the countdownLabel can be set using the countdownLabelText option.

Enable to show dismiss button when is presented in UI

  • Option to add or remove the dismiss button at the top (This works only with the ViewController approach).

Enable to show overlay face view in UI

  • Indicates whether the presented face capture overlay. Default is true.

Localizations:

Variable NameTypeDefault Value
countdownLabelTextstring'Hold Still'
faceStatusTextsMBFaceStatusTextsMBFaceStatusTexts()
cameraPermissionAlertMBCameraPermissionAlertMBCameraPermissionAlert()
  • countdownLabelText

    • Is used to customize the text shown in the countdown label.
  • faceStatusTexts

    • Is used to customize or localize the text displayed to the user indicating if the face is in correct position or not. If no input is provided to faceStatusTexts default values are used.
  • cameraPermissionAlert

    • Is the Alert that show to user when is requiring permissions.

MBFaceStatusTexts

Is object to customize or localize the text displayed to the user indicating if the face is in correct position or not. If no input is provided to faceStatusTexts default values are used.

Variable NameTypeDefault Value
faceTooFarAwaystring'Face too far away'
faceTooFarUpstring'Face too far up'
faceTooFarDownstring'Face too far down'
faceTooFarLeftstring'Face too far left'
faceTooFarRightstring'Face too far right'
faceTooClosestring'Face too close'
faceNotFoundstring'Face not found'
tooManyFacesstring'Too many faces'
validFacestring'Valid faceaway'

MBCameraPermissionAlert

Is object to customize text of Alert that show to user when is requiring permissions.

Variable NameTypeDefault Value
titlestring'To continue the app need access for settings!'
messagestring'Go to Settings?'
settingTextstring'Settings'
cancelTextstring'Cancel'

Events for your own UI

Success event

MBCaptureSessionDelegate is executed for MBCaptureSession when the capture session is finished or aborted.

onSuccess is executed for MBCaptureSession class when the capture session is successfully finished.

@param result contains a list of frames.

func onSuccess(result: MBCaptureSessionResult)

Failure event

onFailure is executed for MBCaptureSessionService class when the camera do not succeed.

@param error describes whether there is a camera or face failure.

func onFailure(error: MBCaptureSessionError)

Capture state event

onStateChanged is executed when the state is changes.

@param stateEnum is status enum of the state changed like onInitializing, validating, countdown, onCaptureStarted, onProcessing, onCaptureFinished.

func onStateChanged(captureState: MBCaptureState)

Countdown event

MBCountDownDelegate listen for the count-down new values.

onCountdown is executed every second before the engine start collecting frames. The time limit is set in the options.

@param timeCounter is the current time in seconds.

func onCountDown(timeCounter: Int)

Validating events

Capture constraints v1

MBOnValidatingDelegate is executed when the status of the face changes.

@param faceStatus describes the face location on the camera preview.

func onValidating(_ faceStatus: DetectedFaceStatus)
Capture constraints v2

MBOnValidatingDelegate ones you enable v2 inside capture constraints this delegate will be triggered.

@param faceStatus is an object that describes if the face is valid in regard to the constraints set for the face capture process.

func onValidating(_ faceStatus: Result<FaceGeometryModel, MBFaceBoundingBoxStatus>)

Capture progress event

onCaptureProgress is executed every time the valid frames collecting is progressing.

@param captureProgressCounter represent the value from 0 to 1 of the collecting frames progress.

func onCaptureProgress(captureProgressCounter: Float)

Events for our UI

Success event

onSuccess is executed for MBCaptureSession class when the capture session is successfully finished.

@param result contains a list of frames.

func onSuccess(result: MBCaptureSessionResult) { }

Failure event

onFailure is executed for MBCaptureSessionService class when the camera do not succeed.

@param error describes whether there is a camera or face.

func onFailure(error: MBCaptureSessionError) { }

State changed event

onStateChanged is executed when the state is changes.

@param stateEnum is status enum of the state changed like onInitializing, validating, countdown, onCaptureStarted, onProcessing and onCaptureFinished.

func onStateChanged(stateEnum: MBCaptureState)

Presented dismiss event

onPresentedDismissTapped is executed when the user close the screen from the X button.

func onPresentedDismissTapped() { }

If you have any questions, please contact. .....