Skip to main content

Barcode Scanning

You can scan barcodes in images using the scan method.

Model Selection​

Specify the barcode-specific model edgeocr_barcode_default with useModel.

Using OCR and barcode recognition simultaneously

If you want to perform text recognition and barcode recognition at the same time, specify model-d{width}x{height}_with_barcode (e.g., model-d320x320_with_barcode).

Setting the Number of Reads​

You can configure the number of reads required for confirmation per barcode format.

// Confirm QR codes only after 5 reads
let barcodeFormats = [BarcodeFormat.QRCode: 5]
let modelSettings = ModelSettings(barcodeNToConfirm: barcodeFormats)
loadModelAndNavigate(
destination: .barcodeView,
uid: "edgeocr_barcode_default",
modelSettings: modelSettings)

Implementing the Barcode Scanner​

Use the scanBarcodes method to scan barcodes, and only use barcodes with ScanConfirmationStatus.Confirmed as results.

func captureOutput(
_ output: AVCaptureOutput,
didOutput sampleBuffer: CMSampleBuffer,
from connection: AVCaptureConnection
) {
let scanResult: ScanResult
do {
scanResult = try edgeOCR.scanBracodes(
sampleBuffer,
barcodeScanOption: barcodeScanOption,
viewBounds: viewBounds)
} catch {
os_log("Failed to scan: %@", type: .debug, error.localizedDescription)
return
}

DispatchQueue.main.async { [weak self] in
self?.drawDetections(result: scanResult)
}
}

func drawDetections(result: ScanResult) {
var detections: [Barcode] = []
CATransaction.begin()
CATransaction.setValue(kCFBooleanTrue, forKey: kCATransactionDisableActions)
detectionLayer.sublayers = nil
for detection in result.getBarcodeDetections() {
if detection.getStatus() == ScanConfirmationStatus.Confirmed {
let bbox = detection.getBoundingBox()
drawDetection(bbox: bbox, text: detection.getText())
detections.append(detection)
}
}
CATransaction.commit()

if detections.count > 0 && !showDialog {
showDialog(detections: detections)
}
}

Resetting the Scan State​

You can reset the read count by calling resetScanningState() after displaying the results.

edgeOCR.resetScanningState()
note

The default detection area for the barcode scanner is the entire input image. Therefore, the aspect ratio obtained from ModelInformation is set to 0.

Higher-accuracy barcode recognition

The high-accuracy barcode recognition model barcode_next is available as an experimental model. See Experimental Barcode Recognition for details.