OCR on Images
Sample Code
This section explains how to perform OCR on still images rather than camera frames. When calling scan with an image as the argument, results are returned synchronously.
Scanning Text in Images
- iOS (Swift)
- Android (Java)
Pass a UIImage to the scan method.
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)
// Draw the bounding box
}
Pass a Bitmap to the scan method.
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);
Scanning Barcodes in Images
- iOS (Swift)
- Android (Java)
Specify a BarcodeScanOption and pass it to the scan method.
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()
// Draw the bounding box
}
Specify a barcode-capable model with useModel, then pass a Bitmap to the scan method.
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);