Quy trình người dùng của trình quét tài liệu (bao gồm màn hình kính ngắm chuyên dụng và màn hình xem trước) do SDK cung cấp. Khung ngắm và màn hình xem trước hỗ trợ các chế độ điều khiển có thể tuỳ chỉnh sau:
nhập từ thư viện ảnh
đặt giới hạn số lượng trang được quét
chế độ quét (để kiểm soát các nhóm tính năng trong luồng)
Bạn có thể truy xuất cả tệp PDF và JPEG cho tài liệu đã quét.
Tạo bản sao GmsDocumentScannerOptions để định cấu hình các tuỳ chọn của trình quét:
Kotlin
val options = GmsDocumentScannerOptions.Builder()
.setGalleryImportAllowed(false)
.setPageLimit(2)
.setResultFormats(RESULT_FORMAT_JPEG, RESULT_FORMAT_PDF)
.setScannerMode(SCANNER_MODE_FULL)
.build()
Sau khi tạo GmsDocumentScannerOptions, hãy lấy một bản sao của GmsDocumentScanner. Sau đó, bạn có thể bắt đầu hoạt động của trình quét theo Activity Result API (API Kết quả hoạt động) được giới thiệu trong AndroidX.
Khi quá trình quét tài liệu hoàn tất, đối tượng GmsDocumentScanningResult sẽ cấp quyền truy cập vào số trang đã quét, URI của hình ảnh ở định dạng JPEG và PDF theo nội dung đã xác định thông qua setResultFormats:
Hãy cân nhắc rằng việc tạo tệp tài liệu sẽ mất thời gian và cần có sức mạnh xử lý, vì vậy, chỉ yêu cầu các định dạng đầu ra (JPEG, PDF hoặc cả hai) mà bạn thực sự cần.
[null,null,["Cập nhật lần gần đây nhất: 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."]]