2️⃣ Read MRZ
Last updated
Was this helpful?
Last updated
Was this helpful?
The Machine Readable Zone - MRZ contains the information that acts as "" to access the chip content. If machine-reading of the MRZ is not possible the information can be entered manually.
fun readMRZ(onCompletion: (DocumentResult<MRZDocument>) -> Unit)
public static func readMRZ(
completion: @escaping (Result<MrzDocument, KeylessDocument.DocumentError>) -> Void
)
Future<DocumentResult<MRZDocument>> readMRZ()
If the Keyless Document SDK can read the MRZ it will return an instance of MRZDocument
containing the data read.
data class MRZDocument(
val documentNumber: String,
val dateOfBirth: String,//Format as yy-MM-dd
val dateOfExpiration: String,//Format as yy-MM-dd
val personalNumber: String? = null,
val firstName: String? = null,
val lastName: String? = null,
val nationality: String? = null,
val issuingState: String? = null,
val documentType: String? = null,
var gender: String? = null
)
public struct MrzDocument {
public let documentNumber: String
public let birthdate: String
public let expirationDate: String
}
class MRZDocument {
final String documentNumber;
final String dateOfBirth; // Format: yy-MM-dd (e.g., 23-05-15)
final String dateOfExpiration; // Format: yy-MM-dd (e.g., 23-05-15)
final String? personalNumber;
final String? firstName;
final String? lastName;
final String? nationality;
final String? issuingState;
final String? documentType;
final String? gender;
}
In case of errors the Keyless Document SDK will return the following errors:
public sealed class DocumentError(
public open val code: Int,
public open val message: String,
)
// errors launching the MrzCameraActivity
public data class InternalError(override val code: Int) : DocumentError(code, "Internal error")
public data object UserCancelled : DocumentError(
code = 5000,
message = "User cancelled the operation."
)
public data object LauncherNotInitialized : DocumentError(
code = 5002,
message = "Launcher is null or not initialized"
)
public data object PermissionDenied : DocumentError(
code = 7001,
message = "Permission denied."
)
public data class UnknownError( override val message: String) : DocumentError(
code = 9999,
message = message
)
public enum DocumentError: Error {
case userCancelled
case cannotCalculateMRZKey
case cameraUnavailable
case cameraPermissionDenied
case unknownError(message: String)
public var code: Int {
switch self {
case .userCancelled: return 5000
case .cameraUnavailable: return 6001
case .cannotCalculateMRZKey: return 6002
case .cameraPermissionDenied: return 6004
case .unknownError: return 9999
}
}
public var message: String {
switch self {
case .userCancelled: return "User cancelled the operation"
case .cannotCalculateMRZKey: return "Failed to calculate MRZ key"
case .cameraUnavailable: return "Camera is unavailable"
case .cameraPermissionDenied: return "Permission denied"
case .unknownError(let message): return message
}
}
}
enum DocumentError {
internalError,
userCancelled,
launcherNotInitialized,
permissionDenied,
cannotCalculateMRZKey,
cameraUnavailable,
cameraPermissionDenied,
unknownError;
KeylessDocument.readMRZ {
when (it) {
is DocumentResult.Success -> {
Log.d(TAG, "MRZ data ${it.value}")
}
is DocumentResult.Failure -> {
Log.d(TAG, "MRZ error ${it.error}")
}
}
KeylessDocument.readMRZ { result in
switch result {
case .success(let document):
print("MRZ data: \(document)")
case .failure(let error):
print("MRZ error: \(error)")
}
}
DocumentResult<MRZDocument> documentResult = await KeylessDocument.instance.readMRZ();
switch (documentResult) {
case DocumentSuccess<MRZDocument>(data: final mrz):
String mrzData = 'MRZ: ${mrz.documentNumber}, ${mrz.dateOfBirth} ${mrz.dateOfExpiration}';
break;
case DocumentFailure(message: final error, code: final code, errorType: final type):
String error = 'MRZ Error: $error $code ($type)';
break;