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 BarcodeScannerDataParsingSwiftViewController: 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
// Set Swiss QR as an accepted document type.
self.scannerViewController.acceptedDocumentTypes = [.swissQR]
}
}
// The implementation of SBSDKBarcodeScannerViewControllerDelegate.
extension BarcodeScannerDataParsingSwiftViewController: SBSDKBarcodeScannerViewControllerDelegate {
// Implement this function to process detected barcodes.
func barcodeScannerController(_ controller: SBSDKBarcodeScannerViewController,
didDetectBarcodes codes: [SBSDKBarcodeScannerResult]) {
// Process the detected barcodes.
let barcode = codes.first
// Parse the resulted document as a Swiss QR document and retrieve it's elements.
if let document = SBSDKBarcodeDocumentSwissQR(document: barcode?.parsedDocument) {
// Enumerate the Swiss QR code data fields.
for field in document.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 self.shouldDetectBarcodes
}
}
#import "BarcodeScannerDataParsingObjcViewController.h"
@import ScanbotBarcodeScannerSDK;
// This is a simple, empty view controller which acts as a container and delegate for the SBSDKBarcodeScannerViewController.
@interface BarcodeScannerDataParsingObjcViewController () <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 BarcodeScannerDataParsingObjcViewController
- (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 = @[SBSDKBarcodeDocumentRootType.swissQR];
}
- (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.
SBSDKBarcodeScannerResult *barcode = codes.firstObject;
if (barcode) {
// Parse the resulted document as a Swiss QR document and retrieve it's elements.
SBSDKBarcodeDocumentSwissQR *document = [[SBSDKBarcodeDocumentSwissQR alloc] initWithGenericDocument:barcode.parsedDocument];
if (document) {
// Enumerate the Swiss QR code data fields.
for (SBSDKGenericDocumentField *field in document.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 self.shouldDetectBarcodes;
}
@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
func parseDataManually() {
// Some raw barcode string.
let rawBarcodeString = "(01)02804086001986(3103)000220(15)220724(30)01(3922)00198"
// Instantiate the parser.
let parser = SBSDKBarcodeDocumentParser()
// Run the parser and check the result.
if let document = parser.parseDocument(inputString: rawBarcodeString) {
// Parse the resulted document as a GS1 document and retrieve it's elements.
if let gs1ParsedDocument = SBSDKBarcodeDocumentGS1(document: document),
let elements = gs1ParsedDocument.elements {
// Enumerate over the elements.
for element in elements {
// Do something with the element.
print("\(element.dataTitle?.value?.text) = \(element.rawValue?.value?.text)")
}
}
}
}
@import ScanbotBarcodeScannerSDK;
@interface BarcodeManualDataParsingObjcExample : NSObject
- (void)parseDataManually;
@end
@implementation BarcodeManualDataParsingObjcExample
- (void)parseDataManually {
// Some raw barcode string.
NSString *rawBarcodeString = @"(01)02804086001986(3103)000220(15)220724(30)01(3922)00198";
// Instantiate the parser.
SBSDKBarcodeDocumentParser *parser = [[SBSDKBarcodeDocumentParser alloc] init];
// Run the parser and check the result.
SBSDKGenericDocument *document = [parser parseDocumentWithInputString:rawBarcodeString];
if (document) {
// Parse the resulted document as a GS1 document.
SBSDKBarcodeDocumentGS1 *gs1ParsedDocument = [[SBSDKBarcodeDocumentGS1 alloc] initWithGenericDocument:document];
// Retrieve the elements.
NSArray<SBSDKBarcodeDocumentGS1Element *> *elements = gs1ParsedDocument.elements;
if (elements) {
// Enumerate over the elements.
for (SBSDKBarcodeDocumentGS1Element *element in elements) {
// Do something with the element.
NSString *dataTitle = element.dataTitle.value.text ?: @"";
NSString *rawValue = element.rawValue.value.text ?: @"";
NSLog(@"%@ = %@", dataTitle, rawValue);
}
}
}
}
@end
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.