@capacitor-mlkit/document-scanner

中文文档
8.2.2发布于 2026/10/31Apache-2.0TypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 7.7万

Android上的ML Kit文档扫描器的电容插件.

中文使用文档
文档目录28 节
  1. 使用大小写
  2. 兼容性
  3. 安装
  4. 机器人
  5. 使用量
  6. 扫描文档
  7. 检查并安装 Google 文件扫描器模块
  8. API 密码
  9. 扫描文档(...)
  10. 是GoogleDocumentScannerModuleAvailable( ) 文档扫描器
  11. 安装 GoogleDocumentScannerModule ()
  12. 添加倾听器( “ googleDocumentScannerModule Install Progress” ,...)
  13. 删除所有收听器( )
  14. 接口
  15. 数字
  16. 页:1
  17. 财务问题
  18. 此插件支持哪些平台 ?
  19. 为什么文档扫描仪不能首先使用?
  20. 从扫描中可以得到哪些结果格式?
  21. 扫描器模式有什么区别?
  22. 是否有设备要求?
  23. 用户可以导入已有相片而不是扫描吗 ?
  24. 相关插件
  25. 术语和隐私
  26. 通讯
  27. 更改日志
  28. 许可证

ML Kit 文档扫描器插件

非官方电容插件 ML Kit 文档扫描器[^1]

使用大小写

文档扫描器插件通常用于应用程序需要将纸面文档与相机数字化时,例如:

  • 收支记录:让用户扫描收据和发票作为JPEG图像或PDF文件进行费用跟踪.
  • 文档存档:将合同、信件和其他文件数字化为多页PDF文件.
  • 提交的表格:让用户在您的应用程序中扫描并提交签名的表格或辅助文档.
  • 清除文档相片: 使用由ML驱动的图像清理能力去除扫描页上的污点和指头.

兼容性

插件版本电容版本状态
8.x.x (英语)>=8.x.x积极支持

安装

你可以用我们的 AI 辅助设置 以安装插件. 添加数据 超级技能 至您的 AI 工具,使用以下命令:

npx skills add capawesome-team/skills --skill capacitor-plugins

然后使用下列提示:

Use the `capacitor-plugins` skill from `capawesome-team/skills` to install the `@capacitor-mlkit/document-scanner` plugin in my project.

如果你喜欢 手动设置,通过运行以下命令来安装插件,并遵循下面的平台特定指令:

npm install @capacitor-mlkit/document-scanner
npx cap sync

机器人

变量

如果需要, 您可以在您的应用程序中定义以下工程变量 variables.gradle 要更改依赖的默认版本的文件 :

  • $mlkitDocumentScannerVersion 版本 com.google.android.gms:play-services-mlkit-document-scanner (默认 : 16.0.0(中文(简体) )

如果您在工程中遇到其它插件的依赖性冲突, 这会有所帮助 .

使用量

以下例子显示了如何扫描文档以及如何检查并安装Google文档扫描器模块.

扫描文档

启动文档扫描程序,打开ML Kit文档扫描器 UI. 您可以允许图片库导入,限制页面数量,并选择结果格式和扫描器模式. 仅在Android上可用:

import { DocumentScanner } from '@capacitor-mlkit/document-scanner';

const scanDocument = async () => {
  const result = await DocumentScanner.scanDocument({
    galleryImportAllowed: true,
    pageLimit: 5,
    resultFormats: 'JPEG_PDF',
    scannerMode: 'FULL',
  });

  console.log('Scanned images:', result.scannedImages);
  console.log('PDF info:', result.pdf);
};

在Android上,系统可能在扫描仪打开时破坏应用程序. 在这种情况下,承诺永远不会实现。 相反,结果是通过 appRestoredResult 一、导 言 应用插件 当应用程序恢复时:

import { App } from '@capacitor/app';

App.addListener('appRestoredResult', event => {
  if (event.pluginId !== 'DocumentScanner' || event.methodName !== 'scanDocument') {
    return;
  }
  if (event.success) {
    const scannedImages = event.data.scannedImages;
  } else {
    console.error(event.error?.message);
  }
});

检查并安装 Google 文件扫描器模块

文档扫描器模型,扫描逻辑,和UI流被Google Play服务动态下载. 检查模块是否可用, 并在需要时安装 。 安装只从此调用开始 googleDocumentScannerModuleInstallProgress 事件向您通报进展。 仅在Android上可用:

import { DocumentScanner } from '@capacitor-mlkit/document-scanner';

const isGoogleDocumentScannerModuleAvailable = async () => {
  const result = await DocumentScanner.isGoogleDocumentScannerModuleAvailable();
  console.log('Is Google Document Scanner module available:', result.available);
};

const installGoogleDocumentScannerModule = async () => {
  await DocumentScanner.installGoogleDocumentScannerModule();
  console.log('Google Document Scanner module installation started.');
};

DocumentScanner.addListener('googleDocumentScannerModuleInstallProgress', (event) => {
  console.log('Installation progress:', event.progress, '%');
  console.log('Current state:', event.state);
});

API 密码

扫描文档(...)

scanDocument(options: ScanOptions) => Promise<ScanResult>

开始文档扫描进程.

请注意: 打开 机器人,系统可能在扫描仪打开时破坏应用程序。 在这种情况下,承诺 决不会下定决心 相反,结果是通过 appRestoredResult 一、导 言 应用插件 当应用程序恢复时.

只能从Android上找到.

参数类型
optionsScanOptions

回返情况: Promise<ScanResult>

自: 7.3.0 (单位:千美元)


是GoogleDocumentScannerModuleAvailable( ) 文档扫描器

isGoogleDocumentScannerModuleAvailable() => Promise<IsGoogleDocumentScannerModuleAvailableResult>

请检查google文档扫描器模块是否可用 .

如果Google文档扫描器模块不可用,您可以通过使用 installGoogleDocumentScannerModule()。 。 。 .

只能从Android上找到.

回返情况: Promise<IsGoogleDocumentScannerModuleAvailableResult>

自: 7.3.0 (单位:千美元)


安装 GoogleDocumentScannerModule ()

installGoogleDocumentScannerModule() => Promise<void>

安装谷歌文档扫描器模块 .

请注意:这只开始安装. 这个 googleDocumentScannerModuleInstallProgress 事件听众会 安装完成后通知您.

只能从Android上找到.

自: 7.3.0 (单位:千美元)


添加倾听器( “ googleDocumentScannerModule Install Progress” ,...)

addListener(eventName: 'googleDocumentScannerModuleInstallProgress', listenerFunc: (event: GoogleDocumentScannerModuleInstallProgressEvent) => void) => Promise<PluginListenerHandle>

当Google文档扫描器模块安装时调用.

只能从Android上找到.

参数类型
eventName'googleDocumentScannerModuleInstallProgress'
listenerFunc(event: GoogleDocumentScannerModuleInstallProgressEvent) => void

回返情况: Promise<PluginListenerHandle>

自: 7.3.0 (单位:千美元)


删除所有收听器( )

removeAllListeners() => Promise<void>

删除此插件的所有听众 .

只能从Android上找到.

自: 7.3.0 (单位:千美元)


接口

扫描结果

文档扫描操作结果 .

Prop类型说明自兹
scannedImagesstring[]用于扫描图像页面的URI阵列(JPEG). 如果结果格式中请求“ JPEG ” 或“ JPEG PDF ” , 则显示 .7.3.0 (单位:千美元)
pdfPdfInfo关于生成的PDF的信息. 如果结果Formats中要求“ PDF” 或“ JPEG PDF ” , 则显示 .7.3.0 (单位:千美元)

PdfInfo 数据

Prop类型说明自兹
uristring生成的 PDF 文件的 URI .7.3.0 (单位:千美元)
pageCountnumberPDF中的页数.7.3.0 (单位:千美元)

扫描选项

Prop类型说明默认自兹
galleryImportAllowedboolean是否允许从相册导入 .false7.3.0 (单位:千美元)
pageLimitnumber可扫描的最大页数.107.3.0 (单位:千美元)
resultFormats'JPEG' | 'PDF' | 'JPEG_PDF'预期成果格式。 可以是"JPEG","PDF",也可以是"JPEG PDF".'JPEG_PDF'7.3.0 (单位:千美元)
scannerMode'FULL' | 'BASE' | 'BASE_WITH_FILTER'扫描仪模式。 BASE:基本编辑能力(剪接,旋转,重排页面等). BASE WITH FILTER:将图像过滤器(grayscale,自动图像增强等)添加到BASE模式. FulL:在BASE WITH FILTER模式中加入由ML驱动的图像清理能力(erase 污点,指等). 该模式还将允许未来的主要功能与Google Play服务更新一起被自动添加,而其他两种模式将保持其目前的功能集,仅接收到一些小的改进."FULL"7.3.0 (单位:千美元)

Is Google DocumentScannerMudule Avail可生效文件

Prop类型说明自兹
availableboolean是否提供谷歌文档扫描器模块 .7.3.0 (单位:千美元)

插件听筒

Prop类型
remove() => Promise<void>

谷歌文档扫描器模块Install ProgressEvent

Prop类型说明自兹
stateGoogleDocumentScannerModuleInstallState安装现状.7.3.0 (单位:千美元)
progressnumber安装进度在0-100之间.7.3.0 (单位:千美元)

数字

谷歌文件扫描器模块Install State

成员数值自兹
UNKNOWN07.3.0 (单位:千美元)
PENDING17.3.0 (单位:千美元)
DOWNLOADING27.3.0 (单位:千美元)
CANCELED37.3.0 (单位:千美元)
COMPLETED47.3.0 (单位:千美元)
FAILED57.3.0 (单位:千美元)
INSTALLING67.3.0 (单位:千美元)
DOWNLOAD_PAUSED77.3.0 (单位:千美元)

页:1

  • ML Kit文档扫描器模型,扫描逻辑,和UI流被Google Play服务动态下载. 用户可能必须在第一次使用前等待这些下载. 您可以使用 GoogleDocumentScannerModuleAvailable 并安装 GoogleDocumentScannerModule 方法来检查和安装模块,并听取 GoogleDocumentScannerModule Install 进度更新事件 .
  • 这个API需要Android API级别21或以上.
  • 它还需要最小的设备总RAM为1.7GB. 如果更低,则返回一个 MlKitException 有错误代码 UNSUPPORTED 当调用 API( 此插件将拒绝承诺) 时 .
  • 认为生成文档文件需要时间并需要处理功率,所以仅请求输出格式(JPEG,或PDF,或两者兼用) resultFormats 选项.

财务问题

此插件支持哪些平台 ?

该插件只在Android上可用,因为基础的ML Kit文档扫描器由Google Play服务提供. 所有的方法都拒绝在其他平台上.

为什么文档扫描仪不能首先使用?

ML Kit文档扫描器模型,扫描逻辑,和UI流被Google Play服务动态下载,因此用户可能不得不等下载后再首次使用. 使用 isGoogleDocumentScannerModuleAvailable() 检查模块, installGoogleDocumentScannerModule() 安装,并听 googleDocumentScannerModuleInstallProgress 用于更新进度的事件.

从扫描中可以得到哪些结果格式?

您可以通过 : resultFormats 选项。 然后,结果包含一系列URI,用于扫描图像页面和/或关于生成的PDF的信息,包括其URI和页数. 由于生成文档文件需要时间和处理能力,所以只请求您实际需要的格式.

扫描器模式有什么区别?

这个 BASE 模式提供了基本的编辑能力,如裁剪、旋转和重排页面。 这个 BASE_WITH_FILTER 模式增加了图像过滤器,如灰度和自动图像增强等. 这个 FULL 模式(默认)额外提供由ML驱动的图像清理能力,例如去除污点和指头,并将随着Google Play服务的更新而自动接收未来的主要功能.

是否有设备要求?

是,API要求Android API级别为21级或以上,最低设备总RAM为1.7 GB. API 返回一个 MlKitException 有错误代码 UNSUPPORTED 而插件拒绝承诺.

用户可以导入已有相片而不是扫描吗 ?

是的,设置 galleryImportAllowed 选项到 true 允许从相册中导入页面。 默认已禁用 .

术语和隐私

此插件使用 Google ML Kit (Google ML Kit) 页面存档备份,存于互联网档案馆数字 :

通讯

随时更新有关卡帕韦索姆、Capacitor和Ionic生态系统的最新消息和最新消息,因为我们 《卡帕索克通讯》。 。 。 .

更改日志

见 变动情况。 。 。 .

许可证

见 自由主义。 。 。 .

[^1]: 这个项目不附属于Google LLC或其任何子公司或子公司,也不获得其认可、支持、赞助或批准.

版本与发布记录

@capacitor-mlkit/document-scanner 共发布 10 个版本,首次发布于 2025/07/31;过去 12 个月发布了 9 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
8.2.2latest2026/10/319 天前
8.2.12026/09/101 个月前
8.1.12026/08/312 个月前
8.1.02026/05/076 个月前
8.0.12026/02/198 个月前
8.0.02025/12/1710 个月前

其他发布通道:dev → 7.0.0-dev.1765885034。

关于 @capacitor-mlkit/document-scanner 的常见问题

@capacitor-mlkit/document-scanner 是什么?

Android上的ML Kit文档扫描器的电容插件.

如何安装 @capacitor-mlkit/document-scanner?

在项目目录运行 npm install @capacitor-mlkit/document-scanner,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@capacitor/core(>=8.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@capacitor-mlkit/document-scanner 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@capacitor-mlkit/document-scanner 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@capacitor-mlkit/document-scanner 可以商用吗?

@capacitor-mlkit/document-scanner 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@capacitor-mlkit/document-scanner 的下载量有多少?

根据 npm 官方下载统计,@capacitor-mlkit/document-scanner 在 2026/10/09 – 2026/11/07 这 30 天内被下载 76,757 次,日均约 2,559 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@capacitor-mlkit/document-scanner 还在维护吗?

最新版本 8.2.2 发布于 2026/10/31(9 天前);至今共发布 10 个版本,其中过去 12 个月发布了 9 个;首次发布于 2025/07/31。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。