Skip to content

This project shows the implementation for the Machine Learning Document Scanner. The sample project contains a button, open the camera and scan the document, and using the Intent to open and preview the pdf file.

License

Notifications You must be signed in to change notification settings

NicosNicolaou16/MLDocumentScanner

Repository files navigation

ML Document Scanner

This project shows the implementation for the Machine Learning Document Scanner. The sample project contains a button, open the camera and scan the document, and using the Intent to open and preview the pdf file.

Example

Step 1 - Add the library

Groovy

ext {
    playServicesMlkitDocumentScanner = "16.0.0-beta1"
}

dependencies {
    implementation 'com.google.android.gms:play-services-mlkit-document-scanner:$playServicesMlkitDocumentScanner'
}

Kotlin DSL

ext {
    playServicesMlkitDocumentScanner = "16.0.0-beta1"
}

dependencies {
    implementation("com.google.android.gms:play-services-mlkit-document-scanner:$playServicesMlkitDocumentScanner")
}

libs.versions.toml

[version]
playServicesMlkitDocumentScanner = "16.0.0-beta1"

[libraries]
play-services-mlkit-document-scanner = { group = "com.google.android.gms", name = "play-services-mlkit-document-scanner", version.ref = "playServicesMlkitDocumentScanner" }
dependencies {
    // ML Kit
    implementation(libs.play.services.mlkit.document.scanner)
}

Step 2 - Setup the Builder

Builder Configuration

val options = GmsDocumentScannerOptions.Builder().apply {
    setGalleryImportAllowed(false)
    setPageLimit(2)
    setResultFormats(RESULT_FORMAT_JPEG, RESULT_FORMAT_PDF)
    setScannerMode(SCANNER_MODE_FULL)
}.build()

Step 3 - Main Implementation

Get the PDF URI

@Composable
fun Scanner(
    innerPadding: PaddingValues
) {
    /**
     * registerForActivityResult for Activity/Fragment instead of the rememberLauncherForActivityResult (For Compose)
     * */
    val scannerLauncher =
        rememberLauncherForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result ->
            if (result.resultCode == RESULT_OK) {
                val data =
                    GmsDocumentScanningResult.fromActivityResultIntent(result.data)
                /**
                 * Option 1 to show the pdf as image uri
                 * */
                data?.pages?.let { pages ->
                    for (page in pages) {
                        val imageUri = page.imageUri
                    }
                }
                /**
                 * Option 2 to show the pdf as pdf uri
                 * */
                data?.pdf?.let { pdf ->
                    val pdfUri = pdf.uri
                    val pageCount = pdf.pageCount
                    // handle pdf uri and open it via Intent
                    openPdfWithIntent(pdfUri)
                }
            }
        }

    // Other Code Here - UI
}

Set the Builder Configuration

val scanner = GmsDocumentScanning.getClient(options)

UI

@Composable
fun Scanner(
    innerPadding: PaddingValues
) {
    //Other Code Here

    ElevatedButton(
        content = {
            Text(
                text = stringResource(id = R.string.scan),
                style = TextStyle(fontSize = 21.sp)
            )
        },
        modifier = Modifier.size(height = 70.dp, width = 250.dp),
        onClick = {
            /**
             * start the scanner
             * */
            scanner.getStartScanIntent(this@MainActivity)
                .addOnSuccessListener { intentSender ->
                    scannerLauncher.launch(
                        IntentSenderRequest.Builder(intentSender).build()
                    )
                }
                .addOnFailureListener {
                    Log.d("exception", "error")
                }
        }
    )
}

Versioning

Target SDK version: 35
Minimum SDK version: 28
Kotlin version: 2.1.0
Gradle version: 8.7.2

References

https://android-developers.googleblog.com/2024/02/ml-kit-document-scanner-api.html
https://developers.google.com/ml-kit/vision/doc-scanner
https://developers.google.com/ml-kit/vision/doc-scanner/android#kotlin

About

This project shows the implementation for the Machine Learning Document Scanner. The sample project contains a button, open the camera and scan the document, and using the Intent to open and preview the pdf file.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages