Skip to main content

Classic UI | iOS Document 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.

danger

Please be aware that processing really large images may lead to out-of-memory crashes on iOS. Since this limit is highly dynamic and untransparent in iOS, depending on the device, your app's current memory usage, the other running apps, the iOS version, the system configuration and many other unknown variables, we did not add a hard limit for image sizes.

As a general rule of thumb, it is quite safe to assume that processing images of the resolution the camera can shoot will most likely not crash your app. On most modern iOS devices this currently translates to 12 megapixels. But even much larger images may work without crashing as well.

Thus we cannot take responsibility for out-of-memory crashes when dealing with very high-resolution images. It is your responsibility, as an app developer, to properly manage the handling of large images and keep the memory footprint of your app as small as possible.

In case you have any questions on this topic, please reach out to our support team.

caution

Please do not use multiple scanners at the same time. For example, do not combine generic document scanner, health insurance scanner, text data scanner, etc. at the same time! Each scanner instance requires a lot of memory, GPU, and processor resources. Using multiple scanners will lead to performance issues for the entire application.

Example for integrating the Barcode Scanner Classic UI Component

import UIKit
import 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,
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 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 Component

import UIKit
import 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!

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

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 ScanbotSDK

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