主题
LimeOcr
提供文字识别功能,支持多语言识别和结构化结果返回
文档
安装
使用
js
// 非 uniappx 不需要导入类型 OCRRecognizeOptions
import { recognizeImage, type OCRRecognizeOptions } from '@/uni_modules/lime-ocr'
const onClick = () => {
recognizeImage({
// language: 'zh',
url: '/static/ocr/QQ20250312-231846.png', // 支持临时地址,缓存地址。如果是网络地址请先下载。
success(res) {
console.log('res', res.text)
// console.log('textBlocks', res.textBlocks)
},
fail(err) {
console.log('err', err)
}
} as OCRRecognizeOptions) // 非uniappx 不需要强制类型 OCRRecognizeOptions
}
方法列表
异步识别方法
recognizeImage(options: OCRRecognizeOptions): void
参数说明
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
url | string | 是 | 要识别的图片URL |
language | 'zh'|'ja'|'ko'|'devanagari' | 否 | 识别语言类型,默认中文 |
success | function | 否 | 识别成功的回调函数 |
fail | function | 否 | 识别失败的回调函数 |
数据结构
OCRResult
字段 | 类型 | 说明 |
---|---|---|
text | string | 合并后的完整识别文本 |
textBlocks | TextBlock[] | 结构化识别结果(多级结构) |
errMsg | string | 结果状态信息 |
错误码说明
错误码 | 说明 |
---|---|
9010001 | 图片加载失败 |
9010002 | 文字识别失败 |
9010003 | 不支持的语言类型 |
9010004 | 路径不存在 |
回调函数示例
成功回调结构:
typescript
(res: OCRResult) => {
text: "完整识别文本",
textBlocks: [{
text: "段落文本",
lines: [{
text: "行文本",
elements: [{
text: "单词",
bounds: { left: 0, top: 0, right: 100, bottom: 50 }
}]
}]
}]
}
## 源代码
[组件源码](https://ext.dcloud.net.cn/plugin?name=lime-ocr)