Displaying OCR Results on Screen
This section explains how to display the position and text of detected and recognized results on the screen.

Image Area Analyzed by the SDK
The area analyzed by scan depends on the specified model. Here we use model-d320x320 as an example.
A model has an expected aspect ratio for its input image. model-d320x320 expects an image with an aspect ratio of 320x320 (width x height).
If an image with a different aspect ratio than what the model expects is passed, the SDK crops a portion of the image (the center by default) and passes it to the model. The width is based on the input image width, and the height is calculated to maintain the model's aspect ratio.

For example, if the input image size is 360x720, the image passed to the model will have a width of 360 and a height of 360 * 320 / 320 = 360.
Matching the input image aspect ratio to the model's input size aspect ratio prevents padding and allows the entire input image to be analyzed, resulting in improved detection accuracy. For example, when using model-d320x320, it is effective to make the input image close to a 1:1 aspect ratio.

For example, if the full screen size is 1080x1920 and you crop the center 1080x480 to pass to EdgeOCR, the cropped image has an aspect ratio of 1080:480 = 1:0.44, and the resized image becomes 320x142. Since the model's input size aspect ratio is 1:1, black padding is added vertically. In terms of processing time, there is no significant difference between processing a 1080x1080 image and a 1080x480 image. However, since the padded area is excluded from analysis, passing a 1080x1080 image may result in higher accuracy.
Detection Result Coordinates
The coordinates obtained from the getBoundingBox method of Detection are relative coordinates where the width and height of the screen area passed to scan are normalized to 1.

To convert to absolute coordinates, multiply the relative coordinates by the width and height of the screen area, respectively.
Implementation
- iOS (Swift)
- Android (Java)
Setting Up the Detection Result Layer
Configure detectionLayer (the layer for drawing detection results) and guideLayer (the guide frame indicating the scan area).
override func setupLayers() {
let width = viewBounds.width
let height = viewBounds.width * CGFloat(aspectRatio)
let cropHorizontalBias = 0.5
let cropVerticalBias = 0.5
guideLayer = CALayer()
guideLayer.frame = CGRect(
x: cropHorizontalBias * (viewBounds.width - width),
y: cropVerticalBias * (viewBounds.height - height),
width: width,
height: height)
guideLayer.borderWidth = 3.0
guideLayer.borderColor = UIColor.green.cgColor
detectionLayer = CALayer()
detectionLayer.frame = viewBounds
DispatchQueue.main.async { [weak self] in
if let layer = self?.previewLayer {
layer.addSublayer(self!.detectionLayer)
layer.addSublayer(self!.guideLayer)
}
}
}
Drawing Detection Results
Convert from relative coordinates to absolute coordinates, then draw bounding boxes and text.
func drawDetections(result: ScanResult) {
CATransaction.begin()
CATransaction.setValue(kCFBooleanTrue, forKey: kCATransactionDisableActions)
detectionLayer.sublayers = nil
for detection in result.getTextDetections() {
let text = detection.getText()
if !text.isEmpty {
let bbox = detection.getBoundingBox()
drawDetection(bbox: bbox, text: text)
}
}
CATransaction.commit()
}
func drawDetection(
bbox: CGRect,
text: String,
boxColor: CGColor = UIColor.green.withAlphaComponent(0.5).cgColor,
textColor: CGColor = UIColor.black.cgColor
) {
let boxLayer = CALayer()
let width = detectionLayer.frame.width
let height = detectionLayer.frame.height
let bounds = CGRect(
x: bbox.minX * width,
y: bbox.minY * height,
width: (bbox.maxX - bbox.minX) * width,
height: (bbox.maxY - bbox.minY) * height)
boxLayer.frame = bounds
boxLayer.borderWidth = 1.0
boxLayer.borderColor = boxColor
let textLayer = CATextLayer()
textLayer.string = text
textLayer.fontSize = 15
textLayer.frame = CGRect(x: 0, y: -20, width: boxLayer.frame.width, height: 20)
textLayer.backgroundColor = boxColor
textLayer.foregroundColor = textColor
boxLayer.addSublayer(textLayer)
detectionLayer.addSublayer(boxLayer)
}
Placing the CameraOverlay
By using the CameraOverlay included in the SDK, you can easily implement OCR result display and whiteout of the area outside the scan range.
In the layout XML, place a CameraOverlay with the same size on top of the PreviewView.
<com.nefrock.edgeocr.ui.CameraOverlay
android:id="@+id/camera_overlay"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="@+id/previewView"
app:layout_constraintBottom_toBottomOf="@+id/previewView"
app:layout_constraintStart_toStartOf="@+id/previewView"
app:layout_constraintEnd_toEndOf="@+id/previewView" />
Setting the Model Aspect Ratio
By setting the model's aspect ratio on the CameraOverlay, the area outside the scan range will be whited out.
float modelAspectRatio = getIntent().getFloatExtra("model_aspect_ratio", 1.0f);
cameraOverlay = findViewById(R.id.camera_overlay);
cameraOverlay.setAspectRatio(modelAspectRatio);
Displaying OCR Results
Simply pass the detection results to the CameraOverlay's setBoxes method to display the OCR results.
imageAnalysis.setAnalyzer(analysisExecutor, image -> {
if (!api.isReady()) {
image.close();
return;
}
try {
ScanResult scanResult = api.scan(image);
List<Text> detections = scanResult.getTextDetections();
cameraOverlay.setBoxes(detections);
} catch (EdgeError e) {
Log.e("EdgeOCR", "Failed to analyze image", e);
} finally {
image.close();
}
});