Integrate document scanning functionality into your Android app using Google ML Kit and Jetpack Compose.
- Scans documents using Google ML Kit's Document Scanner.
- Supports JPEG and PDF formats.
- Customizable scanner options (page limit, scanner mode, etc.).
- Simple and user-friendly UI with Jetpack Compose.



-
Clone the repository:
git clone https://github.com/Bhavyansh03-tech/Document_scanner.git
-
Open the project in Android Studio.
-
Build the project and run it on an emulator or a physical device.
- Dependencies
Add the following dependencies to your
build.gradle
file:
implementation 'com.google.mlkit:document-scanner:16.0.0-beta1'
implementation 'io.coil-kt:coil-compose:2.7.0'
- Usage Here's the code to implement the document scanner:
val options = GmsDocumentScannerOptions.Builder()
.setGalleryImportAllowed(false)
.setResultFormats(
GmsDocumentScannerOptions.RESULT_FORMAT_JPEG,
GmsDocumentScannerOptions.RESULT_FORMAT_PDF
)
.setPageLimit(2)
.setScannerMode(SCANNER_MODE_FULL)
.build()
var selectedImageUri by remember {
mutableStateOf(Uri.EMPTY)
}
val scanner = GmsDocumentScanning.getClient(options)
val scannerLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.StartIntentSenderForResult()
) { result ->
if (result.resultCode == RESULT_OK) {
val scannerResult =
GmsDocumentScanningResult.fromActivityResultIntent(result.data)
scannerResult?.pages?.let { pages ->
for (page in pages) {
selectedImageUri = pages[0].imageUri
}
}
}
}
// Context
val context = LocalContext.current as Activity
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
AsyncImage(
model = selectedImageUri,
contentDescription = null,
contentScale = ContentScale.Crop, modifier = Modifier.size(200.dp)
)
Spacer(modifier = Modifier.height(10.dp))
Button(onClick = {
scanner.getStartScanIntent(context)
.addOnSuccessListener { intentSender ->
scannerLauncher.launch(
IntentSenderRequest
.Builder(intentSender)
.build()
)
}
}) {
Text(text = "Start Scanning")
}
}
Contributions are welcome! Please fork the repository and submit a pull request for any improvements or bug fixes.
- Fork the repository.
- Create your feature branch (
git checkout -b feature/your-feature
). - Commit your changes (
git commit -am 'Add some feature'
). - Push to the branch (
git push origin feature/your-feature
). - Create a new Pull Request.
For questions or feedback, please contact @Bhavyansh03-tech.