Skip to main content

Scanning Workflow | iOS Document Scanner

The Scanbot SDK can process and combine various scanning scenarios via the Scanning Workflow component. It is represented by SBSDKUIWorkflow which can combine document scanning with QR code detection or machine-readable zone recognition, for example. Many steps can be run in a UIImage or CMSampleBufferRef, so a step can either be a live-detection or a still-image capturing step. You can validate each workflow step separately in a SBSDKUIWorkflowStepValidationHandler block or at the end when all the steps have finished in a single SBSDKUIWorkflowValidationHandler block and then, if the validation failed, restart the whole workflow.

The creation of custom steps is possible by subclassing the SBSDKUIWorkflowStep.

You can integrate the component into the application using:

Usage of the Ready to use UI component:#

The main class of the ready-to-use UI component is SBSDKUIWorkflowScannerViewController.

Usually, this view controller is used as a separate screen for processing SBSDKUIWorkflow.

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

import UIKitimport ScanbotSDK;
class ScanningWorkflowUISwiftViewController: 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 = SBSDKUIWorkflowScannerConfiguration.default()
        // Behavior configuration:        // e.g. enable a beep sound on successful detection.        configuration.behaviorConfiguration.isSuccessBeepEnabled = true
        // UI configuration:        // e.g. configure various colors.        configuration.uiConfiguration.topBarBackgroundColor = UIColor.red        configuration.uiConfiguration.topBarButtonsActiveColor = UIColor.white        configuration.uiConfiguration.topBarButtonsInactiveColor = UIColor.white.withAlphaComponent(0.3)
        // Text configuration:        // e.g. customize a UI element's text.        configuration.textConfiguration.cancelButtonTitle = "Cancel"
        // Create an array with the accepted aspect ratios        // e.g. aspect ratios for a portrait/landscape A4 page.        let portraitRatios = [SBSDKAspectRatio(width: 1.0, andHeight: 1.414)]        let landscapeRatios = [SBSDKAspectRatio(width: 1.414, andHeight: 1.0)]
        // Create a document workflow step using the portrait aspect ratio.        let portraitStep = SBSDKUIScanDocumentPageWorkflowStep(title: "Black & White Document 1/2",                                                               message: "Please scan a PORTRAIT A4 document.",                                                               requiredAspectRatios: portraitRatios,                                                               pagePostProcessing: { page in                                                                // Apply the Black & White filter to the document.                                                                page.filter = SBSDKImageFilterTypeBlackAndWhite                                                               }, resultValidation: nil)
        // Create a document workflow step using the landscape aspect ratio.        let landscapeStep = SBSDKUIScanDocumentPageWorkflowStep(title: "Black & White Document 2/2",                                                               message: "Please scan a LANDSCAPE A4 document.",                                                               requiredAspectRatios: landscapeRatios,                                                               pagePostProcessing: { page in                                                                // Apply the Black & White filter to the document.                                                                page.filter = SBSDKImageFilterTypeBlackAndWhite                                                               }, resultValidation: nil)
        // Create a workflow using the document workflow steps.        let workflow = SBSDKUIWorkflow(steps: [portraitStep, landscapeStep],                                       name: "2-page, black and white document",                                       validationHandler: nil)
        // Present the recognizer view controller modally on this view controller.        SBSDKUIWorkflowScannerViewController.present(on: self,                                                     workflow: workflow!,                                                     configuration: configuration,                                                     delegate: self)    }}
extension ScanningWorkflowUISwiftViewController: SBSDKUIWorkflowScannerViewControllerDelegate {    func workflowScanViewController(_ viewController: SBSDKUIWorkflowScannerViewController,                                    didFinish workflow: SBSDKUIWorkflow,                                    with results: [SBSDKUIWorkflowStepResult]) {        // Process the results of the workflow.    }}