Barcode Scanning
You can scan barcodes in images using the scan method.
Model Selection
Specify the barcode-specific model edgeocr_barcode_default with useModel.
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.
- iOS (Swift)
- Android (Java)
// 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)
// Confirm QR codes only after 5 reads
ModelSettings modelSettings = new ModelSettings();
modelSettings.setBarcodeNToConfirm(
Collections.singletonMap(BarcodeFormat.QRCode, 5));
loadModelAndStartActivity(intent, "edgeocr_barcode_default", modelSettings);
Implementing the Barcode Scanner
- iOS (Swift)
- Android (Java)
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)
}
}
Use the scan method to scan, and only use barcodes with ScanConfirmationStatus.Confirmed as results.
@Override
public void analyze(@NonNull ImageProxy image) {
try {
if (!isActive) return;
if (callback == null) return;
List<Barcode> targetDetections = new ArrayList<>();
ScanResult scanResult = api.scan(image);
for (Barcode detection : scanResult.getBarcodeDetections()) {
if (detection.getStatus() == ScanConfirmationStatus.Confirmed) {
targetDetections.add(detection);
}
}
callback.call(targetDetections);
} catch (EdgeError e) {
Log.e("EdgeOCR", Log.getStackTraceString(e));
} finally {
image.close();
}
}
Resetting the Scan State
You can reset the read count by calling resetScanningState() after displaying the results.
- iOS (Swift)
- Android (Java)
edgeOCR.resetScanningState()
api.resetScanningState();
The default detection area for the barcode scanner is the entire input image. Therefore, the aspect ratio obtained from ModelInformation is set to 0.
The high-accuracy barcode recognition model barcode_next is available as an experimental model. See Experimental Barcode Recognition for details.