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
- Swift
- Objective-C
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
}
}
#import "BarcodeScannerObjcViewController.h"
@import ScanbotBarcodeScannerSDK;
// This is a simple, empty view controller which acts as a container and delegate for the SBSDKBarcodeScannerViewController.
@interface BarcodeScannerObjcViewController () <SBSDKBarcodeScannerViewControllerDelegate>
// The instance of the scanner view controller.
@property (strong, nonatomic) SBSDKBarcodeScannerViewController *scannerViewController;
// The property to indicate whether you want the scanner to detect barcodes or not.
@property (nonatomic, assign) BOOL shouldDetectBarcodes;
@end
@implementation BarcodeScannerObjcViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Create the SBSDKBarcodeScannerViewController instance.
self.scannerViewController = [[SBSDKBarcodeScannerViewController alloc] initWithParentViewController:self
parentView:self.view
delegate:self];
// Get current view finder configuration object
SBSDKBaseScannerViewFinderConfiguration *config = self.scannerViewController.viewFinderConfiguration;
// Enable the view finder.
config.isViewFinderEnabled = YES;
// Set the finder's aspect ratio.
config.aspectRatio = [[SBSDKAspectRatio alloc] initWithWidth:2 andHeight:1];
// Set the finder's minimum insets.
config.minimumInset = UIEdgeInsetsMake(100, 50, 100, 50);
// Configure the view finder colors and line properties.
config.lineColor = [UIColor redColor];
config.backgroundColor = [[UIColor redColor] colorWithAlphaComponent: 0.1];
config.lineWidth = 2;
config.lineCornerRadius = 8;
// Set the view finder configuration to apply it.
self.scannerViewController.viewFinderConfiguration = config;
// Get current energy configuration.
SBSDKBaseScannerEnergyConfiguration *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.
NSArray <SBSDKBarcodeType *> *commonTypes = [SBSDKBarcodeType commonTypes];
self.scannerViewController.acceptedBarcodeTypes = commonTypes;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// Start detecting barcodes when the screen is visible.
self.shouldDetectBarcodes = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// Stop detecting barcodes when the screen is not visible.
self.shouldDetectBarcodes = NO;
}
// The implementation of SBSDKBarcodeScannerViewControllerDelegate.
// Implement this method to process detected barcodes.
- (void)barcodeScannerController:(SBSDKBarcodeScannerViewController *)controller
didDetectBarcodes:(NSArray<SBSDKBarcodeScannerResult *> *)codes {
// Process the detected barcodes.
}
// Implement this method when you need to pause the detection (e.g. when showing the results).
- (BOOL)barcodeScannerControllerShouldDetectBarcodes:(SBSDKBarcodeScannerViewController *)controller {
return self.shouldDetectBarcodes;
}
@end
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
- Swift
- Objective-C
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
}
}
#import "BarcodesBatchObjcViewController.h"
@import ScanbotBarcodeScannerSDK;
// This is a simple, empty view controller which acts as a container and delegate
// for the SBSDKBarcodeScannerViewController.
@interface BarcodesBatchObjcViewController () <SBSDKBarcodeScannerViewControllerDelegate>
// The instance of the scanner view controller.
@property (strong, nonatomic) SBSDKBarcodeScannerViewController *scannerViewController;
// Property to indicate whether you want the scanner to detect barcodes or not.
@property (nonatomic, assign) BOOL shouldDetectBarcodes;
@end
@implementation BarcodesBatchObjcViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Create the SBSDKBarcodeScannerViewController instance.
self.scannerViewController = [[SBSDKBarcodeScannerViewController alloc] initWithParentViewController:self
parentView:self.view];
// Get current view finder configuration object
SBSDKBaseScannerViewFinderConfiguration *config = self.scannerViewController.viewFinderConfiguration;
// Enable the view finder.
config.isViewFinderEnabled = YES;
// Set the finder's aspect ratio.
config.aspectRatio = [[SBSDKAspectRatio alloc] initWithWidth:2 andHeight:1];
// Set the finder's minimum insets.
config.minimumInset = UIEdgeInsetsMake(100, 50, 100, 50);
// Configure the view finder colors and line properties.
config.lineColor = [UIColor redColor];
config.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.1];
config.lineWidth = 2;
config.lineCornerRadius = 8;
// Set the view finder configuration to apply it.
self.scannerViewController.viewFinderConfiguration = config;
// Get current energy configuration.
SBSDKBaseScannerEnergyConfiguration *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.
NSArray <SBSDKBarcodeType *> *commonTypes = [SBSDKBarcodeType commonTypes];
self.scannerViewController.acceptedBarcodeTypes = commonTypes;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// Start detecting barcodes when the screen is visible.
self.shouldDetectBarcodes = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// Stop detecting barcodes when the screen is not visible.
self.shouldDetectBarcodes = NO;
}
// The implementation of the SBSDKBarcodeScannerViewControllerDelegate.
// Implement this method to process detected barcodes.
- (void)barcodeScannerController:(SBSDKBarcodeScannerViewController *)controller
didDetectBarcodes:(NSArray<SBSDKBarcodeScannerResult *> *)codes {
// Process the detected barcodes.
}
// Implement this method when you need to pause the detection (e.g. when showing the results).
- (BOOL)barcodeScannerControllerShouldDetectBarcodes:(SBSDKBarcodeScannerViewController *)controller {
return self.shouldDetectBarcodes;
}
@end
Data parsers
Classic UI components
This example shows the usage of the Swiss QR code data parser with classic UI components:
- Swift
- Objective-C
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
}
}
#import "BarcodeScannerObjcViewController.h"
@import ScanbotBarcodeScannerSDK;
// This is a simple, empty view controller which acts as a container and delegate for the SBSDKBarcodeScannerViewController.
@interface BarcodeScannerObjcViewController () <SBSDKBarcodeScannerViewControllerDelegate>
// The instance of the scanner view controller.
@property (strong, nonatomic) SBSDKBarcodeScannerViewController *scannerViewController;
@end
@implementation BarcodeScannerObjcViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Create the SBSDKBarcodeScannerViewController instance.
self.scannerViewController = [[SBSDKBarcodeScannerViewController alloc] initWithParentViewController:self
parentView:self.view
delegate:self];
// Get current view finder configuration object
SBSDKBaseScannerViewFinderConfiguration *config = self.scannerViewController.viewFinderConfiguration;
// Enable the view finder.
config.isViewFinderEnabled = YES;
// Set the finder's aspect ratio.
config.aspectRatio = [[SBSDKAspectRatio alloc] initWithWidth:2 andHeight:1];
// Set the finder's minimum insets.
config.minimumInset = UIEdgeInsetsMake(100, 50, 100, 50);
// Configure the view finder colors and line properties.
config.lineColor = [UIColor redColor];
config.backgroundColor = [[UIColor redColor] colorWithAlphaComponent: 0.1];
config.lineWidth = 2;
config.lineCornerRadius = 8;
// Set the view finder configuration to apply it.
self.scannerViewController.viewFinderConfiguration = config;
// Get current energy configuration.
SBSDKBaseScannerEnergyConfiguration *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.
NSArray <SBSDKBarcodeType *> *commonTypes = [SBSDKBarcodeType commonTypes];
self.scannerViewController.acceptedBarcodeTypes = commonTypes;
// Set Swiss QR as an accepted document type.
self.scannerViewController.acceptedDocumentTypes = @[SBSDKBarcodeDocumentType.swissQR];
}
// The implementation of SBSDKBarcodeScannerViewControllerDelegate.
// Implement this method to process detected barcodes.
- (void)barcodeScannerController:(SBSDKBarcodeScannerViewController *)controller
didDetectBarcodes:(NSArray<SBSDKBarcodeScannerResult *> *)codes {
// Process the detected barcodes.
SBSDKSwissQRCodeDocumentFormat *document = (SBSDKSwissQRCodeDocumentFormat *)(codes[0].formattedResult);
// Enumerate the Swiss QR code data fields.
for (SBSDKSwissQRCodeDocumentField *field in document.fields) {
// Do something with the fields.
}
}
// Implement this method when you need to pause the detection (e.g. when showing the results).
- (BOOL)barcodeScannerControllerShouldDetectBarcodes:(SBSDKBarcodeScannerViewController *)controller {
return YES;
}
@end
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
- Swift
- Objective-C
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.
}
}
@import ScanbotBarcodeScannerSDK;
// Some barcode raw string.
NSString *rawBarcodeString = @"...";
// Instantiate the parser.
SBSDKBarcodeDocumentParser *parser = [[SBSDKBarcodeDocumentParser alloc] init];
// Run the parser.
SBSDKSwissQRCodeDocumentFormat *document
= (SBSDKSwissQRCodeDocumentFormat *)[parser parseDocumentWithInputString: rawBarcodeString];
// Check the result.
if (document != nil) {
// Enumerate the Swiss QR code data fields.
for (SBSDKSwissQRCodeDocumentField *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 LicenseWhat do you think of this documentation?
What can we do to improve it? Please be as detailed as you like.