外观
光学字符识别 (OCR)
ocr 从图片或当前屏幕中识别文本。MonkeyKing 提供三种工作模式:mlkit(默认)、paddle 和 rapid。主模块根据 ocr.mode 或单次调用的 options.mode 分派;ocr.mlkit、ocr.paddle、ocr.rapid 则固定使用对应引擎。
所有公开识别入口对 Rhino 脚本表现为同步调用:它们在返回前完成推理。省略图片时会捕获当前屏幕,必须先取得屏幕捕获权限;在 UI 线程或紧密循环中反复调用可能造成明显阻塞。
通用输入与结果
ts
type OcrMode = 'mlkit' | 'paddle' | 'rapid'
type OcrImage = ImageWrapper | string
type OcrRegion = number[] | android.graphics.Rect | org.opencv.core.Rect
interface OcrResult {
text: string
label: string // text 的同义字段
confidence: number // 0..1
bounds: android.graphics.Rect
}每个 recognizeText、detect 和可调用模块都接受同一组输入形式:
ts
fn(options?: OcrOptions): Result[]
fn(region: OcrRegion): Result[]
fn(image: OcrImage, options?: OcrOptions): Result[]
fn(image: OcrImage, region: OcrRegion): Result[]image是路径时,运行时先用images.read解码;路径无效会抛出WrappedIllegalArgumentException。- 省略
image时同步捕获屏幕;调用前应先执行images.requestScreenCapture(...)。 options.region缺省或为undefined时识别整张图;显式为null时返回空数组。区域也可作为第二个位置参数直接传入。- 区域识别先裁剪图像;
detect返回的bounds会重新偏移到原图坐标系。 - 每个公开入口最多接受三个实参。未知模式、错误参数数量或无法解析的图片会抛出参数异常。
- 识别所需的引擎库会在首次调用对应引擎时检查并准备。
完整选项见 OcrOptions。
客户端能力与最低版本
Market 默认模型最低支持版本为 1.0.48。 该版本随应用交付 Paddle PP-OCRv5 Mobile 和 Rapid PP-OCRv6 ppocrv6-small,无需运行时下载。此约定适用于主模块以及两个固定引擎的可调用形式、detect 和 recognizeText。
| 客户端 | Paddle / Rapid 来源 | 未指定模型目录时 |
|---|---|---|
| Market 1.0.48 及以上 | 已启用的内置引擎 | Paddle 使用内置 Mobile,Rapid 使用内置 ppocrv6-small。首次调用由应用准备默认模型。 |
| 普通 App | 已安装、启用且授权的兼容外部插件 | 使用所选插件交付的模型与能力;缺少插件会报错,不享有 Market 默认模型保证。 |
| 打包应用 INRT | 打包时交付的引擎、模型与运行库 | 使用该 APK 交付的能力;未交付相应能力会报错。PP-OCRv5 ONNX 的 profile 必须匹配打包档位。 |
Market 的模型选择规则:
- 不传
modelDirectory:使用所选引擎的默认模型。ocr的默认模式仍是 ML Kit,需要 Paddle/Rapid 时使用mode或固定入口。 - 传入
modelDirectory:优先且仅使用该手机本地目录,支持绝对路径和相对脚本目录的路径,不作为网络下载入口。目录缺失、损坏或与 profile 不匹配时明确报错,不回退默认模型。 - 不传
modelProfile:Paddle 为Mobile,Rapid 为ppocrv6-small。Paddle 的其他有效档位必须同时提供对应目录;未知 profile 会被拒绝,Rapid 只支持ppocrv6-small。
完整档位、文件名和目录层级见 OcrOptions 的模型目录与 profile。App 外部插件是否支持文件模型由插件决定;INRT 的目录选项也不能补齐未交付的引擎或切换到未交付的 profile。
js
// Market 1.0.48 起,现有默认调用无需增加目录参数。
const paddle = ocr.detect('/sdcard/Download/page.png', { mode: 'paddle' })
const rapid = ocr.rapid('/sdcard/Download/page.png')
// 显式本地目录:Paddle 根目录下应有 models/ 子目录。
const custom = ocr.paddle.detect('/sdcard/Download/page.png', {
modelDirectory: './ocr-models/paddle',
modelProfile: 'Mobile',
})
// Rapid 根目录直接包含四个模型/字典文件。
const text = ocr.rapid.recognizeText('/sdcard/Download/page.png', {
modelDirectory: '/sdcard/ocr-models/rapid',
modelProfile: 'ppocrv6-small',
})ocr 模块
运行时自动提供,无需导入。主模块公开模式切换、文本识别、结构化检测和摘要方法,并可直接作为函数调用。
ocr.mode
ts
ocr.mode: 'mlkit' | 'paddle' | 'rapid'读写当前默认引擎,初始值为 mlkit。赋值会走与 ocr.tap 相同的模式解析;模式名不区分大小写。未知名称会抛出 WrappedIllegalArgumentException。
js
console.log(ocr.mode) // mlkit
ocr.mode = 'paddle'
const text = ocr('/sdcard/Download/page.png')ocr(input?, optionsOrRegion?)
ts
ocr(options?: OcrOptions): string[]
ocr(region: OcrRegion): string[]
ocr(image: OcrImage, options?: OcrOptions): string[]
ocr(image: OcrImage, region: OcrRegion): string[]ocr(...) 是 ocr.recognizeText(...) 的快捷形式,使用当前模式或 options.mode。成功时返回字符串数组,没有识别结果时为 []。Paddle/Rapid 的模型或引擎错误会抛出,不能把 [] 当作所有失败的统一返回值。
js
images.requestScreenCapture(false)
const labels = ocr({
mode: 'mlkit',
region: [0, 0, 0.75, 0.5],
})
console.log(labels.join('\n'))ocr.recognizeText(input?, optionsOrRegion?)
ts
ocr.recognizeText(options?: OcrOptions): string[]
ocr.recognizeText(region: OcrRegion): string[]
ocr.recognizeText(image: OcrImage, options?: OcrOptions): string[]
ocr.recognizeText(image: OcrImage, region: OcrRegion): string[]返回引擎输出的文本标签。需要置信度和位置时改用 ocr.detect。Market 1.0.48 起,Paddle/Rapid 省略 modelDirectory 使用各自内置默认模型;显式目录失败不会回退默认模型。
js
const labels = ocr.recognizeText('/sdcard/Download/receipt.jpg', {
mode: 'paddle',
scoreThreshold: 0.6,
})
labels.forEach(label => console.log(label))ocr.detect(input?, optionsOrRegion?)
ts
ocr.detect(options?: OcrOptions): OcrResult[]
ocr.detect(region: OcrRegion): OcrResult[]
ocr.detect(image: OcrImage, options?: OcrOptions): OcrResult[]
ocr.detect(image: OcrImage, region: OcrRegion): OcrResult[]返回包含文本、置信度和 Android 边界矩形的结果。使用区域时,边界仍以原始图片左上角为坐标原点。Market 1.0.48 起,Paddle/Rapid 省略 modelDirectory 使用各自内置默认模型;也可指定本地目录和 modelProfile,详见 OcrOptions。模型错误会抛出,显式目录失败不会回退默认模型。
js
const results = ocr.detect('/sdcard/Download/page.png', {
mode: 'rapid',
region: [40, 80, 800, 1200],
})
results
.filter(result => result.confidence >= 0.7)
.forEach(result => console.log(result.text, result.bounds))ocr.tap(mode)
ts
ocr.tap(mode: OcrMode | typeof ocr.mlkit | typeof ocr.paddle | typeof ocr.rapid): void切换默认引擎。除模式字符串外,也接受三个引擎模块对象。未知值抛出 WrappedIllegalArgumentException,不会产生识别结果。
js
ocr.tap('rapid')
console.log(ocr.mode) // rapid
ocr.tap(ocr.mlkit)ocr.summary()
ts
ocr.summary(): string不接受参数,返回包含当前模式和全部可用模式(mlkit、paddle、rapid)的多行摘要。传入参数会触发参数数量异常。
ocr.toString()
ts
ocr.toString(): string返回值与 ocr.summary() 相同。该函数由运行时作为字面量 toString 属性导出,不接受参数。
js
console.log(String(ocr))
console.log(ocr.toString())ocr.mlkit 模块
固定使用 ML Kit 中文文字识别器,不读取 options.mode。识别器按需创建,并在脚本运行时回收阶段关闭。当前实现会等待 ML Kit Task 完成;任务取消或失败时记录警告并返回空数组。
ocr.mlkit(input?, optionsOrRegion?)
ts
ocr.mlkit(options?: OcrOptions): string[]
ocr.mlkit(region: OcrRegion): string[]
ocr.mlkit(image: OcrImage, options?: OcrOptions): string[]
ocr.mlkit(image: OcrImage, region: OcrRegion): string[]ocr.mlkit.recognizeText 的快捷形式。除通用 region 外,当前版本的 ML Kit 实现不消费 Paddle 专用选项。
ocr.mlkit.recognizeText(input?, optionsOrRegion?)
ts
ocr.mlkit.recognizeText(input?: OcrImage | OcrRegion | OcrOptions, optionsOrRegion?: OcrOptions | OcrRegion): string[]返回按版面顺序整理的文本行。已回收图像、空结果或失败任务返回 []。
ocr.mlkit.detect(input?, optionsOrRegion?)
ts
ocr.mlkit.detect(input?: OcrImage | OcrRegion | OcrOptions, optionsOrRegion?: OcrOptions | OcrRegion): OcrResult[]把 ML Kit 的每一条文字行转换为 OcrResult。置信度和边界来自内置识别器;失败不会抛出 ML Kit Task 异常,而是返回空数组。
js
const results = ocr.mlkit.detect('/sdcard/Download/chinese.png')
results.forEach(result => console.log(result.text, result.confidence))ocr.paddle 模块
固定使用 Paddle OCR。Market 1.0.48 起,未指定 modelDirectory 时使用内置 PP-OCRv5 Mobile;显式目录优先且失败不回退,其他有效 profile 需要对应目录。普通 App 通过可用的 Paddle OCR 插件执行,没有可用插件时抛出 WrappedIllegalArgumentException;INRT 使用打包时交付的本地引擎与模型。调用同步等待结果。
ocr.paddle(input?, optionsOrRegion?)
ts
ocr.paddle(options?: OcrOptions): string[]
ocr.paddle(region: OcrRegion): string[]
ocr.paddle(image: OcrImage, options?: OcrOptions): string[]
ocr.paddle(image: OcrImage, region: OcrRegion): string[]ocr.paddle.recognizeText 的快捷形式。支持 modelDirectory、modelProfile 及引擎允许的推理选项;mergeLine 只影响结构化 detect 结果。Market 1.0.48 起省略目录使用 Mobile。当前 PP-OCRv5 ONNX 不支持显式 useSlim: true 或 useOpenCL: true,详见 OcrOptions。
ocr.paddle.recognizeText(input?, optionsOrRegion?)
ts
ocr.paddle.recognizeText(input?: OcrImage | OcrRegion | OcrOptions, optionsOrRegion?: OcrOptions | OcrRegion): string[]返回 Paddle 输出的文本数组。默认 cpuThreadNum = 4、useOpenCL = false;PP-OCRv5 ONNX 调用应省略 useSlim,不要显式设置为 true。Market 1.0.48 起省略 modelDirectory 使用 Mobile,指定目录时按 modelProfile 加载且不回退默认模型;模型准备或推理错误会抛出。
ocr.paddle.detect(input?, optionsOrRegion?)
ts
ocr.paddle.detect(input?: OcrImage | OcrRegion | OcrOptions, optionsOrRegion?: OcrOptions | OcrRegion): OcrResult[]返回 Paddle 文本、置信度和矩形。Market 1.0.48 起省略 modelDirectory 使用 Mobile,显式目录及 modelProfile 的规则见 OcrOptions。mergeLine: true 时,只有在 splitWords 与 useWordSegmentation 都为 false 的情况下才会合并同一行;合并后的置信度按各段文本长度加权,边界为各段并集。
js
const results = ocr.paddle.detect('/sdcard/Download/document.jpg', {
cpuThreadNum: 4,
scoreThreshold: 0.55,
mergeLine: true,
})ocr.rapid 模块
固定使用 Rapid OCR。Market 1.0.48 起,省略 modelDirectory 使用内置 PP-OCRv6 ppocrv6-small,显式目录优先且失败不回退。普通 App 需要可用的 Rapid OCR 插件;INRT 需要打包时交付 Rapid 能力。Rapid 接收 modelDirectory、modelProfile、cpuThreadNum、detLongSize、scoreThreshold 等选项;useOpenCL: true 会报错,不能视为忽略。modelProfile 只支持精确值 ppocrv6-small。
ocr.rapid(input?, optionsOrRegion?)
ts
ocr.rapid(options?: OcrOptions): string[]
ocr.rapid(region: OcrRegion): string[]
ocr.rapid(image: OcrImage, options?: OcrOptions): string[]
ocr.rapid(image: OcrImage, region: OcrRegion): string[]ocr.rapid.recognizeText 的快捷形式,同步等待推理完成。Market 1.0.48 起省略 modelDirectory 使用 ppocrv6-small,也可指定该档位的本地目录。选项与校验规则见 OcrOptions。
ocr.rapid.recognizeText(input?, optionsOrRegion?)
ts
ocr.rapid.recognizeText(input?: OcrImage | OcrRegion | OcrOptions, optionsOrRegion?: OcrOptions | OcrRegion): string[]返回 Rapid OCR 文本块的 text 字段数组,没有检测结果时为 []。Market 1.0.48 起省略 modelDirectory 使用 ppocrv6-small;显式目录校验失败、缺少插件/交付能力或推理失败会抛出,不能解释为空结果。图片路径解析失败也会报错。
ocr.rapid.detect(input?, optionsOrRegion?)
ts
ocr.rapid.detect(input?: OcrImage | OcrRegion | OcrOptions, optionsOrRegion?: OcrOptions | OcrRegion): OcrResult[]把每个 Rapid 文本块转换为 OcrResult:边界取文本块左上与右下点,置信度取 boxScore。Market 1.0.48 起省略 modelDirectory 使用 ppocrv6-small;显式目录失败不会回退默认模型。线程数、检测长边和检测框分数阈值可通过 OcrOptions 调整。
js
const results = ocr.rapid.detect('/sdcard/Download/sign.png')
results.forEach(result => console.log(result.text, result.bounds))生命周期、权限与错误恢复
- 省略图片时需要屏幕捕获授权;读取文件路径时需要相应存储访问能力。OCR 模块不会代替脚本请求这些权限。
- 引擎调用同步占用调用线程。批量识别时应在工作线程串行执行,并及时回收不再使用的图像。
- ML Kit 识别器由运行时缓存并在退出时关闭;Paddle/Rapid 的库文件由引擎准备流程管理。
- 使用区域时会产生临时裁剪图像;避免同时在其他线程回收原始图像。
- Paddle/Rapid 的缺模型、校验失败、未知 profile、不支持的选项以及加载/推理失败会抛出。
[]只表示空识别结果、region: null等明确情况;ML Kit Task 取消或失败返回空数组的行为不代表其他引擎。 - 显式目录报错时,核对本地访问权限、profile、文件名与层级,并使用完整且与引擎匹配的模型包;不会自动回退默认模型。
模型未准备通常指出缺少的文件,校验不符和加载失败则需要修复模型包。 - Market 1.0.48 的默认模型支持不要求修改或重新发布采用默认 Paddle/Rapid 调用的现有脚本。普通 App 与 INRT 仍遵循各自的能力交付规则。
逐符号版本与 Rhino 2.0 示例
下列每个条目都对应一个公开 API 符号;示例按 Rhino 2.0 语法书写。需要文件、网络或 UI 资源的示例应在具备相应运行条件时执行。
call:ocr · Rhino 2.0 示例:
js
console.log(typeof ocr);call:ocr.mlkit · Rhino 2.0 示例:
js
console.log(typeof ocr.mlkit);call:ocr.paddle · Rhino 2.0 示例:
js
console.log(typeof ocr.paddle);call:ocr.rapid · Rhino 2.0 示例:
js
console.log(typeof ocr.rapid);module:ocr · Rhino 2.0 示例:
js
console.log(typeof ocr);module:ocr.mlkit · Rhino 2.0 示例:
js
console.log(typeof ocr.mlkit);module:ocr.paddle · Rhino 2.0 示例:
js
console.log(typeof ocr.paddle);module:ocr.rapid · Rhino 2.0 示例:
js
console.log(typeof ocr.rapid);ocr.detect · Rhino 2.0 示例:
js
console.log(typeof ocr.detect);ocr.mlkit.detect · Rhino 2.0 示例:
js
console.log(typeof ocr.mlkit.detect);ocr.mlkit.recognizeText · Rhino 2.0 示例:
js
console.log(typeof ocr.mlkit.recognizeText);ocr.mode · Rhino 2.0 示例:
js
console.log(ocr.mode);ocr.paddle.detect · Rhino 2.0 示例:
js
console.log(typeof ocr.paddle.detect);ocr.paddle.recognizeText · Rhino 2.0 示例:
js
console.log(typeof ocr.paddle.recognizeText);ocr.rapid.detect · Rhino 2.0 示例:
js
console.log(typeof ocr.rapid.detect);ocr.rapid.recognizeText · Rhino 2.0 示例:
js
console.log(typeof ocr.rapid.recognizeText);ocr.recognizeText · Rhino 2.0 示例:
js
console.log(typeof ocr.recognizeText);ocr.summary · Rhino 2.0 示例:
js
console.log(typeof ocr.summary);ocr.tap · Rhino 2.0 示例:
js
console.log(typeof ocr.tap);ocr.toString · Rhino 2.0 示例:
js
console.log(typeof ocr.toString);