Skip to main content

Classic UI

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

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 a free trial license to test the Scanbot SDK thoroughly.

Get your free Trial License

What do you think of this documentation?