Experimental バーコード認識
サンプルコード
- Android: MainActivity.java(
barcode_panda_button)
experimental
このページで説明するモデルと API は experimental です。モデルは availableModelsWithExperimental() から取得してください。詳しくは Experimental 機能の概要を参照してください。
通常のバーコードスキャン(edgeocr_barcode_default)に加えて、experimental な高精度バーコード認識モデル barcode_next を提供しています。スキャンの実装はバーコードスキャンと同じで、ロードするモデルと ModelSettings の設定のみが異なります。
barcode_next は、まず従来のバーコード認識を試し、読み取れなかった場合に新しい認識処理を使用します(設定で新しい認識処理のみを使うこともできます)。バーコードの検出には YotsukaD 検出モデルを使用します。検出の信頼度の閾値は検出器の詳細設定で変更できます。
高精度バーコード認識(barcode_next)
既定の設定のままでも使用できます。
使用方法
- iOS (Swift)
- Android (Java)
var modelSettings = ModelSettings(barcodeNToConfirm: [BarcodeFormat.AnyFormat: 1])
modelSettings.nextBarcodeRecognizerSettings = NextBarcodeRecognizerSettings(
nextOnly: false,
enableRelaxedPass: false,
enableReverse: true,
constrainVerticalToRegion: true) // バーコードが密集している場合に有効
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); // バーコードが密集している場合に有効
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 の項目
| 項目 | 既定値 | 説明 |
|---|---|---|
nextOnly | false | true にすると新しい認識処理のみを使用します。false の場合は従来のバーコード認識を先に試し、失敗した場合に新しい認識処理を使用します。 |
enableRelaxedPass | false | 判定条件を緩和した追加の読み取りを有効にするかどうか |
enableReverse | true | 逆方向からのスキャンを有効にするかどうか |
constrainVerticalToRegion | false | true にすると、スキャンラインをバーコードの検出領域の外側に広げません。バーコードが密集している場合に、隣のバーコードの誤読を防ぐのに有用です。 |
バーコードのバイナリデータの取得
Barcode からは、文字列に変換する前のバイナリデータと、その各区間のエンコーディングを取得できます。QR コードなどで、文字列以外のデータや複数のエンコーディングが混在するデータを扱う場合に使用します。
getRawData(): バーコードのバイナリデータgetEncodingRanges(): バイナリデータの各区間のエンコーディング(エンコーディング・開始位置・長さ)。区間が重複することはありません。
- 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");
}
}
注記
バーコードにエンコーディングが明示されていない場合は推測した値が返るため、誤ったエンコーディングが返される可能性があります。