22 February 2024
We are excited to announce the launch of the ML Kit Document Scanner API. This new API makes it easy to add advanced document scanning capabilities with a high-quality and consistent user interface to your Android app. The ML Kit Document Scanner API enables your users to quickly and easily digitize paper documents.
Like the other ML Kit APIs, the ML Kit Document Scanner API enables you to seamlessly integrate features powered by Machine Learning (ML) without any ML knowledge.
Despite the digital revolution, paper documents and printouts are still present in our everyday life. Some of our most important documents are still physical (identity documents, receipts, etc.).
The ML Kit Document Scanner API offers a number of benefits, including:
The ML Kit Document Scanner API is already used by Google Drive Android application and the Google Pixel Camera.
The ML Kit Document Scanner API requires Android API level 21 or above. The models, scanning logic, and UI flow are dynamically downloaded via Google Play services so the ML Kit Document Scanner API has a minimal impact on your app size.
To integrate it in your app, start by configuring the scanner options and getting a scanner client:
val options = GmsDocumentScannerOptions.Builder()
.setGalleryImportAllowed(false)
.setPageLimit(2)
.setResultFormats(RESULT_FORMAT_JPEG, RESULT_FORMAT_PDF)
.setScannerMode(SCANNER_MODE_FULL)
.build()
val scanner = GmsDocumentScanning.getClient(options)
Then register an ActivityResultCallback to receive the scanning results:
val scannerLauncher = registerForActivityResult(StartIntentSenderForResult()) {
result -> {
if (result.resultCode == RESULT_OK) {
val result =
GmsDocumentScanningResult.fromActivityResultIntent(result.data)
result.getPages()?.let { pages ->
for (page in pages) {
val imageUri = page.getImageUri()
}
}
result.getPdf()?.let { pdf ->
val pdfUri = pdf.getUri()
val pageCount = pdf.getPageCount()
}
}
}
}
Finally launch the document scanner activity:
scanner.getStartScanIntent(activity)
.addOnSuccessListener { intentSender ->
scannescannerrLauncher.launch(IntentSenderRequest.Builder(intentSender).build())
}
.addOnFailureListener { ... }
To get started with the ML Kit Document Scanner API, visit the documentation. We can’t wait to see what you’ll build with it!