Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Scanner di documenti con ML Kit su Android
Utilizza l'API Scanner di ML Kit per aggiungere facilmente una funzionalità di scanner alla tua app.
Funzionalità
Dettagli
Nome SDK
play-services-mlkit-document-scanner
Implementazione
I modelli, la logica di scansione e il flusso dell'interfaccia utente vengono scaricati dinamicamente da Google Play Services.
Impatto delle dimensioni dell'app
Aumento delle dimensioni del download di circa 300 KB.
Tempo di inizializzazione
Gli utenti potrebbero dover attendere il download dei modelli, della logica e del flusso dell'interfaccia utente prima del primo utilizzo.
Prova
Prova la
app di esempio
per vedere un esempio di utilizzo di questa API.
Prima di iniziare
Nel file build.gradle a livello di progetto, assicurati di includere il repository Maven di Google sia nelle sezioni buildscript che in allprojects.
Aggiungi la dipendenza per la libreria dello Scanner di documenti di ML Kit al file Gradle a livello di app del tuo modulo, che in genere è app/build.gradle:
Il flusso utente dello scanner di documenti (che include una schermata del mirino e una schermata di anteprima dedicata) è fornito dall'SDK. Il mirino e la schermata di anteprima supportano i seguenti controlli personalizzabili:
Importazione dalla galleria fotografica
impostando un limite al numero di pagine scansionate
modalità di scansione (per controllare gli insiemi di funzionalità nel flusso)
Puoi recuperare i file PDF e JPEG dei documenti scansionati.
Crea un'istanza di GmsDocumentScannerOptions per configurare le opzioni dello scanner:
Kotlin
val options = GmsDocumentScannerOptions.Builder()
.setGalleryImportAllowed(false)
.setPageLimit(2)
.setResultFormats(RESULT_FORMAT_JPEG, RESULT_FORMAT_PDF)
.setScannerMode(SCANNER_MODE_FULL)
.build()
Dopo aver creato GmsDocumentScannerOptions, ottieni un'istanza di GmsDocumentScanner. Puoi quindi avviare l'attività dello scanner seguendo le API Result Activity introdotte in AndroidX.
Al termine della scansione del documento, un oggetto GmsDocumentScanningResult consente di accedere al numero di pagine scansionate, agli URI delle immagini in formato JPEG e PDF in base a quanto definito tramite setResultFormats:
Tieni presente che la generazione dei file dei documenti richiede tempo e potenza di elaborazione, quindi richiedi solo i formati di output (JPEG, PDF o entrambi) di cui hai effettivamente bisogno.
[null,null,["Ultimo aggiornamento 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."]]