Handling OCR Results
The onResults callback delivers an OCRResponse once both sides of the document have been processed (or the front only, if setRequireBackSide(false)).
OCRResponse​
data class OCRResponse(
val documentText: Map<String, String>, // field name → extracted value
val documentImages: Map<String, Bitmap> // image label → cropped bitmap
)
Common documentText keys​
| Key | Example value |
|---|---|
"Document Number" | "A12345678" |
"Given Names" | "John" |
"Surname" | "Doe" |
"Date of Birth" | "01.01.1990" |
"Date of Expiry" | "01.01.2030" |
"Nationality" | "Tanzanian" |
"Sex" | "M" |
Common documentImages keys​
| Key | Description |
|---|---|
"Portrait" | Cropped face photo from the document |
"Document front side" | Cropped front-side image |
"Document back side" | Cropped back-side image (if requireBackSide = true) |
Example — extract and display​
onResults = { result: OCRResponse ->
// Text fields
val name = result.documentText["Given Names"] ?: ""
val docId = result.documentText["Document Number"] ?: ""
val expiry = result.documentText["Date of Expiry"] ?: ""
// Images
val portrait = result.documentImages["Portrait"]
val frontSide = result.documentImages["Document front side"]
// Convert portrait to Base64 for upload
val portrait64 = portrait?.let { bitmap ->
val stream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, stream)
Base64.encodeToString(stream.toByteArray(), Base64.NO_WRAP)
}
}
Empty results​
If documentText and documentImages are both empty, the session was unable to extract data. The most common causes are poor lighting, motion blur, or an unsupported document type. Prompt the user to retry and check your session configuration.