1. Swift
  2. Multiple Barcode Scanning

Swift

Multiple Barcode Scanning

Multiple Barcode Scanning is a mode in the Vision SDK that allows you to scan and detect multiple barcodes within the camera frame simultaneously. This is useful in scenarios where several barcodes are present in a single image or view, and you want to capture all of them in one go.

Unlike single barcode scanning, this mode does not restrict detection to just one barcode. Instead, it returns an array of all valid barcodes detected within the scanning region, improving efficiency for batch processing or document scanning use cases.

⚙️ Configure the SDK

To enable single barcode scanning, you need to configure the Vision SDK using the configure method:

        
scannerView.configure(
    delegate: VisionSDK.CodeScannerViewDelegate,
    sessionPreset: AVCaptureSession.Preset = .high,
    captureMode: VisionSDK.CaptureMode = .auto, 
    captureType: VisionSDK.CaptureType = .multiple, 
    scanMode: VisionSDK.CodeScannerMode = .autoBarCodeOrQRCode)

      

🤖 Auto Mode Barcode Scanning

For automatic barcode capture, set the captureMode to .auto:

        scannerView.setCaptureModeTo(.auto)

      

Configuration example:

        scannerView.configure(
    delegate: VisionSDK.CodeScannerViewDelegate,
    sessionPreset: .high,
    captureMode: .auto,
    captureType: .multiple,
    scanMode: .autoBarCodeOrQRCode
)

      

✋ Manual Mode Barcode Scanning

For manual barcode capture, set the captureMode to .manual:

        captureMode: VisionSDK.CaptureMode = .manual

      

Configuration example:

        scannerView.configure(
    delegate: VisionSDK.CodeScannerViewDelegate,
    sessionPreset: .high,
    captureMode: .manual,
    captureType: .multiple,
    scanMode: .autoBarCodeOrQRCode
)

      

🧭 Detection Indicators

To receive detection indicators (without actual values), implement the following delegate method:

        func codeScannerViewDidDetect(_ text: Bool, barCode: Bool, qrCode: Bool, document: Bool)

      
  • text: Indicates presence of text
  • barCode: Indicates presence of a barcode
  • qrCode: Indicates presence of a QR code
  • document: Indicates presence of a document

🔘 Enable Detection Indicators

You can toggle detection indicators as follows:

        
let objectDetectionConfiguration = CodeScannerView.ObjectDetectionConfiguration()

// Enable text detection indicator
objectDetectionConfiguration.isTextIndicationOn = true

// Enable barcode/QR code detection indicator
objectDetectionConfiguration.isBarCodeOrQRCodeIndicationOn = true

// Enable document detection indicator
objectDetectionConfiguration.isDocumentIndicationOn = true

scannerView.setObjectDetectionConfigurationTo(objectDetectionConfiguration)


      

📦 Delegate Method for Barcode Values

When a barcode is successfully scanned, the following delegate method is called:

        func codeScannerView(_ scannerView: VisionSDK.CodeScannerView, didSuccess codes: [DetectedBarcode])

      

❗️Error Handling Delegate

If an error occurs during the barcode scanning process, the following delegate method will be triggered. You can implement this to handle any failures gracefully.

        func codeScannerView(_ scannerView: VisionSDK.CodeScannerView, didFailure error: NSError)

      

Use this method to log errors, display messages to the user, or perform recovery actions when scanning fails.


🧾 Barcode Object Structure

The DetectedBarcode object returned in the delegate contains the following properties:

        stringValue: String
symbology: BarcodeSymbology // e.g., UPC, Code128, QRCode
extractedData: [String: String]?

      

🧪 Sample Code

        import VisionSDK

let scannerView = CodeScannerView(frame: view.bounds)
self.view.addSubview(scannerView)

let objectDetectionConfiguration = CodeScannerView.ObjectDetectionConfiguration()
objectDetectionConfiguration.isBarCodeOrQRCodeIndicationOn = true
scannerView.setObjectDetectionConfigurationTo(objectDetectionConfiguration)

scannerView.configure(
    delegate: VisionSDK.CodeScannerViewDelegate,
    sessionPreset: .high,
    captureMode: .auto,
    captureType: .multiple,
    scanMode: .autoBarCodeOrQRCode
)

scannerView.startRunning() // Start scanning

// Delegate callback for scanned barcodes
func codeScannerView(_ scannerView: CodeScannerView, didSuccess codes: [DetectedBarcode]) {
    print(codes)
}

func codeScannerView(_ scannerView: CodeScannerView, didFailure error: NSError) {

}

func codeScannerViewDidDetect(_ text: Bool, barCode: Bool, qrCode: Bool, document: Bool) {

}