Alur penggunaan pemindai dokumen (yang mencakup layar jendela bidik khusus
dan layar pratinjau) disediakan oleh SDK. Jendela bidik dan layar pratinjau mendukung kontrol yang dapat disesuaikan berikut:
mengimpor dari galeri foto
menetapkan batas jumlah halaman yang dipindai
mode pemindai (untuk mengontrol kumpulan fitur dalam alur)
Anda dapat mengambil file PDF dan JPEG untuk dokumen yang dipindai.
Buat instance GmsDocumentScannerOptions untuk mengonfigurasi opsi pemindai:
Kotlin
val options = GmsDocumentScannerOptions.Builder()
.setGalleryImportAllowed(false)
.setPageLimit(2)
.setResultFormats(RESULT_FORMAT_JPEG, RESULT_FORMAT_PDF)
.setScannerMode(SCANNER_MODE_FULL)
.build()
Setelah membuat GmsDocumentScannerOptions, dapatkan
instance GmsDocumentScanner. Kemudian, Anda dapat memulai aktivitas pemindai
mengikuti
Activity Result API
yang diperkenalkan di AndroidX.
Setelah pemindaian dokumen selesai, objek GmsDocumentScanningResult akan memberikan akses ke jumlah halaman yang dipindai, URI gambar dalam format JPEG dan PDF sesuai dengan yang ditentukan melalui setResultFormats:
Perhatikan bahwa pembuatan file dokumen memerlukan waktu dan daya pemrosesan, jadi hanya minta format output (JPEG, atau PDF, atau keduanya) yang benar-benar Anda perlukan.
[null,null,["Terakhir diperbarui pada 2025-08-29 UTC."],[[["\u003cp\u003eEasily add a document scanning feature to your Android app using the ML Kit document scanner API.\u003c/p\u003e\n"],["\u003cp\u003eThe API provides a customizable UI flow for document scanning, including options for importing from the photo gallery, setting page limits, and adjusting scanner modes.\u003c/p\u003e\n"],["\u003cp\u003eRetrieve scanned documents as JPEG and/or PDF files using the \u003ccode\u003eGmsDocumentScanningResult\u003c/code\u003e object.\u003c/p\u003e\n"],["\u003cp\u003eEnsure your app has a minSdkVersion of 21 or higher and a minimum device RAM of 1.7GB to use the API.\u003c/p\u003e\n"],["\u003cp\u003eOptimize performance by only requesting necessary output formats (JPEG and/or PDF).\u003c/p\u003e\n"]]],["The ML Kit document scanner API on Android enables adding a document scanning feature to apps. It requires Android API level 21+ and 1.7GB+ RAM. Implementation involves adding a dependency and configuring `GmsDocumentScannerOptions` to customize settings like gallery import, page limits, and output formats (JPEG/PDF). After obtaining an instance of `GmsDocumentScanner`, you use `Activity Result APIs` to start the scanner. The `GmsDocumentScanningResult` provides access to scanned pages' URIs and PDF details.\n"],null,["Document scanner with ML Kit on Android\n\nUse the ML Kit document scanner API to easily add a document scanner feature to\nyour app.\n\n| **Feature** | **Details** |\n|---------------------|--------------------------------------------------------------------------------------------|\n| Sdk name | play-services-mlkit-document-scanner |\n| Implementation | The models, scanning logic and UI flow are dynamically downloaded by Google Play services. |\n| App size impact | \\~300KB download size increase. |\n| Initialization time | Users might have to wait for the models, logic and UI flow to download before first use. |\n\nTry it out\n\nPlay around with the\n[sample app](https://github.com/googlesamples/mlkit/tree/master/android/documentscanner)\nto see an example usage of this API.\n\nBefore you begin **Note:** this API requires Android API level 21 or above. Make sure that your app's build file uses a minSdkVersion value of 21 or higher. It also requires a minimal [device total RAM](https://developer.android.com/reference/android/app/ActivityManager.MemoryInfo#totalMem) of 1.7GB. If lower, it returns an `MlKitException` with error code `UNSUPPORTED` when calling the API.\n\n1. In your project-level `build.gradle` file, make sure to include Google's\n Maven repository in both your buildscript and allprojects sections.\n\n2. Add the dependency for the ML Kit document scanner library to your\n module's app-level gradle file, which is usually app/build.gradle:\n\n dependencies {\n // ...\n implementation 'com.google.android.gms:play-services-mlkit-document-scanner:16.0.0-beta1'\n }\n\nDocument Scanner configuration\n\nThe document scanner user flow (which includes a dedicated viewfinder screen\nand preview screen) is provided by the SDK. The viewfinder and preview screen supports the following customizable controls:\n\n- importing from the photo gallery\n- setting a limit to the number of pages scanned\n- scanner mode (to control the feature sets in the flow)\n\nYou can retrieve both PDF and JPEG files for your scanned documents.\n\nInstantiate `GmsDocumentScannerOptions` to configure the scanner options: \n\nKotlin \n\n```scdoc\nval options = GmsDocumentScannerOptions.Builder()\n .setGalleryImportAllowed(false)\n .setPageLimit(2)\n .setResultFormats(RESULT_FORMAT_JPEG, RESULT_FORMAT_PDF)\n .setScannerMode(SCANNER_MODE_FULL)\n .build()\n```\n\nJava \n\n```scdoc\nGmsDocumentScannerOptions options = new GmsDocumentScannerOptions.Builder()\n .setGalleryImportAllowed(false)\n .setPageLimit(2)\n .setResultFormats(RESULT_FORMAT_JPEG, RESULT_FORMAT_PDF)\n .setScannerMode(SCANNER_MODE_FULL)\n .build();\n```\n\nScan documents\n\nAfter creating your `GmsDocumentScannerOptions`, get an\ninstance of `GmsDocumentScanner`. You can then start the scanner activity\nfollowing\n[Activity Result APIs](https://developer.android.com/training/basics/intents/result)\nintroduced in AndroidX.\n\nWhen the document scanning is complete, a `GmsDocumentScanningResult` object will give access to the number of pages scanned, the URIs of the\nimages in JPEG format and PDF accordingly to what was defined via\n`setResultFormats`: \n\nKotlin \n\n```verilog\nval scanner = GmsDocumentScanning.getClient(options)\nval scannerLauncher = registerForActivityResult(StartIntentSenderForResult()) {\n result -\u003e {\n if (result.resultCode == RESULT_OK) {\n val result =\n GmsDocumentScanningResult.fromActivityResultIntent(result.data)\n result.getPages()?.let { pages -\u003e\n for (page in pages) {\n val imageUri = pages.get(0).getImageUri()\n }\n }\n result.getPdf()?.let { pdf -\u003e\n val pdfUri = pdf.getUri()\n val pageCount = pdf.getPageCount()\n }\n }\n }\n}\n\nscanner.getStartScanIntent(activity)\n .addOnSuccessListener { intentSender -\u003e\n scannerLauncher.launch(IntentSenderRequest.Builder(intentSender).build())\n }\n .addOnFailureListener {\n ...\n }\n```\n\nJava \n\n```verilog\nGmsDocumentScanner scanner = GmsDocumentScanning.getClient(options);\nActivityResultLauncher\u003cIntentSenderRequest\u003e scannerLauncher =\n registerForActivityResult(\n new StartIntentSenderForResult(),\n result -\u003e {\n if (result.getResultCode() == RESULT_OK) {\n GmsDocumentScanningResult result = GmsDocumentScanningResult.fromActivityResultIntent(result.getData());\n for (Page page : result.getPages()) {\n Uri imageUri = pages.get(0).getImageUri();\n }\n\n Pdf pdf = result.getPdf();\n Uri pdfUri = pdf.getUri();\n int pageCount = pdf.getPageCount();\n }\n });\n\nscanner.getStartScanIntent(activity)\n .addOnSuccessListener(intentSender -\u003e\n scannerLauncher.launch(new IntentSenderRequest.Builder(intentSender).build()))\n .addOnFailureListener(...);\n```\n\nTips to improve performance\n\nConsider that generating document files takes time and requires processing\npower, so only request the output formats (JPEG, or PDF, or both) you actually\nneed."]]