1. Swift
  2. Single Barcode Scanning

Swift

Single Barcode Scanning

Single barcode scanning allows you to detect and process one barcode at a time within a predefined scanning window. This ensures precise detection by limiting the scan area, improving accuracy and performance.

⚙️ 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 = .single, 
    scanMode: VisionSDK.CodeScannerMode = .autoBarCodeOrQRCode)

      

🤖 Auto Mode Barcode Scanning

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

        captureMode: VisionSDK.CaptureMode = .auto

      

Configuration example:

        scannerView.configure(
    delegate: VisionSDK.CodeScannerViewDelegate,
    sessionPreset: .high,
    captureMode: .auto,
    captureType: .single,
    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: .single,
    scanMode: .autoBarCodeOrQRCode
)

      

🎯 Focus Area Configuration

To define the scan region, use the focusImageRect parameter:

        
let focusSettings = CodeScannerView.FocusSettings()
focusSettings.focusImageRect = CGRect(x: 0, y: 100, width: 100, height: 100)


// To display focus image on camera layer
focusSettings.shouldDisplayFocusImage = true

scannerView.setFocusSettingsTo(focusSettings)

      

This helps confirm the scanning region visually during development or debugging.


🧭 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 focusSettings = CodeScannerView.FocusSettings()
focusSettings.focusImageRect = CGRect(x: 0, y: 100, width: 100, height: 100)

// To display focus image on camera layer
focusSettings.shouldDisplayFocusImage = true
scannerView.setFocusSettingsTo(focusSettings)

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

scannerView.configure(
    delegate: VisionSDK.CodeScannerViewDelegate,
    sessionPreset: AVCaptureSession.Preset = .high,
    captureMode: VisionSDK.CaptureMode = .auto, 
    captureType: VisionSDK.CaptureType = .single, 
    scanMode: VisionSDK.CodeScannerMode = .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) {

}