画像の OCR
サンプルコード
カメラのフレームではなく、静止画像に対して OCR を行う方法を説明します。画像を引数に scan を呼び出した場合、同期的に結果が返却されます。
テキスト画像のスキャン
- iOS (Swift)
- Android (Java)
UIImage を scan メソッドに渡します。
let rotatedImage = image.fixImageRotation()
let detections = try edgeOCR.scan(rotatedImage)
for detection in detections.getTextDetections() {
let bbox = detection.getBoundingBox()
let x = image.size.width * bbox.minX
let y = image.size.height * bbox.minY
let width = image.size.width * bbox.width
let height = image.size.height * bbox.height
let rect = CGRect(x: x, y: y, width: width, height: height)
// バウンディングボックスを描画
}
Bitmap を scan メソッドに渡します。
AssetManager assetManager = getAssets();
Bitmap bitmap = BitmapFactory.decodeStream(assetManager.open("images/sample.bmp"));
ScanResult scanResult = api.scan(bitmap);
List<Text> detections = scanResult.getTextDetections();
cameraOverlay.setBoxes(detections);
バーコード画像のスキャン
- iOS (Swift)
- Android (Java)
BarcodeScanOption を指定して scan メソッドに渡します。
let rotatedImage = image.fixImageRotation()
let barcodeScanOption = BarcodeScanOption(
targetFormats: [BarcodeFormat.AnyFormat])
let detections = try edgeOCR.scan(rotatedImage, barcodeScanOption: barcodeScanOption)
for detection in detections.getBarcodeDetections() {
let bbox = detection.getBoundingBox()
// バウンディングボックスを描画
}
バーコードを読み取れるモデルを useModel で指定した上で、Bitmap を scan メソッドに渡します。
AssetManager assetManager = getAssets();
Bitmap bitmap = BitmapFactory.decodeStream(assetManager.open("images/sample_barcode.bmp"));
api.resetScanningState();
ScanResult scanResult = api.scan(bitmap);
List<Barcode> detections = scanResult.getBarcodeDetections();
cameraOverlay.setBoxes(detections);