Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
Escáner de documentos con ML Kit en Android
Usa la API de Document Scanner de ML Kit para agregar fácilmente una función de escáner de documentos a tu app.
Función
Detalles
Nombre del SDK
play-services-mlkit-document-scanner
Implementación
Los Servicios de Google Play descargan de forma dinámica los modelos, la lógica de análisis y el flujo de la IU.
Impacto del tamaño de la app
Aumento de aproximadamente 300 KB en el tamaño de descarga.
Hora de inicialización
Es posible que los usuarios deban esperar a que se descarguen los modelos, la lógica y el flujo de la IU antes del primer uso.
Probar
Prueba la app de ejemplo para ver un ejemplo de uso de esta API.
Antes de comenzar
En tu archivo build.gradle de nivel de proyecto, asegúrate de incluir el repositorio Maven de Google en las secciones buildscript y allprojects.
Agrega la dependencia para la biblioteca del escáner de documentos de ML Kit al archivo Gradle a nivel de la app de tu módulo, que suele ser app/build.gradle:
El SDK proporciona el flujo de usuarios del escáner de documentos (que incluye una pantalla de visor y una pantalla de vista previa dedicadas). El visor y la pantalla de vista previa admiten los siguientes controles personalizables:
Importar desde la galería de fotos
establecer un límite para la cantidad de páginas escaneadas
modo de escáner (para controlar los conjuntos de atributos en el flujo)
Puedes recuperar archivos PDF y JPEG de tus documentos escaneados.
Crea una instancia de GmsDocumentScannerOptions para configurar las opciones del escáner:
Kotlin
val options = GmsDocumentScannerOptions.Builder()
.setGalleryImportAllowed(false)
.setPageLimit(2)
.setResultFormats(RESULT_FORMAT_JPEG, RESULT_FORMAT_PDF)
.setScannerMode(SCANNER_MODE_FULL)
.build()
Después de crear tu GmsDocumentScannerOptions, obtén una instancia de GmsDocumentScanner. Luego, puedes iniciar la actividad del escáner siguiendo las APIs de Activity Result que se introdujeron en AndroidX.
Cuando se complete el escaneo de documentos, un objeto GmsDocumentScanningResult brindará acceso a la cantidad de páginas escaneadas, los URIs de las imágenes en formato JPEG y PDF según lo que se definió a través de setResultFormats:
Ten en cuenta que generar archivos de documentos lleva tiempo y requiere potencia de procesamiento, por lo que solo solicita los formatos de salida (JPEG, PDF o ambos) que realmente necesitas.
[null,null,["Última actualización: 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."]]