Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Dokumentenscanner mit ML Kit auf Android-Geräten
Mit der ML Kit Document Scanner API können Sie Ihrer App ganz einfach eine Dokumentenscannerfunktion hinzufügen.
Feature
Details
SDK-Name
play-services-mlkit-document-scanner
Implementierung
Die Modelle, die Scanlogik und der UI-Flow werden dynamisch von den Google Play-Diensten heruntergeladen.
Auswirkungen auf die App-Größe
Die Downloadgröße erhöht sich um etwa 300 KB.
Initialisierungszeit
Vor der ersten Verwendung müssen die Nutzer möglicherweise warten, bis die Modelle, die Logik und der UI-Ablauf heruntergeladen wurden.
Jetzt ausprobieren
In der Beispielanwendung sehen Sie ein Beispiel für die Verwendung dieser API.
Hinweis
In die Datei build.gradle auf Projektebene muss das Maven-Repository von Google in die Abschnitte „buildscript“ und „allprojects“ aufgenommen werden.
Fügen Sie die Abhängigkeit für die ML Kit-Bibliothek für Dokumentenscanner der Gradle-Datei Ihres Moduls auf App-Ebene hinzu, die in der Regel „app/build.gradle“ lautet:
Der Nutzerfluss des Dokumentenscanners (einschließlich eines speziellen Sucher- und Vorschaubildschirms) wird vom SDK bereitgestellt. Im Sucher und auf dem Vorschaubildschirm sind die folgenden anpassbaren Steuerelemente verfügbar:
Import aus der Fotogalerie
Begrenzung der Anzahl der gescannten Seiten
Scannermodus (zur Steuerung der Funktionssätze im Ablauf)
Sie können sowohl PDF- als auch JPEG-Dateien für Ihre gescannten Dokumente abrufen.
Instanziere GmsDocumentScannerOptions, um die Scanneroptionen zu konfigurieren:
Kotlin
val options = GmsDocumentScannerOptions.Builder()
.setGalleryImportAllowed(false)
.setPageLimit(2)
.setResultFormats(RESULT_FORMAT_JPEG, RESULT_FORMAT_PDF)
.setScannerMode(SCANNER_MODE_FULL)
.build()
Nachdem Sie Ihre GmsDocumentScannerOptions erstellt haben, rufen Sie eine Instanz von GmsDocumentScanner ab. Sie können dann die Scanneraktivität gemäß den in AndroidX eingeführten Activity Result APIs starten.
Wenn das Scannen des Dokuments abgeschlossen ist, bietet ein GmsDocumentScanningResult-Objekt Zugriff auf die Anzahl der gescannten Seiten, die URIs der Bilder im JPEG-Format und PDF gemäß den über setResultFormats definierten Angaben:
Das Erstellen von Dokumentdateien erfordert Zeit und Rechenleistung. Fordern Sie daher nur die Ausgabeformate (JPEG, PDF oder beide) an, die Sie tatsächlich benötigen.
[null,null,["Zuletzt aktualisiert: 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."]]