
Live camera passport scanning with auto-capture, manual capture and gallery import with optional cropping; on-device OCR, strict ICAO MRZ validation, configurable UI, torch, review and tips sheets.
A Kotlin Multiplatform / Compose Multiplatform library that scans passports on Android and iOS from commonMain.
| Platform | Target | Minimum version | Camera / OCR |
|---|---|---|---|
| Android | android |
API 24 (Android 7.0) | CameraX + ML Kit Text Recognition |
| iOS (device) | iosArm64 |
iOS 14 | AVFoundation + Apple Vision |
Desktop (JVM), web (JS/Wasm) and iosX64 (Intel simulators) are not supported.
// build.gradle.kts of your shared module
kotlin {
sourceSets {
commonMain.dependencies {
implementation("io.github.dipeshlama:passport-scanner:<latest-version>")
}
}
}The library is published to Maven Central, so mavenCentral() is all you need in your repositories.
The latest release is built with these versions. Use the same or newer in your app to avoid dependency conflicts.
| Library | Version |
|---|---|
| Compose Multiplatform | 1.12.2 |
| Kotlin | 2.4.21 |
| Material 3 (Compose Multiplatform) | 1.12.0-alpha03 |
| CameraX | 1.4.2 |
| ML Kit text recognition | 19.0.1 |
Android: nothing to do. The library's manifest declares the CAMERA permission. It also tells Play services to download the ML Kit text model when the app is installed. If your app already sets com.google.mlkit.vision.DEPENDENCIES (for example ocr,face), add tools:replace="android:value" to your entry and keep ocr in its value.
iOS: add a camera usage description to your Info.plist:
<key>NSCameraUsageDescription</key>
<string>The camera is used to scan your passport.</string>The gallery uses PHPickerViewController, which needs no photo library permission.
@Composable
fun ScanPassportScreen() {
var scanning by remember { mutableStateOf(false) }
if (scanning) {
PassportScanner(
onResult = { result ->
scanning = false
when (result) {
is PassportScanResult.Success -> {
val mrz = result.data
// mrz.givenNames, mrz.surname, mrz.documentNumber, mrz.nationality,
// mrz.dateOfBirth, mrz.expiryDate, mrz.gender, mrz.issuingCountry
// result.image: JPEG of the passport page (ByteArray), if you need to upload it
}
is PassportScanResult.ImageOnly -> {
// MRZ unreadable; the user chose to keep the photo and fill in details manually
}
}
},
onCancel = { scanning = false },
)
} else {
Button(onClick = { scanning = true }) { Text("Scan passport") }
}
}PassportScanner is a full-screen composable, so it works as a destination in any navigation library (Voyager, Navigation Compose, Decompose and others) or as plain state in a screen, as above. It calls onResult once and then stops scanning, so navigate away when it does.
onCancel is called for the top bar's back button and for system back / the back gesture. The scanner consumes system back while it's shown, so back never falls through and closes your app. Open sheets (review, tips) take system back first and just close.
PassportScanner(
onResult = { /* ... */ },
onCancel = { /* ... */ },
config = PassportScannerConfig(
reviewBeforeReturning = true, // show a confirm/re-scan sheet first
allowGallery = true, // "Upload from gallery" button
allowImageOnlyResult = true, // offer "Use this photo" when the MRZ can't be read
showTips = true, // tips button and sheet
showTorch = true, // flashlight button (hidden if the device has none)
rejectExpired = false, // true: ask for another passport if this one has expired
cropGalleryImage = false, // true: let the user crop a gallery photo before it's read
),
strings = PassportScannerStrings(
title = stringResource(Res.string.scan_passport), // localise any text
),
)With cropGalleryImage = true, the user can crop a gallery photo before it's read. Only the cropped area is read and returned.
With rejectExpired = true, an expired passport is never returned: the scanner shows strings.expiredTitle with a "Scan another passport" button. A passport is still valid on its expiry date. To check it yourself instead, use result.data.isExpired().
By default, sheet colours and fonts come from your MaterialTheme; see Sheet colours and fonts to change them.
There are four levels; use whichever is enough.
1. Text: pass strings = PassportScannerStrings(...) and override any field.
2. Colours: the scan line that sweeps up and down, the frame corners, the scrim, the text/icon colour over the camera, the "Reading passport…" spinner and the camera permission prompt:
colors = PassportScannerDefaults.colors(
scanLine = Color(0xFF00C853),
frameCorners = Color.White,
scrim = Color.Black.copy(alpha = 0.5f),
content = Color.White,
processingIndicator = Color(0xFF00C853), // spinner while a photo is read
processingScrim = Color.Black.copy(alpha = 0.7f),
processingText = Color.White,
permissionText = Color.White, // "Allow camera access…" message
permissionButtonContainer = Color(0xFF00C853),
permissionButtonContent = Color.Black,
)3. Sheet colours and fonts: see below.
4. Slots: replace a piece of the UI with your own composable. Leave a slot null (the default) to keep the built-in one:
PassportScanner(
onResult = { /* ... */ },
onCancel = { /* ... */ },
title = { Text("Verify identity", style = MaterialTheme.typography.titleLarge) },
instruction = { PassportScannerDefaults.Instruction("Hold the photo page inside the frame") },
galleryButton = { onClick, enabled ->
TextButton(onClick = onClick, enabled = enabled) { Text("Choose photo") }
},
captureButton = { onClick, enabled ->
FilledIconButton(onClick = onClick, enabled = enabled) { Icon(MyIcons.Camera, "Capture") }
},
captureHint = { Text("Tap if auto-scan doesn't pick it up") },
)| Slot | Replaces | Default text from strings
|
|---|---|---|
title |
Title in the top bar | title |
instruction |
Message above the frame | instruction |
galleryButton(onClick, enabled) |
"Upload from gallery" button | gallery |
captureButton(onClick, enabled) |
Round shutter button |
capture (accessibility label) |
captureHint |
Message under the shutter button | captureHint |
Slots are drawn over the camera with LocalContentColor set to colors.content, so plain Text/Icon are readable without extra styling. The built-in pieces are public in PassportScannerDefaults (Title, Instruction, GalleryButton, CaptureButton, CaptureHint), so you can reuse or wrap them instead of starting from scratch.
The review, "couldn't read" and tips sheets take their own colours and text styles. Every parameter defaults to your MaterialTheme, so override only what you need:
val myFont = FontFamily(Font(Res.font.inter_regular), Font(Res.font.inter_medium, FontWeight.Medium))
PassportScanner(
onResult = { /* ... */ },
onCancel = { /* ... */ },
sheetColors = PassportScannerDefaults.sheetColors(
container = Color(0xFF1C1B1F),
title = Color.White,
body = Color(0xFFCAC4D0),
label = Color(0xFFCAC4D0),
value = Color.White,
divider = Color(0xFF49454F),
primaryButtonContainer = Color(0xFF00C853),
primaryButtonContent = Color.Black,
secondaryButtonContent = Color(0xFF00C853),
accent = Color(0xFF00C853),
),
sheetTypography = PassportScannerDefaults.sheetTypography(
fontFamily = myFont, // applied to every sheet text style
title = MaterialTheme.typography.headlineSmall,
),
)sheetColors |
Used for |
|---|---|
container, scrim, dragHandle
|
Sheet background, dimmed backdrop, top handle |
title, body
|
Sheet titles; descriptions and tips |
label, value, divider
|
Review sheet rows |
primaryButtonContainer, primaryButtonContent
|
"Confirm" / "Try again" |
secondaryButtonContent |
"Re-scan", "Use this photo", "Got it" (text and outline) |
accent |
MRZ highlight in the tips illustration |
sheetTypography has title, body, label, value and button styles. fontFamily is applied on top of whichever styles you pass, so you can change the font alone or both font and sizes.
PassportMrzData.toString() (and so PassportScanResult.Success.toString()) prints *** for every field. Read the properties (data.documentNumber, data.surname, …) when you need the values. Equality and copy() work as usual.
If you already have OCR text, use the parser on its own:
val data: PassportMrzData? = MrzParser.parse(listOf(line1, line2))MrzParser.find(List<OcrLine>) does the same with line positions, which lets it rebuild MRZ lines that the OCR engine split into pieces.
The sample/ folder holds a small app that opens the scanner and shows the result. It has a switch to try gallery cropping on and off. It is separate from the library in passportscanner/ and uses it the way your own app would.
sample/androidApp: the Android app. sample/iosApp: the iOS app. sample/shared: the Compose screen both of them show../gradlew :sample:androidApp:installDebug
sample/iosApp/iosApp.xcodeproj in Xcode and run it on a device. The simulator has no camera, but gallery import works there.yyyy-MM-dd).MIT License. See LICENSE.
A Kotlin Multiplatform / Compose Multiplatform library that scans passports on Android and iOS from commonMain.
| Platform | Target | Minimum version | Camera / OCR |
|---|---|---|---|
| Android | android |
API 24 (Android 7.0) | CameraX + ML Kit Text Recognition |
| iOS (device) | iosArm64 |
iOS 14 | AVFoundation + Apple Vision |
Desktop (JVM), web (JS/Wasm) and iosX64 (Intel simulators) are not supported.
// build.gradle.kts of your shared module
kotlin {
sourceSets {
commonMain.dependencies {
implementation("io.github.dipeshlama:passport-scanner:<latest-version>")
}
}
}The library is published to Maven Central, so mavenCentral() is all you need in your repositories.
The latest release is built with these versions. Use the same or newer in your app to avoid dependency conflicts.
| Library | Version |
|---|---|
| Compose Multiplatform | 1.12.2 |
| Kotlin | 2.4.21 |
| Material 3 (Compose Multiplatform) | 1.12.0-alpha03 |
| CameraX | 1.4.2 |
| ML Kit text recognition | 19.0.1 |
Android: nothing to do. The library's manifest declares the CAMERA permission. It also tells Play services to download the ML Kit text model when the app is installed. If your app already sets com.google.mlkit.vision.DEPENDENCIES (for example ocr,face), add tools:replace="android:value" to your entry and keep ocr in its value.
iOS: add a camera usage description to your Info.plist:
<key>NSCameraUsageDescription</key>
<string>The camera is used to scan your passport.</string>The gallery uses PHPickerViewController, which needs no photo library permission.
@Composable
fun ScanPassportScreen() {
var scanning by remember { mutableStateOf(false) }
if (scanning) {
PassportScanner(
onResult = { result ->
scanning = false
when (result) {
is PassportScanResult.Success -> {
val mrz = result.data
// mrz.givenNames, mrz.surname, mrz.documentNumber, mrz.nationality,
// mrz.dateOfBirth, mrz.expiryDate, mrz.gender, mrz.issuingCountry
// result.image: JPEG of the passport page (ByteArray), if you need to upload it
}
is PassportScanResult.ImageOnly -> {
// MRZ unreadable; the user chose to keep the photo and fill in details manually
}
}
},
onCancel = { scanning = false },
)
} else {
Button(onClick = { scanning = true }) { Text("Scan passport") }
}
}PassportScanner is a full-screen composable, so it works as a destination in any navigation library (Voyager, Navigation Compose, Decompose and others) or as plain state in a screen, as above. It calls onResult once and then stops scanning, so navigate away when it does.
onCancel is called for the top bar's back button and for system back / the back gesture. The scanner consumes system back while it's shown, so back never falls through and closes your app. Open sheets (review, tips) take system back first and just close.
PassportScanner(
onResult = { /* ... */ },
onCancel = { /* ... */ },
config = PassportScannerConfig(
reviewBeforeReturning = true, // show a confirm/re-scan sheet first
allowGallery = true, // "Upload from gallery" button
allowImageOnlyResult = true, // offer "Use this photo" when the MRZ can't be read
showTips = true, // tips button and sheet
showTorch = true, // flashlight button (hidden if the device has none)
rejectExpired = false, // true: ask for another passport if this one has expired
cropGalleryImage = false, // true: let the user crop a gallery photo before it's read
),
strings = PassportScannerStrings(
title = stringResource(Res.string.scan_passport), // localise any text
),
)With cropGalleryImage = true, the user can crop a gallery photo before it's read. Only the cropped area is read and returned.
With rejectExpired = true, an expired passport is never returned: the scanner shows strings.expiredTitle with a "Scan another passport" button. A passport is still valid on its expiry date. To check it yourself instead, use result.data.isExpired().
By default, sheet colours and fonts come from your MaterialTheme; see Sheet colours and fonts to change them.
There are four levels; use whichever is enough.
1. Text: pass strings = PassportScannerStrings(...) and override any field.
2. Colours: the scan line that sweeps up and down, the frame corners, the scrim, the text/icon colour over the camera, the "Reading passport…" spinner and the camera permission prompt:
colors = PassportScannerDefaults.colors(
scanLine = Color(0xFF00C853),
frameCorners = Color.White,
scrim = Color.Black.copy(alpha = 0.5f),
content = Color.White,
processingIndicator = Color(0xFF00C853), // spinner while a photo is read
processingScrim = Color.Black.copy(alpha = 0.7f),
processingText = Color.White,
permissionText = Color.White, // "Allow camera access…" message
permissionButtonContainer = Color(0xFF00C853),
permissionButtonContent = Color.Black,
)3. Sheet colours and fonts: see below.
4. Slots: replace a piece of the UI with your own composable. Leave a slot null (the default) to keep the built-in one:
PassportScanner(
onResult = { /* ... */ },
onCancel = { /* ... */ },
title = { Text("Verify identity", style = MaterialTheme.typography.titleLarge) },
instruction = { PassportScannerDefaults.Instruction("Hold the photo page inside the frame") },
galleryButton = { onClick, enabled ->
TextButton(onClick = onClick, enabled = enabled) { Text("Choose photo") }
},
captureButton = { onClick, enabled ->
FilledIconButton(onClick = onClick, enabled = enabled) { Icon(MyIcons.Camera, "Capture") }
},
captureHint = { Text("Tap if auto-scan doesn't pick it up") },
)| Slot | Replaces | Default text from strings
|
|---|---|---|
title |
Title in the top bar | title |
instruction |
Message above the frame | instruction |
galleryButton(onClick, enabled) |
"Upload from gallery" button | gallery |
captureButton(onClick, enabled) |
Round shutter button |
capture (accessibility label) |
captureHint |
Message under the shutter button | captureHint |
Slots are drawn over the camera with LocalContentColor set to colors.content, so plain Text/Icon are readable without extra styling. The built-in pieces are public in PassportScannerDefaults (Title, Instruction, GalleryButton, CaptureButton, CaptureHint), so you can reuse or wrap them instead of starting from scratch.
The review, "couldn't read" and tips sheets take their own colours and text styles. Every parameter defaults to your MaterialTheme, so override only what you need:
val myFont = FontFamily(Font(Res.font.inter_regular), Font(Res.font.inter_medium, FontWeight.Medium))
PassportScanner(
onResult = { /* ... */ },
onCancel = { /* ... */ },
sheetColors = PassportScannerDefaults.sheetColors(
container = Color(0xFF1C1B1F),
title = Color.White,
body = Color(0xFFCAC4D0),
label = Color(0xFFCAC4D0),
value = Color.White,
divider = Color(0xFF49454F),
primaryButtonContainer = Color(0xFF00C853),
primaryButtonContent = Color.Black,
secondaryButtonContent = Color(0xFF00C853),
accent = Color(0xFF00C853),
),
sheetTypography = PassportScannerDefaults.sheetTypography(
fontFamily = myFont, // applied to every sheet text style
title = MaterialTheme.typography.headlineSmall,
),
)sheetColors |
Used for |
|---|---|
container, scrim, dragHandle
|
Sheet background, dimmed backdrop, top handle |
title, body
|
Sheet titles; descriptions and tips |
label, value, divider
|
Review sheet rows |
primaryButtonContainer, primaryButtonContent
|
"Confirm" / "Try again" |
secondaryButtonContent |
"Re-scan", "Use this photo", "Got it" (text and outline) |
accent |
MRZ highlight in the tips illustration |
sheetTypography has title, body, label, value and button styles. fontFamily is applied on top of whichever styles you pass, so you can change the font alone or both font and sizes.
PassportMrzData.toString() (and so PassportScanResult.Success.toString()) prints *** for every field. Read the properties (data.documentNumber, data.surname, …) when you need the values. Equality and copy() work as usual.
If you already have OCR text, use the parser on its own:
val data: PassportMrzData? = MrzParser.parse(listOf(line1, line2))MrzParser.find(List<OcrLine>) does the same with line positions, which lets it rebuild MRZ lines that the OCR engine split into pieces.
The sample/ folder holds a small app that opens the scanner and shows the result. It has a switch to try gallery cropping on and off. It is separate from the library in passportscanner/ and uses it the way your own app would.
sample/androidApp: the Android app. sample/iosApp: the iOS app. sample/shared: the Compose screen both of them show../gradlew :sample:androidApp:installDebug
sample/iosApp/iosApp.xcodeproj in Xcode and run it on a device. The simulator has no camera, but gallery import works there.yyyy-MM-dd).MIT License. See LICENSE.