OCR with Master Data (Exact Match)
Sample Code
This section explains how to check whether OCR results match entries in master data (whitelist) and display highlights or dialogs when a match is found.

Implementing the Analyzer
Implement an Analyzer class that compares detection results against master data. It classifies text into those that match the master data and those that do not.
- iOS (Swift)
- Android (Java)
class WhiteListAnalyzer {
let whiteList: Set = [
"090-1234-5678",
"090-0000-1234",
"090-2222-3456",
"090-4444-5555",
"090-6666-7777",
"090-8888-9999",
]
func analyze(_ detections: [Text]) -> AnalyzerResult {
var targetDetections: [Text] = []
var notTargetDetections: [Text] = []
for detection in detections {
let text = detection.getText()
if whiteList.contains(text) {
targetDetections.append(detection)
} else {
notTargetDetections.append(detection)
}
}
return AnalyzerResult(
targetDetections: targetDetections,
notTargetDetections: notTargetDetections)
}
}
Similarly, use a Set to perform exact matching against the master data.
public class WhiteListAnalyzer {
private final Set<String> whiteList = new HashSet<>(Arrays.asList(
"090-1234-5678",
"090-0000-1234",
"090-2222-3456",
"090-4444-5555",
"090-6666-7777",
"090-8888-9999"
));
public AnalyzerResult analyze(List<Text> detections) {
List<Text> targetDetections = new ArrayList<>();
List<Text> notTargetDetections = new ArrayList<>();
for (Text detection : detections) {
String text = detection.getText();
if (whiteList.contains(text)) {
targetDetections.add(detection);
} else {
notTargetDetections.add(detection);
}
}
return new AnalyzerResult(targetDetections, notTargetDetections);
}
}
Displaying Detection Results
Text that matches the master data is displayed in green, while non-matching text is displayed in red.
- iOS (Swift)
- Android (Java)
func drawDetections(result: ScanResult) {
CATransaction.begin()
CATransaction.setValue(kCFBooleanTrue, forKey: kCATransactionDisableActions)
detectionLayer.sublayers = nil
let detections = result.getTextDetections()
let analyzerResult = analyzer.analyze(detections)
for targetDetection in analyzerResult.getTargetDetections() {
let text = targetDetection.getText()
let bbox = targetDetection.getBoundingBox()
drawDetection(bbox: bbox, text: text) // Green (default)
}
for notTargetDetection in analyzerResult.getNotTargetDetections() {
let text = notTargetDetection.getText()
let bbox = notTargetDetection.getBoundingBox()
drawDetection(
bbox: bbox, text: text,
boxColor: UIColor.red.withAlphaComponent(0.5).cgColor)
}
CATransaction.commit()
}
On Android, when using CameraOverlay, filter the results before passing them to setBoxes and implement color-coded display. See the sample app for details.