Skip to main content

Classic UI | iOS Barcode Scanner

Barcode Scanner UI

The main class of the Classic 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 classic component does not display results, instead it just forwards them to the delegate.

Example for integrating the Barcode Scanner Classic UI Component

import UIKit
import ScanbotBarcodeScannerSDK

// 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,
delegate: self)

// Get current view finder configuration object
let config = self.scannerViewController.viewFinderConfiguration

// Enable the view finder.
config.isViewFinderEnabled = true

// Set the finder's aspect ratio.
config.aspectRatio = SBSDKAspectRatio(width: 2, height: 1)

// Set the finder's minimum insets.
config.minimumInset = UIEdgeInsets(top: 100, left: 50, bottom: 100, right: 50)

// Configure the view finder colors and line properties.
config.lineColor = UIColor.red
config.backgroundColor = UIColor.red.withAlphaComponent(0.1)
config.lineWidth = 2
config.lineCornerRadius = 8

// Set the view finder configuration to apply it.
self.scannerViewController.viewFinderConfiguration = config

// Get current energy configuration.
let energyConfig = self.scannerViewController.energyConfiguration

// Set detection rate.
energyConfig.detectionRate = 5

// Set the energy configuration to apply it.
self.scannerViewController.energyConfiguration = energyConfig

// Define and set barcode types that should be accepted by the scanner.
let commonTypes = SBSDKBarcodeType.commonTypes
self.scannerViewController.acceptedBarcodeTypes = commonTypes
}
}

// The implementation of SBSDKBarcodeScannerViewControllerDelegate.
extension BarcodeScannerSwiftViewController: SBSDKBarcodeScannerViewControllerDelegate {

// Implement this function to process 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
}
}

Batch Barcode Scanner UI

The Scanbot Barcode Scanner 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.

Example for integrating the Batch Barcode Scanner classic UI components

import UIKit
import ScanbotBarcodeScannerSDK

// 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!

// Property to indicate whether you want 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,
delegate: self)

// Get current view finder configuration object
let config = self.scannerViewController.viewFinderConfiguration

// Enable the view finder.
config.isViewFinderEnabled = true

// Set the finder's aspect ratio.
config.aspectRatio = SBSDKAspectRatio(width: 2, height: 1)

// Set the finder's minimum insets.
config.minimumInset = UIEdgeInsets(top: 100, left: 50, bottom: 100, right: 50)

// Configure the view finder colors and line properties.
config.lineColor = UIColor.red
config.backgroundColor = UIColor.red.withAlphaComponent(0.1)
config.lineWidth = 2
config.lineCornerRadius = 8

// Set the view finder configuration to apply it.
self.scannerViewController.viewFinderConfiguration = config

// Get current energy configuration.
let energyConfig = self.scannerViewController.energyConfiguration

// Set detection rate.
energyConfig.detectionRate = 5

// Set the energy configuration to apply it.
self.scannerViewController.energyConfiguration = energyConfig

// Define and set barcode types that should be accepted by the scanner.
let commonTypes = SBSDKBarcodeType.commonTypes
self.scannerViewController.acceptedBarcodeTypes = commonTypes
}
}

// The implementation of the SBSDKBarcodeScannerViewControllerDelegate.
extension BarcodesBatchSwiftViewController: SBSDKBarcodeScannerViewControllerDelegate {

// Implement this function to process 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 results)
func barcodeScannerControllerShouldDetectBarcodes(_ controller: SBSDKBarcodeScannerViewController) -> Bool {
return self.shouldDetectBarcodes
}
}

Data parsers

Barcodes, especially two-dimensional ones (e.g. Data Matrix and QR codes) are often used to encode structured data or documents. This structured data can be parsed into document-like data structures that let you access the data fields conveniently, for example for bank transfer details, medical plans, boarding passes, medical certificates, and vCard addresses.

Barcodes, especially the two-dimensional ones, e.g. data matrices and QR codes, are often used to encode structured data or documents. This structured data can be parsed into document-like data structures that let you access the data fields conveniently, for example Swiss QR-Codes, SEPA forms, medical plans, boarding passes, medical certificates and vCard addresses.

The following data parsers are currently supported:

  • AAMVA: Parse the AAMVA data format from PDF-417 barcodes on US driver’s licenses.
  • Boarding pass data from PDF-417 barcodes.
  • Parser for German Medical Certificates (aka. Disability Certificate or AU-Bescheinigung) coded in a PDF-417 barcode.
  • GS1 encoded data from barcodes.
  • Data from PDF-417 barcodes on ID Cards.
  • Parse and extract data from XML of Data Matrix barcodes on Medical Plans (German Medikationsplan).
  • Data parser of QR-Code values printed on SEPA pay forms.
  • vCard data from a QR-Code (e.g. on business cards).
  • Swiss QR data from a QR-Code for easy, automatic and efficient payments.

Classic UI components

This example shows the usage of the Swiss QR code data parser with classic UI components:

import UIKit
import ScanbotBarcodeScannerSDK

// 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!

override func viewDidLoad() {
super.viewDidLoad()

// Create the SBSDKBarcodeScannerViewController instance.
self.scannerViewController = SBSDKBarcodeScannerViewController(parentViewController: self,
parentView: self.view,
delegate: self)

// Get current view finder configuration object
let config = self.scannerViewController.viewFinderConfiguration

// Enable the view finder.
config.isViewFinderEnabled = true

// Set the finder's aspect ratio.
config.aspectRatio = SBSDKAspectRatio(width: 2, height: 1)

// Set the finder's minimum insets.
config.minimumInset = UIEdgeInsets(top: 100, left: 50, bottom: 100, right: 50)

// Configure the view finder colors and line properties.
config.lineColor = UIColor.red
config.backgroundColor = UIColor.red.withAlphaComponent(0.1)
config.lineWidth = 2
config.lineCornerRadius = 8

// Set the view finder configuration to apply it.
self.scannerViewController.viewFinderConfiguration = config

// Get current energy configuration.
let energyConfig = self.scannerViewController.energyConfiguration

// Set detection rate.
energyConfig.detectionRate = 5

// Set the energy configuration to apply it.
self.scannerViewController.energyConfiguration = energyConfig

// Define and set barcode types that should be accepted by the scanner.
let commonTypes = SBSDKBarcodeType.commonTypes
self.scannerViewController.acceptedBarcodeTypes = commonTypes

// Set Swiss QR as an accepted document type.
self.scannerViewController.acceptedDocumentTypes = [.swissQR]
}
}

// The implementation of SBSDKBarcodeScannerViewControllerDelegate.
extension BarcodeScannerSwiftViewController: SBSDKBarcodeScannerViewControllerDelegate {

// Implement this function to process detected barcodes.
func barcodeScannerController(_ controller: SBSDKBarcodeScannerViewController,
didDetectBarcodes codes: [SBSDKBarcodeScannerResult]) {
// Process the detected barcodes.
if let document = codes.first?.formattedResult as? SBSDKSwissQRCodeDocumentFormat {

// Enumerate the Swiss QR code data fields.
for field in document.fields {
// Do something with the fields.
}
}
}

// Implement this function when you need to pause the detection (e.g. when showing the results).
func barcodeScannerControllerShouldDetectBarcodes(_ controller: SBSDKBarcodeScannerViewController) -> Bool {
return true
}
}

Manual data parsing

Besides scanning barcodes and parsing the results, it is also possible to parse any given string containing structured data using the SBSDKBarcodeDocumentParser class.

Example for manually parsing the data

import ScanbotBarcodeScannerSDK

// Some barcode raw string.
let rawBarcodeString = "..."

// Instantiate the parser.
let parser = SBSDKBarcodeDocumentParser()

// Run the parser and check the result.
if let document = parser.parseDocument(inputString: rawBarcodeString) as? SBSDKSwissQRCodeDocumentFormat {
// Enumerate the Swiss QR code data fields.
for field in document.fields {
// Do something with the fields.
}
}


Want to scan longer than one minute?

Generate your free "no-strings-attached" Trial License and properly test the Scanbot SDK.

Get your free Trial License

What do you think of this documentation?