Skip to main content

MRZ Scanner | iOS Document Scanner

The Scanbot SDK provides the ability to recognize machine-readable zones in a UIImage or CMSampleBufferRef. The result is encapsulated in an SBSDKMRZRecognizerResult instance.

MRZ Scanner UI#

There are two ways to integrate the component into the application:

Classical UI Component#

import UIKitimport ScanbotSDK
class MRZScannerSwiftViewController: UIViewController {
    // The instance of the camera session for the MRZ recognition feature.    private let cameraSession = SBSDKCameraSession(for: FeatureMRZRecognition)
    // The instance of the MRZ recognizer.    private let mrzRecognizer = SBSDKMachineReadableZoneRecognizer()
    override func viewDidLoad() {        super.viewDidLoad()
        // Assign self as a video delegate for the camera session.        self.cameraSession.videoDelegate = self
        // Add the camera's preview layer as a sublayer to the view controller's view layer.        self.view.layer.addSublayer(self.cameraSession.previewLayer)    }
    override func viewWillAppear(_ animated: Bool) {        super.viewWillAppear(animated)
        // Start the camera session just before view appears to provide a smooth transition.        self.cameraSession.start()    }
    override func viewDidDisappear(_ animated: Bool) {        super.viewDidDisappear(animated)
        // Stop the camera session when view is no longer visible.        self.cameraSession.stop()    }}
// MARK: - SBSDKCameraSessionDelegateextension MRZScannerSwiftViewController: SBSDKCameraSessionDelegate {
    // Implement this function to process the camera's output.    func captureOutput(_ output: AVCaptureOutput,                       didOutput sampleBuffer: CMSampleBuffer,                       from connection: AVCaptureConnection) {
        // Recognize the MRZ code with recognizer.        let result = self.mrzRecognizer.recognizePersonalIdentity(from: sampleBuffer,                                                                  orientation: self.cameraSession.videoOrientation,                                                                  searchMachineReadableZone: false,                                                                  machineReadableZoneRect: CGRect(x: 0,                                                                                                  y: 0,                                                                                                  width: 300,                                                                                                  height: 70))        // Process the recognized result.    }}

Ready-To-Use UI Component#

The main class of the Ready-To-Use UI (RTU UI) component is SBSDKUIMRZScannerViewController.

Usually this view controller is used as a separate screen for scanning machine-readable zones in a UIImage or CMSampleBufferRef. It returns the recognition results in a delegate method.

While you don't have direct control of the actual scanner view controller you can use the SBSDKUIMRZScannerConfiguration to customize it in a variety of ways, such as colors, texts and behavior.

import UIKitimport ScanbotSDK
class MRZScannerUISwiftViewController: UIViewController {
    override func viewDidAppear(_ animated: Bool) {        super.viewDidAppear(animated)
        // Start scanning here. Usually this is an action triggered by some button or menu.        self.startScanning()    }
    func startScanning() {
        // Create the default configuration object.        let configuration = SBSDKUIMRZScannerConfiguration.default()
        // Behavior configuration:        // e.g. enable a beep sound on successful detection.        configuration.behaviorConfiguration.isSuccessBeepEnabled = true
        // UI configuration:        // e.g. configure various colors and the finder's aspect ratio.        configuration.uiConfiguration.topBarButtonsColor = UIColor.white        configuration.uiConfiguration.topBarBackgroundColor = UIColor.red        configuration.uiConfiguration.finderAspectRatio = SBSDKAspectRatio(width: 1, andHeight: 0.25)
        // Text configuration:        // e.g. customize some UI elements' text.        configuration.textConfiguration.cancelButtonTitle = "Cancel"        configuration.textConfiguration.flashButtonTitle = "Flash"
        // Present the recognizer view controller modally on this view controller.        SBSDKUIMRZScannerViewController.present(on: self, with: configuration, andDelegate: self)    }}
extension MRZScannerUISwiftViewController: SBSDKUIMRZScannerViewControllerDelegate {    func mrzDetectionViewController(_ viewController: SBSDKUIMRZScannerViewController, didDetect zone: SBSDKMachineReadableZoneRecognizerResult) {        // Process the detected result.    }}