前言
uni app实现OCR功能,目前只能依靠插件的方式实现。这里在android平台上,采用这个UTS插件来实现。
插件功能
OCR功能,能够识别中文文本和英文文本。可以从相机拍照或者从相册选取,文本识别后可以直接返回或者手动从图片中选择。
-
OCR识别文本功能(主要是中文,英文也可以)
-
支持相册和相机选择图片
-
文本返回内容可配置
使用方式
识别样例
原图:
识别结果(从照片选取,也可以不从照片选取直接返回识别文本)
Block
Line
Symbol
插件使用及相关API说明
在线使用插件通用流程
-
购买此插件,选择该插件绑定的项目(使用者项目)。
-
购买页面导入到相应项目。
-
根据插件作者的提供的文档开发代码,在代码中引用插件,调用插件功能。
-
打包自定义基座,得到自定义基座,然后运行时选择自定义基座,进行log输出测试。
-
开发完毕后正式云打包。
此插件的使用
引入插件
javascript
import {OCROptions,uTSOCRRecognize} from "@/uni_modules/xt-ocr-recognize"
使用插件
csharp
let options = {
params: `{
'imgLocation': 1,
'pickTextFromImg': false,
'pickTextAreaType': 2,
'textAreaType':1
}`,
complete: (res? : string) => {
console.log(res)
}
} as OCROptions;
uTSOCRRecognize(options);
参数说明
参数名 | 参数类型 | 参数默认值 | 参数说明 |
---|---|---|---|
imgLocation | Integer | 0 | 识别图片选取方式 0 - camera 相机获取 1 - album 相册获取 |
pickTextFromImg | Boolean | False | 识别文本是否手动从图片中选择,这个取值受textAreaType和pickTextAreaType影响 True - 返回值手动从已识别的图片上获取,返回值类型为文本,选择区域受pickTextAreaType影响 False - 直接返回结果,返回值是JSON对象,返回值内容受textAreaType影响 |
textAreaType | Integer | 0 | 当pickTextFromImg 为False时,设置该值,文本返回区域类型 0 - block 1 - line 2 - element 3 - symbol 4 - all 数据比较大 具体数据类型会有说明 |
pickTextAreaType | Integer | 0 | 当pickTextFromImg 为True时,设置该值,文本选取区域类型 0 - block 1 - line 2 - element 3 - symbol |
回调结果说明
回调结果是一个JSON格式数据,包含两个属性
返回值名称 | 返回值类型 | 返回值说明 |
---|---|---|
status | String | 成功为"success",其他为失败 |
result | String | 只有成功时才有,内容为识别结果,结果格式根据pickTextFromImg/textAreaType/pickTextAreaType不同而不同,后面会有说明 |
result值说明
识别图片样例:
pickTextFromImg | textAreaType | pickTextAreaType | JSON数据格式样例 |
---|---|---|---|
False | 0 | NA(不生效) | 下方0.0 |
False | 1 | NA(不生效) | 下方0.1 |
False | 2 | NA(不生效) | 下方0.2 |
False | 3 | NA(不生效) | 下方0.3 |
False | 4 | NA(不生效) | 下方0.4 |
True | NA(不生效) | 0 | 下方1.0 |
True | NA(不生效) | 1 | 下方1.1 |
True | NA(不生效) | 2 | 下方1.2 |
True | NA(不生效) | 3 | 下方1.3 |
css
0.0
{
"status": "success",
"result": {
"text": "从帐户添加联系人\n从文件导入联系人",
"blocks": [
{
"blockText": "从帐户添加联系人",
"blockCornerPoints": [
{
"x": 358,
"y": 160
},
{
"x": 724,
"y": 160
},
{
"x": 724,
"y": 204
},
{
"x": 358,
"y": 204
}
],
"blockBoundingBox": {
"left": 358,
"top": 160,
"right": 724,
"bottom": 204
}
},
{
"blockText": "从文件导入联系人",
"blockCornerPoints": [
{
"x": 358,
"y": 330
},
{
"x": 723,
"y": 330
},
{
"x": 723,
"y": 374
},
{
"x": 358,
"y": 374
}
],
"blockBoundingBox": {
"left": 358,
"top": 330,
"right": 723,
"bottom": 374
}
}
]
}
}
css
0.1
{
"status": "success",
"result": {
"text": "从帐户添加联系人\n从文件导入联系人",
"lines": [
{
"lineText": "从帐户添加联系人",
"lineCornerPoints": [
{
"x": 358,
"y": 160
},
{
"x": 724,
"y": 160
},
{
"x": 724,
"y": 204
},
{
"x": 358,
"y": 204
}
],
"lineBoundingBox": {
"left": 358,
"top": 160,
"right": 724,
"bottom": 204
}
},
{
"lineText": "从文件导入联系人",
"lineCornerPoints": [
{
"x": 358,
"y": 330
},
{
"x": 723,
"y": 330
},
{
"x": 723,
"y": 374
},
{
"x": 358,
"y": 374
}
],
"lineBoundingBox": {
"left": 358,
"top": 330,
"right": 723,
"bottom": 374
}
}
]
}
}
css
0.2
{
"status": "success",
"result": {
"text": "从帐户添加联系人\n从文件导入联系人",
"elements": [
{
"elementText": "从帐户添加联系人",
"elementCornerPoints": [
{
"x": 358,
"y": 160
},
{
"x": 724,
"y": 160
},
{
"x": 724,
"y": 204
},
{
"x": 358,
"y": 204
}
],
"elementBoundingBox": {
"left": 358,
"top": 160,
"right": 724,
"bottom": 204
}
},
{
"elementText": "从文件导入联系人",
"elementCornerPoints": [
{
"x": 358,
"y": 330
},
{
"x": 723,
"y": 330
},
{
"x": 723,
"y": 374
},
{
"x": 358,
"y": 374
}
],
"elementBoundingBox": {
"left": 358,
"top": 330,
"right": 723,
"bottom": 374
}
}
]
}
}
css
0.3
{
"status": "success",
"result": {
"text": "从帐户添加联系人\n从文件导入联系人",
"symbols": [
{
"symbolText": "从",
"symbolCornerPoints": [
{
"x": 358,
"y": 160
},
{
"x": 418,
"y": 160
},
{
"x": 418,
"y": 204
},
{
"x": 358,
"y": 204
}
],
"symbolBoundingBox": {
"left": 358,
"top": 160,
"right": 418,
"bottom": 204
}
},
{
"symbolText": "帐",
"symbolCornerPoints": [
{
"x": 418,
"y": 160
},
{
"x": 464,
"y": 160
},
{
"x": 464,
"y": 204
},
{
"x": 418,
"y": 204
}
],
"symbolBoundingBox": {
"left": 418,
"top": 160,
"right": 464,
"bottom": 204
}
},
{
"symbolText": "户",
"symbolCornerPoints": [
{
"x": 464,
"y": 160
},
{
"x": 509,
"y": 160
},
{
"x": 509,
"y": 204
},
{
"x": 464,
"y": 204
}
],
"symbolBoundingBox": {
"left": 464,
"top": 160,
"right": 509,
"bottom": 204
}
},
{
"symbolText": "添",
"symbolCornerPoints": [
{
"x": 509,
"y": 160
},
{
"x": 555,
"y": 160
},
{
"x": 555,
"y": 204
},
{
"x": 509,
"y": 204
}
],
"symbolBoundingBox": {
"left": 509,
"top": 160,
"right": 555,
"bottom": 204
}
},
{
"symbolText": "加",
"symbolCornerPoints": [
{
"x": 555,
"y": 160
},
{
"x": 601,
"y": 160
},
{
"x": 601,
"y": 204
},
{
"x": 555,
"y": 204
}
],
"symbolBoundingBox": {
"left": 555,
"top": 160,
"right": 601,
"bottom": 204
}
},
{
"symbolText": "联",
"symbolCornerPoints": [
{
"x": 601,
"y": 160
},
{
"x": 650,
"y": 160
},
{
"x": 650,
"y": 204
},
{
"x": 601,
"y": 204
}
],
"symbolBoundingBox": {
"left": 601,
"top": 160,
"right": 650,
"bottom": 204
}
},
{
"symbolText": "系",
"symbolCornerPoints": [
{
"x": 650,
"y": 160
},
{
"x": 696,
"y": 160
},
{
"x": 696,
"y": 204
},
{
"x": 650,
"y": 204
}
],
"symbolBoundingBox": {
"left": 650,
"top": 160,
"right": 696,
"bottom": 204
}
},
{
"symbolText": "人",
"symbolCornerPoints": [
{
"x": 696,
"y": 160
},
{
"x": 724,
"y": 160
},
{
"x": 724,
"y": 204
},
{
"x": 696,
"y": 204
}
],
"symbolBoundingBox": {
"left": 696,
"top": 160,
"right": 724,
"bottom": 204
}
},
{
"symbolText": "从",
"symbolCornerPoints": [
{
"x": 358,
"y": 330
},
{
"x": 401,
"y": 330
},
{
"x": 401,
"y": 374
},
{
"x": 358,
"y": 374
}
],
"symbolBoundingBox": {
"left": 358,
"top": 330,
"right": 401,
"bottom": 374
}
},
{
"symbolText": "文",
"symbolCornerPoints": [
{
"x": 404,
"y": 330
},
{
"x": 448,
"y": 330
},
{
"x": 448,
"y": 374
},
{
"x": 404,
"y": 374
}
],
"symbolBoundingBox": {
"left": 404,
"top": 330,
"right": 448,
"bottom": 374
}
},
{
"symbolText": "件",
"symbolCornerPoints": [
{
"x": 450,
"y": 330
},
{
"x": 493,
"y": 330
},
{
"x": 493,
"y": 374
},
{
"x": 450,
"y": 374
}
],
"symbolBoundingBox": {
"left": 450,
"top": 330,
"right": 493,
"bottom": 374
}
},
{
"symbolText": "导",
"symbolCornerPoints": [
{
"x": 497,
"y": 330
},
{
"x": 538,
"y": 330
},
{
"x": 538,
"y": 374
},
{
"x": 497,
"y": 374
}
],
"symbolBoundingBox": {
"left": 497,
"top": 330,
"right": 538,
"bottom": 374
}
},
{
"symbolText": "入",
"symbolCornerPoints": [
{
"x": 542,
"y": 330
},
{
"x": 585,
"y": 330
},
{
"x": 585,
"y": 374
},
{
"x": 542,
"y": 374
}
],
"symbolBoundingBox": {
"left": 542,
"top": 330,
"right": 585,
"bottom": 374
}
},
{
"symbolText": "联",
"symbolCornerPoints": [
{
"x": 588,
"y": 330
},
{
"x": 631,
"y": 330
},
{
"x": 631,
"y": 374
},
{
"x": 588,
"y": 374
}
],
"symbolBoundingBox": {
"left": 588,
"top": 330,
"right": 631,
"bottom": 374
}
},
{
"symbolText": "系",
"symbolCornerPoints": [
{
"x": 635,
"y": 330
},
{
"x": 676,
"y": 330
},
{
"x": 676,
"y": 374
},
{
"x": 635,
"y": 374
}
],
"symbolBoundingBox": {
"left": 635,
"top": 330,
"right": 676,
"bottom": 374
}
},
{
"symbolText": "人",
"symbolCornerPoints": [
{
"x": 680,
"y": 330
},
{
"x": 723,
"y": 330
},
{
"x": 723,
"y": 374
},
{
"x": 680,
"y": 374
}
],
"symbolBoundingBox": {
"left": 680,
"top": 330,
"right": 723,
"bottom": 374
}
}
]
}
}
css
0.4
JSON结构,可以用嵌套4个For循环读取
blocks [ block { blockText: block文本 blockCornerPoints:识别矩形框四个顶点 blockBoundingBox:识别区域矩形 lineJSONArray [ line { lineText: block文本 lineCornerPoints:识别矩形框四个顶点 lineBoundingBox:识别区域矩形 elementJSONArray [ element { elementText: block文本 elementCornerPoints:识别矩形框四个顶点 elementBoundingBox:识别区域矩形 symbolJSONArray [ symbol { symbolText: block文本 symbolCornerPoints:识别矩形框四个顶点 symbolBoundingBox:识别区域矩形 } ]
}
]
}
]
}
]
{
"status": "success",
"result": {
"text": "从帐户添加联系人\n从文件导入联系人",
"blocks": [
{
"blockText": "从帐户添加联系人",
"blockCornerPoints": [
{
"x": 358,
"y": 160
},
{
"x": 724,
"y": 160
},
{
"x": 724,
"y": 204
},
{
"x": 358,
"y": 204
}
],
"blockBoundingBox": {
"left": 358,
"top": 160,
"right": 724,
"bottom": 204
},
"lineJSONArray": [
{
"lineText": "从帐户添加联系人",
"lineCornerPoints": [
{
"x": 358,
"y": 160
},
{
"x": 724,
"y": 160
},
{
"x": 724,
"y": 204
},
{
"x": 358,
"y": 204
}
],
"lineBoundingBox": {
"left": 358,
"top": 160,
"right": 724,
"bottom": 204
},
"elementJSONArray": [
{
"elementText": "从帐户添加联系人",
"elementCornerPoints": [
{
"x": 358,
"y": 160
},
{
"x": 724,
"y": 160
},
{
"x": 724,
"y": 204
},
{
"x": 358,
"y": 204
}
],
"elementBoundingBox": {
"left": 358,
"top": 160,
"right": 724,
"bottom": 204
},
"symbolJSONArray": [
{
"symbolText": "从",
"symbolCornerPoints": [
{
"x": 358,
"y": 160
},
{
"x": 418,
"y": 160
},
{
"x": 418,
"y": 204
},
{
"x": 358,
"y": 204
}
],
"symbolBoundingBox": {
"left": 358,
"top": 160,
"right": 418,
"bottom": 204
}
},
{
"symbolText": "帐",
"symbolCornerPoints": [
{
"x": 418,
"y": 160
},
{
"x": 464,
"y": 160
},
{
"x": 464,
"y": 204
},
{
"x": 418,
"y": 204
}
],
"symbolBoundingBox": {
"left": 418,
"top": 160,
"right": 464,
"bottom": 204
}
},
{
"symbolText": "户",
"symbolCornerPoints": [
{
"x": 464,
"y": 160
},
{
"x": 509,
"y": 160
},
{
"x": 509,
"y": 204
},
{
"x": 464,
"y": 204
}
],
"symbolBoundingBox": {
"left": 464,
"top": 160,
"right": 509,
"bottom": 204
}
},
{
"symbolText": "添",
"symbolCornerPoints": [
{
"x": 509,
"y": 160
},
{
"x": 555,
"y": 160
},
{
"x": 555,
"y": 204
},
{
"x": 509,
"y": 204
}
],
"symbolBoundingBox": {
"left": 509,
"top": 160,
"right": 555,
"bottom": 204
}
},
{
"symbolText": "加",
"symbolCornerPoints": [
{
"x": 555,
"y": 160
},
{
"x": 601,
"y": 160
},
{
"x": 601,
"y": 204
},
{
"x": 555,
"y": 204
}
],
"symbolBoundingBox": {
"left": 555,
"top": 160,
"right": 601,
"bottom": 204
}
},
{
"symbolText": "联",
"symbolCornerPoints": [
{
"x": 601,
"y": 160
},
{
"x": 650,
"y": 160
},
{
"x": 650,
"y": 204
},
{
"x": 601,
"y": 204
}
],
"symbolBoundingBox": {
"left": 601,
"top": 160,
"right": 650,
"bottom": 204
}
},
{
"symbolText": "系",
"symbolCornerPoints": [
{
"x": 650,
"y": 160
},
{
"x": 696,
"y": 160
},
{
"x": 696,
"y": 204
},
{
"x": 650,
"y": 204
}
],
"symbolBoundingBox": {
"left": 650,
"top": 160,
"right": 696,
"bottom": 204
}
},
{
"symbolText": "人",
"symbolCornerPoints": [
{
"x": 696,
"y": 160
},
{
"x": 724,
"y": 160
},
{
"x": 724,
"y": 204
},
{
"x": 696,
"y": 204
}
],
"symbolBoundingBox": {
"left": 696,
"top": 160,
"right": 724,
"bottom": 204
}
}
]
}
]
}
]
},
{
"blockText": "从文件导入联系人",
"blockCornerPoints": [
{
"x": 358,
"y": 330
},
{
"x": 723,
"y": 330
},
{
"x": 723,
"y": 374
},
{
"x": 358,
"y": 374
}
],
"blockBoundingBox": {
"left": 358,
"top": 330,
"right": 723,
"bottom": 374
},
"lineJSONArray": [
{
"lineText": "从文件导入联系人",
"lineCornerPoints": [
{
"x": 358,
"y": 330
},
{
"x": 723,
"y": 330
},
{
"x": 723,
"y": 374
},
{
"x": 358,
"y": 374
}
],
"lineBoundingBox": {
"left": 358,
"top": 330,
"right": 723,
"bottom": 374
},
"elementJSONArray": [
{
"elementText": "从文件导入联系人",
"elementCornerPoints": [
{
"x": 358,
"y": 330
},
{
"x": 723,
"y": 330
},
{
"x": 723,
"y": 374
},
{
"x": 358,
"y": 374
}
],
"elementBoundingBox": {
"left": 358,
"top": 330,
"right": 723,
"bottom": 374
},
"symbolJSONArray": [
{
"symbolText": "从",
"symbolCornerPoints": [
{
"x": 358,
"y": 330
},
{
"x": 401,
"y": 330
},
{
"x": 401,
"y": 374
},
{
"x": 358,
"y": 374
}
],
"symbolBoundingBox": {
"left": 358,
"top": 330,
"right": 401,
"bottom": 374
}
},
{
"symbolText": "文",
"symbolCornerPoints": [
{
"x": 404,
"y": 330
},
{
"x": 448,
"y": 330
},
{
"x": 448,
"y": 374
},
{
"x": 404,
"y": 374
}
],
"symbolBoundingBox": {
"left": 404,
"top": 330,
"right": 448,
"bottom": 374
}
},
{
"symbolText": "件",
"symbolCornerPoints": [
{
"x": 450,
"y": 330
},
{
"x": 493,
"y": 330
},
{
"x": 493,
"y": 374
},
{
"x": 450,
"y": 374
}
],
"symbolBoundingBox": {
"left": 450,
"top": 330,
"right": 493,
"bottom": 374
}
},
{
"symbolText": "导",
"symbolCornerPoints": [
{
"x": 497,
"y": 330
},
{
"x": 538,
"y": 330
},
{
"x": 538,
"y": 374
},
{
"x": 497,
"y": 374
}
],
"symbolBoundingBox": {
"left": 497,
"top": 330,
"right": 538,
"bottom": 374
}
},
{
"symbolText": "入",
"symbolCornerPoints": [
{
"x": 542,
"y": 330
},
{
"x": 585,
"y": 330
},
{
"x": 585,
"y": 374
},
{
"x": 542,
"y": 374
}
],
"symbolBoundingBox": {
"left": 542,
"top": 330,
"right": 585,
"bottom": 374
}
},
{
"symbolText": "联",
"symbolCornerPoints": [
{
"x": 588,
"y": 330
},
{
"x": 631,
"y": 330
},
{
"x": 631,
"y": 374
},
{
"x": 588,
"y": 374
}
],
"symbolBoundingBox": {
"left": 588,
"top": 330,
"right": 631,
"bottom": 374
}
},
{
"symbolText": "系",
"symbolCornerPoints": [
{
"x": 635,
"y": 330
},
{
"x": 676,
"y": 330
},
{
"x": 676,
"y": 374
},
{
"x": 635,
"y": 374
}
],
"symbolBoundingBox": {
"left": 635,
"top": 330,
"right": 676,
"bottom": 374
}
},
{
"symbolText": "人",
"symbolCornerPoints": [
{
"x": 680,
"y": 330
},
{
"x": 723,
"y": 330
},
{
"x": 723,
"y": 374
},
{
"x": 680,
"y": 374
}
],
"symbolBoundingBox": {
"left": 680,
"top": 330,
"right": 723,
"bottom": 374
}
}
]
}
]
}
]
}
]
}
}
erlang
1.0 按照选取次序为 1,2,3...
{
"status": "success",
"result": {
"1": "从帐户添加联系人",
"2": "从文件导入联系人"
}
}
erlang
1.1按照选取次序为 1,2,3...
{
"status": "success",
"result": {
"1": "从帐户添加联系人",
"2": "从文件导入联系人"
}
}
erlang
1.2按照选取次序为 1,2,3...
{
"status": "success",
"result": {
"1": "从帐户添加联系人",
"2": "从文件导入联系人"
}
}
erlang
1.3按照选取次序为 1,2,3...
{
"status": "success",
"result": {
"1": "从",
"2": "户",
"3": "联",
"4": "从",
"5": "导"
}
}