Skip to main content

Experimental Barcode Recognition

Sample code
experimental

The models and APIs on this page are experimental. Get the models through availableModelsWithExperimental(). See Overview of Experimental Features for details.

In addition to standard barcode scanning (edgeocr_barcode_default), the experimental high-accuracy barcode recognition model barcode_next is available. Scanning works the same as in Barcode Scanning; only the model you load and the ModelSettings differ.

barcode_next first tries standard barcode recognition and falls back to the new recognition method if that fails (you can also configure it to use only the new method). Barcodes are detected with the YotsukaD detection model. You can change its confidence threshold in Detector Settings.

High-accuracy barcode recognition (barcode_next)​

It works with the default settings.

Usage​

var modelSettings = ModelSettings(barcodeNToConfirm: [BarcodeFormat.AnyFormat: 1])
modelSettings.nextBarcodeRecognizerSettings = NextBarcodeRecognizerSettings(
nextOnly: false,
enableRelaxedPass: false,
enableReverse: true,
constrainVerticalToRegion: true) // Useful when barcodes are densely packed

guard let model = edgeOCR.availableModelsWithExperimental()
.first(where: { $0.getUID() == "barcode_next" }) else {
fatalError("Model not found")
}
let modelInfo = try await edgeOCR.useModel(model, settings: modelSettings)

NextBarcodeRecognizerSettings fields​

FieldDefaultDescription
nextOnlyfalseIf true, only the new recognition method is used. If false, standard barcode recognition is tried first, and the new method is used if it fails.
enableRelaxedPassfalseWhether to enable an additional pass with relaxed criteria
enableReversetrueWhether to enable scanning in the reverse direction
constrainVerticalToRegionfalseIf true, scan lines are not extended beyond the detected barcode region. Useful for preventing misreads of neighboring barcodes when barcodes are densely packed.

Getting barcode binary data​

From a Barcode, you can get the binary data before it is converted to a string, along with the encoding of each range. Use this for data that isn't text, or data that mixes several encodings, such as in QR codes.

  • getRawData(): The barcode's binary data
  • getEncodingRanges(): The encoding of each range of the binary data (encoding, start position, length). Ranges never overlap.
for barcode in scanResult.getBarcodeDetections() {
let rawData: [UInt8] = barcode.getRawData()
for (encoding, start, length) in barcode.getEncodingRanges() {
let bytes = rawData[start..<(start + length)]
print("\(encoding): \(bytes.count) bytes")
}
}
note

If the barcode doesn't specify its encoding explicitly, the encoding is guessed, so an incorrect encoding may be returned.