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)


      

You can also control whether image sharpness scoring runs, using isImageSharpnessIndicationOn (default false on iOS, since it's a new, opt-in flag that adds an extra Neural Engine/CPU cost most consumers don't need):

        objectDetectionConfiguration.isImageSharpnessIndicationOn = true

      
NOTE

Platform difference: on Android, this same flag existed before and defaults to true to preserve historical behavior for existing consumers. On iOS it's new in v2.3.13 and defaults to false. If you support both platforms, set this flag explicitly rather than relying on the default.


⏸️ Pausing and Resuming Detection

Available since v2.3.13. Use pauseDetection() and resumeDetection() to temporarily stop all detection work (barcode, text, document, and sharpness) while keeping the camera preview and session running:

        // Stop detection, e.g. right after a successful capture while showing a loading state
scannerView.pauseDetection()

// Resume detection later, e.g. once the loading state is dismissed
scannerView.resumeDetection()

      

This is different from stopping the camera entirely. pauseDetection() keeps the live preview visible and the session active, so resuming is instant, there's no camera restart flicker, and no permission or session setup work needs to happen again. It's intended for short-lived UI states (a loading spinner, a result screen) where you don't want the camera doing analysis work in the background, but you also don't want to tear the session down.

While paused:

  • No detection callbacks fire (codeScannerViewDidDetect, barcode/document/text results, sharpness updates).
  • Any bounding boxes or detection indicators currently on screen are cleared immediately.
  • Any in-progress price tag detection is cancelled rather than left to finish in the background.

Switching scanMode automatically resumes detection even if it was previously paused, since changing modes rebuilds the underlying scanner. If your UI has a manual pause toggle, resync it to "on" when the user changes scan mode.


📦 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) {

}

// called when text, barcode, or qr code is detected in camera stream, Depends on configuration of scanner view in ObjectDetectionConfiguration e.g. isTextIndicationOn, isBarCodeOrQRCodeIndicationOn
func codeScannerViewDidDetect(_ text: Bool, barCode: Bool, qrCode: Bool, document: Bool) {
        
}

// called when text, barcode or qr code is detected in camera stream. This function is called every nth frame that is processed in live video feed.
func codeScannerViewDidDetectBoxes(_ text: Bool, barCode: [DetectedCode], qrCode: [DetectedCode], document: CGRect) {
    print("Total Codes\((barCode + qrCode).count)")
}