← Back to Blog

How to create a document scanner using ML Kit and Jetpack Compose

Jordan Mungujakisa
7 min read

ML Kit Document Scanner Demo

Google recently released their ML Kit document scanner API that can help you easily add a powerful AI-powered document scanner to your app. In this blog post, I will go through a step-by-step guide to implementing it in your app. So, let’s dive right in.

1. Add the Dependency

Add the following dependency to your app-level build.gradle file:

implementation 'com.google.android.gms:play-services-mlkit-document-scanner:16.0.0-beta1'

2. Set Document Scanner Options

Define the scanner options:

  • Allow imports from the gallery
  • Set a limit for the maximum number of pages scanned
  • Set the scanner mode to control the feature sets in the workflow
val options = GmsDocumentScannerOptions.Builder()
    .setGalleryImportAllowed(true)
    .setPageLimit(100)
    .setResultFormats(RESULT_FORMAT_JPEG, RESULT_FORMAT_PDF)
    .setScannerMode(SCANNER_MODE_FULL)
    .build()

3. Create the Document Scanner Instance

Obtain the client and pass in the scanner options:

val scanner = GmsDocumentScanning.getClient(options)

4. Register for Activity Result

Use the activity callback methods to launch the document scanner activity and receive a result:

val scannerLauncher =
     registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result ->
     if (result.resultCode == RESULT_OK) {
        handleResult(result)
      }
   }

5. Launch the Scanner Activity

Obtain an IntentSender and launch the scanner activity:

scanner.getStartScanIntent(context)
        .addOnSuccessListener { intentSender ->
              scannerLauncher.launch(
                 IntentSenderRequest
                 .Builder(intentSender)
                 .build()
         )
        }.addOnFailureListener {
            Toast.makeText(context, "${it.message}", Toast.LENGTH_SHORT).show()
        }

6. Handle the Scanner Result

Store the PDF meta information in a Room database for later retrieval:

fun handleResult(result: ActivityResult) {
        val currentTime = System.currentTimeMillis()
        val scanResult = GmsDocumentScanningResult.fromActivityResultIntent(result.data)
        scanResult?.pdf?.let { pdf ->
            val pdfUri = pdf.uri
            val pageCount = pdf.pageCount
            // do something with the result
        }
    }

7. Demo and Source Code

Here’s a demonstration of how the PDF Document Scanner I created works.

PDF Document Scanner Demo

You can download the app on the Google Play Store here: https://play.google.com/store/apps/details?id=com.keru.pdfcreator

The source code is here: https://github.com/jordan-jakisa/pdf_creator

Please star the repo if you found this article useful 🙏

Ciao 🤓


Originally published on Medium

Mobile app alchemist who is trying to transmute elegant designs, into elegant code, into beautiful mobile app experiences.

0 Comments

No comments yet. Be the first to share your thoughts!