1. Swift
  2. Template-Based Barcode Scanning

Swift

Template-Based Barcode Scanning

Template-Based Barcode Scanning is a powerful mode in the Vision SDK that enables scanning specific barcodes from a document based on a predefined template. This is ideal when a document contains multiple barcodes, but you only want to extract a particular subset — for example, barcodes associated with tracking numbers, invoice codes, or specific labels.

🎯 Use Case

Imagine a shipping label that includes:

  • A QR code for tracking
  • A Code128 for internal processing
  • A UPC for product identification

With template-based scanning, you can configure the SDK to scan only the tracking QR code and ignore the others.


🧩 Creating a Template

To create a custom template, use the GenerateTemplateController provided by the Vision SDK. This controller enables you to define the layout and types of barcodes to be scanner.

Presenting the Template Controller

        func openTemplateController() {
    
    let scanController = GenerateTemplateController.instantiate()
    scanController.delegate = self
        
    if let sheet = scanController.sheetPresentationController {
        sheet.prefersGrabberVisible = true
    }
        
    self.present(scanController, animated: true)
}

      

Handling Template Creation Results

Implement the GenerateTemplateControllerDelegate methods to handle the outcomes of the template creation process:

            extension BarcodeViewController: GenerateTemplateControllerDelegate {
    
        // This function provides you with the ID of the template that has been created
        func templateScanController(_ controller: GenerateTemplateController, didFinishWith result: String) {
            print(result)
        }
    
        func templateScanController(_ controller: GenerateTemplateController, didFailWithError error: any Error) {
            controller.dismiss(animated: true)
        }
    
        func templateScanControllerDidCancel(_ controller: GenerateTemplateController) {
            print("Template creation cancelled")
        }
    }

      

💾 Managing Template

The Vision SDK automatically saves created templates into its secure storage. You can manage these templates using the following methods:

  • Retrieve all saved template IDs:

            let templateIDs = CodeScannerView.getAllTemplates()
    
          
  • Delete a specific template by ID:

            CodeScannerView.deleteTemplateWithId("template_id")
    
          
  • Delete all saved templates:

            CodeScannerView.deleteAllTemplates()
    
          

📸 Scanning with a Template

After creating and saving a template, you can configure the scanner to use it for scanning specific barcodes:

        
    let objectDetectionConfiguration = CodeScannerView.ObjectDetectionConfiguration()
    objectDetectionConfiguration.selectedTemplateId = "your_template_id"
    scannerView.setObjectDetectionConfigurationTo(objectDetectionConfiguration)


    scannerView.configure(
        delegate: self,
        sessionPreset: .high,
        captureMode: .auto,
        captureType: .templateBased,
        scanMode: .autoBarCodeOrQRCode
    )


      

Replace "your_template_id" with the actual ID of the template you created.


✅ Benefits

  • Selective Scanning Focus on specific barcodes within a document.
  • Efficiency Reduce processing time by ignoring irrelevant barcodes.
  • Customization Tailor scanning behavior to match your document layouts.