Skip to main content

Barcode and QR-Code Scanner | iOS Document Scanner

The Scanbot SDK provides the ability to search and decode multiple types of barcodes in a UIImage or CMSampleBufferRef. The result is encapsulated in an array of SBSDKBarcodeScannerResult instances. The Scanbot SDK supports the following types of barcodes:

1D barcodes:

  • EAN_13
  • EAN_8
  • UPC_A
  • UPC_E
  • CODE_39
  • CODE_93
  • CODE_128
  • ITF (Interleaved 2 of 5)
  • MSI_PLESSEY

2D barcodes:

  • QR_CODE
  • DATA_MATRIX
  • AZTEC
  • PDF_417

To provide better detection results the Scanbot SDK supports the ability to accumulate multiple frames before running detection. In this case, the barcode scanner will return empty results until the frames have been called a given number of times. Then the barcode scanner will perform detection on the frame with the least amount of blur. This feature is intended for use with live detection.

Barcode Scanner UI#

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

Classical UI Component#

The main class of the Classical UI component is SBSDKBarcodeScannerViewController.

Usually this view controller is embedded as a child view controller into another view controller, the parent view controller. The parent view controller usually acts as the delegate and processes the recognition results. You still have full control over the UI elements and can add additional views and buttons to your view controller. The classical component does not display results, instead it just forwards them to the delegate.

import UIKitimport ScanbotSDK
// This is a simple, empty view controller which acts as a container and delegate for the SBSDKBarcodeScannerViewController.class BarcodeScannerSwiftViewController: UIViewController {
    // The instance of the scanner view controller.    var scannerViewController: SBSDKBarcodeScannerViewController?
    // The variable to indicate whether you want the scanner to detect barcodes or not.    var shouldDetectBarcodes = false
    override func viewDidLoad() {        super.viewDidLoad()
        // Create the SBSDKBarcodeScannerViewController instance.        self.scannerViewController = SBSDKBarcodeScannerViewController(parentViewController: self,                                                                       parentView: self.view)
        // Define a finder view:        // set the color and width of the finder line,        self.scannerViewController?.viewFinderLineColor = UIColor.green        self.scannerViewController?.viewFinderLineWidth = 5
        // set the finder's aspect ratio,        self.scannerViewController?.finderAspectRatio = SBSDKAspectRatio(width: 1, andHeight: 0.5)
        // set the finder's minimum insets,        self.scannerViewController?.finderMinimumInset = UIEdgeInsets(top: 100, left: 50, bottom: 100, right: 50)
        // enable the finder view.        self.scannerViewController?.shouldUseFinderFrame = true
        // Set the detection rate.        self.scannerViewController?.detectionRate = 5
        // Define and set the barcode types that should be accepted by the scanner.        let commonTypes = SBSDKBarcodeType.commonTypes()        self.scannerViewController?.acceptedBarcodeTypes = commonTypes    }
    override func viewDidAppear(_ animated: Bool) {        super.viewDidAppear(animated)
        // Start detecting barcodes when the screen is visible.        self.shouldDetectBarcodes = true    }
    override func viewWillDisappear(_ animated: Bool) {        super.viewWillDisappear(animated)
        // Stop detecting barcodes when the screen is not visible.        self.shouldDetectBarcodes = false    }
}
// The implementation of SBSDKBarcodeScannerViewControllerDelegate.extension BarcodeScannerSwiftViewController: SBSDKBarcodeScannerViewControllerDelegate {
    // Implement this function to process the detected barcodes.    func barcodeScannerController(_ controller: SBSDKBarcodeScannerViewController,                                  didDetectBarcodes codes: [SBSDKBarcodeScannerResult]) {        // Process the detected barcodes.    }
    // Implement this function when you need to pause the detection (e.g. when showing the results).    func barcodeScannerControllerShouldDetectBarcodes(_ controller: SBSDKBarcodeScannerViewController) -> Bool {        return self.shouldDetectBarcodes    }}

Ready-To-Use UI Component#

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

Usually this view controller is used as a separate screen for scanning multiple barcodes 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 SBSDKUIBarcodeScannerConfiguration to customize it in a variety of ways, such as colors, texts and behavior.

import UIKitimport ScanbotSDK
class BarcodeScannerUISwiftViewController: 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 = SBSDKUIBarcodeScannerConfiguration.default()
        // Behavior configuration:        // e.g. set minimum text length.        // **NOTE:** Currently works for ITF and MSI Plessey barcodes only.        configuration.behaviorConfiguration.additionalParameters.minimumTextLength = 3
        // UI Configuration:        // e.g. configure various colors.        configuration.uiConfiguration.topBarBackgroundColor = UIColor.red        configuration.uiConfiguration.topBarButtonsColor = UIColor.white
        // Text configuration:        // e.g. customize a UI element's text.        configuration.textConfiguration.cancelButtonTitle = "Cancel"
        // Create an array of accepted barcode types.        let acceptedMachineCodeTypes = SBSDKUIMachineCodesCollection.twoDimensionalBarcodes()
        // Present the recognizer view controller modally on this view controller.        SBSDKUIBarcodeScannerViewController.present(on: self,                                                    withAcceptedMachineCodeTypes: acceptedMachineCodeTypes,                                                    configuration: configuration,                                                    andDelegate: self)    }}
extension BarcodeScannerUISwiftViewController: SBSDKUIBarcodeScannerViewControllerDelegate {    func qrBarcodeDetectionViewController(_ viewController: SBSDKUIBarcodeScannerViewController,                                          didDetect barcodeResults: [SBSDKBarcodeScannerResult]) {        // Process the detected results.    }}

Batch Barcode Scanner UI#

The Scanbot SDK provides the ability to search and decode multiple instances of different types of barcodes in a UIImage or CMSampleBufferRef. The result is encapsulated in an array of SBSDKBarcodeScannerResult instances.

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

Classical UI Component#

The main class of the Classical UI component is SBSDKBarcodeScannerViewController.

Usually this view controller is embedded as a child view controller into another view controller, the parent view controller. The parent view controller usually acts as the delegate and processes the recognition results. You still have full control over the UI elements and can add additional views and buttons to your view controller. The classical component does not display results, instead it just forwards them to the delegate.

import UIKitimport ScanbotSDK
// This is a simple, empty view controller which acts as a container and delegate for the SBSDKBarcodeScannerViewController.class BarcodesBatchSwiftViewController: UIViewController {
    // The instance of the scanner view controller.    var scannerViewController: SBSDKBarcodeScannerViewController?
    // A property to indicate whether you want the scanner to detect barcodes or not.    var shouldDetectBarcodes = false
    override func viewDidLoad() {        super.viewDidLoad()
        // Create the SBSDKBarcodeScannerViewController instance        self.scannerViewController = SBSDKBarcodeScannerViewController(parentViewController: self,                                                                       parentView: self.view)
        // Define a finder view:        // Set the color and width of the finder line,        self.scannerViewController?.viewFinderLineColor = UIColor.green        self.scannerViewController?.viewFinderLineWidth = 5
        // set the finder's aspect ratio,        self.scannerViewController?.finderAspectRatio = SBSDKAspectRatio(width: 1, andHeight: 0.5)
        // set the finder's minimum insets,        self.scannerViewController?.finderMinimumInset = UIEdgeInsets(top: 100, left: 50, bottom: 100, right: 50)
        // enable the finder view.        self.scannerViewController?.shouldUseFinderFrame = true
        // Set the detection rate.        self.scannerViewController?.detectionRate = 5
        // Define and set the barcode types that should be accepted by the scanner.        let commonTypes = SBSDKBarcodeType.commonTypes()        self.scannerViewController?.acceptedBarcodeTypes = commonTypes    }
    override func viewDidAppear(_ animated: Bool) {        super.viewDidAppear(animated)
        // Start detecting barcodes when the screen is visible.        self.shouldDetectBarcodes = true    }
    override func viewWillDisappear(_ animated: Bool) {        super.viewWillDisappear(animated)
        // Stop detecting barcodes when the screen is not visible.        self.shouldDetectBarcodes = false    }}
// The implementation of the SBSDKBarcodeScannerViewControllerDelegate.extension BarcodesBatchSwiftViewController: SBSDKBarcodeScannerViewControllerDelegate {
    // Implement this function to process the detected barcodes.    func barcodeScannerController(_ controller: SBSDKBarcodeScannerViewController,                                  didDetectBarcodes codes: [SBSDKBarcodeScannerResult]) {        // Process the detected barcodes.    }
    // Implement this function when you need to pause the detection (e.g. when showing the results).    func barcodeScannerControllerShouldDetectBarcodes(_ controller: SBSDKBarcodeScannerViewController) -> Bool {        return self.shouldDetectBarcodes    }}

Ready-To-Use UI Component#

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

Usually this view controller is used as a separate screen for scanning multiple barcodes in a UIImage or CMSampleBufferRef. It displays the recognition results in an expandable table view. Once you are happy with the results, press the submit button and the recognizer view controller is dismissed and passes the results to its delegate.

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

import Foundationimport ScanbotSDK
// This is a simple, empty view controller which acts as a container and delegate for the SBSDKBarcodeScannerViewController.class BarcodesBatchScannerUISwiftViewController: 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 = SBSDKUIBarcodesBatchScannerConfiguration.default()
        // Behavior configuration:        // e.g. set minimum text length.        // **NOTE:** Currently works for ITF and MSI Plessey barcodes only.        configuration.behaviorConfiguration.additionalDetectionParameters.minimumTextLength = 3
        // UI Configuration:        // e.g. configure various colors.        configuration.uiConfiguration.topBarBackgroundColor = UIColor.red        configuration.uiConfiguration.topBarButtonsColor = UIColor.white        configuration.uiConfiguration.topBarButtonsInactiveColor = UIColor.white.withAlphaComponent(0.3)
        // Create an array of accepted barcode types.        let acceptedMachineCodeTypes = SBSDKUIMachineCodesCollection.oneDimensionalBarcodes()
        // Present the recognizer view controller modally on this view controller.        SBSDKUIBarcodesBatchScannerViewController.present(on: self,                                                          withAcceptedMachineCodeTypes: acceptedMachineCodeTypes,                                                          configuration: configuration,                                                          andDelegate: self)    }}
extension BarcodesBatchScannerUISwiftViewController: SBSDKUIBarcodesBatchScannerViewControllerDelegate {    func barcodesBatchScannerViewController(_ viewController: SBSDKUIBarcodesBatchScannerViewController,                                            didFinishWith barcodeResults: [SBSDKUIBarcodeMappedResult]) {        // Process the detected results.    }}

Swiss QR-Code#

The Scanbot SDK provides the ability to search and decode Swiss QR-Codes in a UIImage or CMSampleBufferRef. To enable this feature specify the document type during barcode scanner initialization.

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

Usage of the Classical UI component:#

import UIKitimport ScanbotSDK
// This is a simple, empty view controller which acts as a container and delegate for the SBSDKBarcodeScannerViewController.class BarcodeScannerSwiftViewController: UIViewController {
    // The instance of the scanner view controller.    var scannerViewController: SBSDKBarcodeScannerViewController?
    // The variable to indicate whether you want the scanner to detect barcodes or not.    var shouldDetectBarcodes = false
    override func viewDidLoad() {        super.viewDidLoad()
        // Create the SBSDKBarcodeScannerViewController instance.        self.scannerViewController = SBSDKBarcodeScannerViewController(parentViewController: self,                                                                       parentView: self.view)
        // Define a finder view:        // set the color and width of the finder line,        self.scannerViewController?.viewFinderLineColor = UIColor.green        self.scannerViewController?.viewFinderLineWidth = 5
        // set the finder's aspect ratio,        self.scannerViewController?.finderAspectRatio = SBSDKAspectRatio(width: 1, andHeight: 0.5)
        // set the finder's minimum insets,        self.scannerViewController?.finderMinimumInset = UIEdgeInsets(top: 100, left: 50, bottom: 100, right: 50)
        // enable the finder view.        self.scannerViewController?.shouldUseFinderFrame = true
        // Set the detection rate.        self.scannerViewController?.detectionRate = 5
        // Define and set the barcode document types that should be accepted by the scanner.        let acceptedBarcodeDocumentTypes = [SBSDKBarcodeDocumentTypeSwissQR]        self.scannerViewController?.acceptedDocumentTypes = acceptedBarcodeDocumentTypes    }
    override func viewDidAppear(_ animated: Bool) {        super.viewDidAppear(animated)
        // Start detecting barcodes when the screen is visible.        self.shouldDetectBarcodes = true    }
    override func viewWillDisappear(_ animated: Bool) {        super.viewWillDisappear(animated)
        // Stop detecting barcodes when the screen is not visible.        self.shouldDetectBarcodes = false    }
}
// The implementation of SBSDKBarcodeScannerViewControllerDelegate.extension BarcodeScannerSwiftViewController: SBSDKBarcodeScannerViewControllerDelegate {
    // Implement this function to process the detected barcodes.    func barcodeScannerController(_ controller: SBSDKBarcodeScannerViewController,                                  didDetectBarcodes codes: [SBSDKBarcodeScannerResult]) {        // Process the detected barcodes.    }
    // Implement this function when you need to pause the detection (e.g. when showing the results).    func barcodeScannerControllerShouldDetectBarcodes(_ controller: SBSDKBarcodeScannerViewController) -> Bool {        return self.shouldDetectBarcodes    }}

Usage of the Ready to use UI component:#

import UIKitimport ScanbotSDK
class BarcodeScannerUISwiftViewController: 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 = SBSDKUIBarcodeScannerConfiguration.default()
        // Behavior configuration:        // e.g. turn on the flashlight.        configuration.behaviorConfiguration.isFlashEnabled = true
        // UI Configuration:        // e.g. configure various colors.        configuration.uiConfiguration.topBarBackgroundColor = UIColor.red        configuration.uiConfiguration.topBarButtonsColor = UIColor.white
        // Text configuration:        // e.g. customize a UI element's text.        configuration.textConfiguration.cancelButtonTitle = "Cancel"
        // Create an array of accepted barcode document types.        let acceptedBarcodeDocumentTypes = [SBSDKBarcodeDocumentTypeSwissQR]
        // Present the recognizer view controller modally on this view controller.        SBSDKUIBarcodeScannerViewController.present(on: self,                                                    withAcceptedMachineCodeTypes: nil,                                                    withAcceptedDocumentTypes: acceptedBarcodeDocumentTypes,                                                    configuration: configuration,                                                    andDelegate: self)    }}
extension BarcodeScannerUISwiftViewController: SBSDKUIBarcodeScannerViewControllerDelegate {    func qrBarcodeDetectionViewController(_ viewController: SBSDKUIBarcodeScannerViewController,                                          didDetect barcodeResults: [SBSDKBarcodeScannerResult]) {        // Process the detected results.    }}

SEPA QR-Code#

The Scanbot SDK provides the ability to search and decode SEPA QR-Codes in a UIImage or CMSampleBufferRef. To enable this feature specify the document type during barcode scanner initialization.

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

Usage of the Classical UI component:#

import UIKitimport ScanbotSDK
// This is a simple, empty view controller which acts as a container and delegate for the SBSDKBarcodeScannerViewController.class BarcodeScannerSwiftViewController: UIViewController {
    // The instance of the scanner view controller.    var scannerViewController: SBSDKBarcodeScannerViewController?
    // The variable to indicate whether you want the scanner to detect barcodes or not.    var shouldDetectBarcodes = false
    override func viewDidLoad() {        super.viewDidLoad()
        // Create the SBSDKBarcodeScannerViewController instance.        self.scannerViewController = SBSDKBarcodeScannerViewController(parentViewController: self,                                                                       parentView: self.view)
        // Define a finder view:        // set the color and width of the finder line,        self.scannerViewController?.viewFinderLineColor = UIColor.green        self.scannerViewController?.viewFinderLineWidth = 5
        // set the finder's aspect ratio,        self.scannerViewController?.finderAspectRatio = SBSDKAspectRatio(width: 1, andHeight: 0.5)
        // set the finder's minimum insets,        self.scannerViewController?.finderMinimumInset = UIEdgeInsets(top: 100, left: 50, bottom: 100, right: 50)
        // enable the finder view.        self.scannerViewController?.shouldUseFinderFrame = true
        // Set the detection rate.        self.scannerViewController?.detectionRate = 5
        // Define and set the barcode document types that should be accepted by the scanner.        let acceptedBarcodeDocumentTypes = [SBSDKBarcodeDocumentTypeSEPA]        self.scannerViewController?.acceptedDocumentTypes = acceptedBarcodeDocumentTypes    }
    override func viewDidAppear(_ animated: Bool) {        super.viewDidAppear(animated)
        // Start detecting barcodes when the screen is visible.        self.shouldDetectBarcodes = true    }
    override func viewWillDisappear(_ animated: Bool) {        super.viewWillDisappear(animated)
        // Stop detecting barcodes when the screen is not visible.        self.shouldDetectBarcodes = false    }
}
// The implementation of SBSDKBarcodeScannerViewControllerDelegate.extension BarcodeScannerSwiftViewController: SBSDKBarcodeScannerViewControllerDelegate {
    // Implement this function to process the detected barcodes.    func barcodeScannerController(_ controller: SBSDKBarcodeScannerViewController,                                  didDetectBarcodes codes: [SBSDKBarcodeScannerResult]) {        // Process the detected barcodes.    }
    // Implement this function when you need to pause the detection (e.g. when showing the results).    func barcodeScannerControllerShouldDetectBarcodes(_ controller: SBSDKBarcodeScannerViewController) -> Bool {        return self.shouldDetectBarcodes    }}

Usage of the Ready to use UI component:#

import UIKitimport ScanbotSDK
class BarcodeScannerUISwiftViewController: 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 = SBSDKUIBarcodeScannerConfiguration.default()
        // Behavior configuration:        // e.g. turn on the flashlight.        configuration.behaviorConfiguration.isFlashEnabled = true
        // UI Configuration:        // e.g. configure various colors.        configuration.uiConfiguration.topBarBackgroundColor = UIColor.red        configuration.uiConfiguration.topBarButtonsColor = UIColor.white
        // Text configuration:        // e.g. customize a UI element's text.        configuration.textConfiguration.cancelButtonTitle = "Cancel"
        // Create an array of accepted barcode document types.        let acceptedBarcodeDocumentTypes = [SBSDKBarcodeDocumentTypeSEPA]
        // Present the recognizer view controller modally on this view controller.        SBSDKUIBarcodeScannerViewController.present(on: self,                                                    withAcceptedMachineCodeTypes: nil,                                                    withAcceptedDocumentTypes: acceptedBarcodeDocumentTypes,                                                    configuration: configuration,                                                    andDelegate: self)    }}
extension BarcodeScannerUISwiftViewController: SBSDKUIBarcodeScannerViewControllerDelegate {    func qrBarcodeDetectionViewController(_ viewController: SBSDKUIBarcodeScannerViewController,                                          didDetect barcodeResults: [SBSDKBarcodeScannerResult]) {        // Process the detected results.    }}