更新记录
1.0.20250929(2025-09-29)
- 初次发版
平台兼容性
uni-app(4.45)
Vue2 |
Vue3 |
Chrome |
Safari |
app-vue |
app-nvue |
Android |
Android插件版本 |
iOS |
鸿蒙 |
- |
- |
- |
- |
- |
- |
5.0 |
1.0.20250929 |
- |
- |
微信小程序 |
支付宝小程序 |
抖音小程序 |
百度小程序 |
快手小程序 |
京东小程序 |
鸿蒙元服务 |
QQ小程序 |
飞书小程序 |
快应用-华为 |
快应用-联盟 |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
uni-app x(4.45)
Chrome |
Safari |
Android |
Android插件版本 |
iOS |
鸿蒙 |
微信小程序 |
- |
- |
5.0 |
1.0.20250929 |
- |
- |
- |
引入插件
- 点击插件试用引入到项目
- 打包自定义基座包
- 按照下方示例测试
- 先插件试用,完全符合需求后购买
插件引入
import * as mlkitorc from '@/uni_modules/cms-mlkitorc';
插件方法
方法 |
描述 |
参数 |
ocrText(OcrTextOption):void |
图片解析 |
无 |
OcrTextOption
参数 |
类型 |
描述 |
必填 |
url |
string |
图片路径支持本地和网络连接 |
是 |
language |
string |
zh:中文 deva:梵文 ja:日文 ko:韩文 en:英文(默认zh) |
否 |
success(OcrTextSuccessResult) |
function |
成功回调 |
是 |
fail(errCode : number, errMsg : string) |
function |
失败回调 |
否 |
OcrTextSuccessResult
参数 |
类型 |
描述 |
text |
string |
解析后文本内容 |
blocks |
BlockTextItem[] |
解析后详细数据 |
BlockTextItem
参数 |
类型 |
描述 |
text |
string |
解析后的文本 |
rect |
TextRect |
用于定位的矩形数据 |
points |
TextPointPos[] |
用于定位的四个坐标点 |
language |
string |
语言(不准确) |
TextRect
参数 |
类型 |
left |
number |
top |
number |
width |
number |
height |
number |
TextPointPos
错误码
错误码 |
描述 |
9000001 |
图片转换InputImage失败 |
9000002 |
Ocr解析失败 |
uniapp示例
<template>
<view>
<view class="raido_ul">
<view v-for="item of languages " :key="item.Code" :class="{ active: item.Code == language }"
@click="language = item.Code">
{{item.Name}}
</view>
</view>
<button type="primary" @click="onLocalImageBtnClick">解析本地图片</button>
网络图片:<input class="url_input" v-model="netImageUrl" />
<button type="primary" @click="onNetImageBtnClick">解析网络图片</button>
<block v-if="imageUrl">
<view>图片:</view>
<!-- 图片标注 -->
<view style="position: relative;">
<image :src="imageUrl" mode="widthFix" style="width:100%" />
<view v-for="(item,i) of showBlocks" :key="i" class="text_block" :style="{
top: item.rect.top * scaleRatio + 'px',
left: item.rect.left * scaleRatio + 'px',
width: item.rect.width * scaleRatio + 'px',
height: item.rect.height * scaleRatio + 'px',
lineHeight: item.rect.height * scaleRatio + 'px',
}" />
</view>
</block>
<block v-if="showText">
<view>文字:</view>
<view v-html="showText"></view>
</block>
</view>
</template>
<script>
import * as mlkitorc from '@/uni_modules/cms-mlkitorc';
export default {
data() {
return {
netImageUrl: "https://env-00jxt1cobn8u-static.normal.cloudstatic.cn/kuaiqucan/img/imageTextOcr.png",
imageUrl: "",
showText: "",
scaleRatio: 0,
showBlocks: [],
language: "zh",
languages: [
{ Code: "zh", Name: "中文" },
{ Code: "deva", Name: "梵文" },
{ Code: "ko", Name: "韩文" },
{ Code: "ja", Name: "日文" },
{ Code: "en", Name: "英文"},
]
}
},
methods: {
onLocalImageBtnClick() {
// 选择图片
uni.chooseImage({
count: 1, // 仅选择1张图片
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: async (res) => {
let absPath = plus.io.convertLocalFileSystemURL(res.tempFilePaths[0]);
this.ocrText(absPath) // 调用UTS识别方法
},
fail: (err) => {
}
})
},
onNetImageBtnClick() {
this.ocrText(this.netImageUrl)
},
rpx2px(rpx) {
let screenWidth = uni.getSystemInfoSync().screenWidth;
return Math.round(rpx * (screenWidth / 750));
},
ocrText(url) {
this.imageUrl = url;
this.showText = "";
this.showBlocks = [];
uni.showLoading({
title: "正在解析中..."
})
uni.getImageInfo({
src: url,
success: (res) => {
let setW = this.rpx2px(750);
this.scaleRatio = setW / res.width
}
});
mlkitorc.ocrText({
url: url,
language: this.language,
success: (opt) => {
this.showText = opt.text.replace(/\n/g, "<br/>");
this.showBlocks = opt.blocks;
uni.hideLoading()
},
fail: () => {
uni.hideLoading()
}
})
}
}
}
</script>
<style scoped lang="scss">
.text_block {
position: absolute;
border: 1px solid red;
color: red;
text-align: center;
}
.url_input {
border: 1px solid #e2e2e2;
width: 95%;
border-radius: 4rpx;
margin: 10rpx auto;
padding: 10rpx;
}
.raido_ul {
display: flex;
align-items: center;
height: 60rpx;
padding: 10rpx;
>view {
flex: 1;
text-align: center;
font-size: 26rpx;
line-height: 60rpx;
}
>view.active {
background: #007aff;
color: #fff;
border-radius: 4rpx;
}
}
</style>
需要帮助?有其他插件的需求?联系我!