メインコンテンツまでスキップ

画像の OCR

カメラのフレームではなく、静止画像に対して OCR を行う方法を説明します。画像を引数に scan を呼び出した場合、同期的に結果が返却されます。

テキスト画像のスキャン

UIImagescan メソッドに渡します。

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)
// バウンディングボックスを描画
}

バーコード画像のスキャン

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()
// バウンディングボックスを描画
}