Experimental Barcode Recognition
- Android: MainActivity.java (
barcode_panda_button)
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
- iOS (Swift)
- Android (Java)
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)
ModelSettings modelSettings = new ModelSettings();
modelSettings.setBarcodeNToConfirm(Collections.singletonMap(BarcodeFormat.Any, 1));
NextBarcodeRecognizerSettings next = modelSettings.getNextBarcodeRecognizerSettings();
next.setConstrainVerticalToRegion(true); // Useful when barcodes are densely packed
modelSettings.setNextBarcodeRecognizerSettings(next);
Model model = null;
for (Model candidate : api.availableModelsWithExperimental()) {
if (candidate.getUID().equals("barcode_next")) {
model = candidate;
break;
}
}
api.useModel(model, modelSettings, successCallback, errorCallback);
NextBarcodeRecognizerSettings fields
| Field | Default | Description |
|---|---|---|
nextOnly | false | If 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. |
enableRelaxedPass | false | Whether to enable an additional pass with relaxed criteria |
enableReverse | true | Whether to enable scanning in the reverse direction |
constrainVerticalToRegion | false | If 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 datagetEncodingRanges(): The encoding of each range of the binary data (encoding, start position, length). Ranges never overlap.
- iOS (Swift)
- Android (Java)
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")
}
}
for (Barcode barcode : scanResult.getBarcodeDetections()) {
byte[] rawData = barcode.getRawData();
for (Barcode.EncodingRange range : barcode.getEncodingRanges()) {
byte[] bytes = Arrays.copyOfRange(rawData, range.start, range.start + range.length);
Log.d("EdgeOCR", range.encoding + ": " + bytes.length + " bytes");
}
}
If the barcode doesn't specify its encoding explicitly, the encoding is guessed, so an incorrect encoding may be returned.