更新记录

1.0(2026-01-07)

更新各平台OCR识别


平台兼容性

uni-app(4.0)

Vue2 Vue3 Chrome Safari app-vue app-nvue Android iOS 鸿蒙
- - - - - - 5.0 14 11
微信小程序 支付宝小程序 抖音小程序 百度小程序 快手小程序 京东小程序 鸿蒙元服务 QQ小程序 飞书小程序 快应用-华为 快应用-联盟
- - - - - - - - - - -

uni-app x(4.0)

Chrome Safari Android iOS 鸿蒙 微信小程序
- - 5.0 14 11 -

其他

多语言 暗黑模式 宽屏模式
× ×

WG-OCR 离线 OCR 文字识别UTS插件

基于原生高性能库实现的离线图文识别插件,支持中英文混合识别。支持Android/IOS/Harmony!

快速开始

基础用法

<template>
  <view>
    <button @click="recognizeImage">识别图片</button>
    <text>{{ resultText }}</text>
  </view>
</template>

<script>
import { WgOCRImpl } from "@/uni_modules/wg-ocr";

export default {
  data() {
    return {
      resultText: "",
      wgOcr: null,
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      this.wgOcr = new WgOCRImpl();
    },
    async recognizeImage() {
      //  选择图片
      const chooseRes = await uni.chooseImage({
        count: 1,
        sourceType: ["album", "camera"],
      });

      // 执行识别
      const result = await this.wgOcr.recognize(chooseRes.tempFilePaths[0]);

      // 处理结果
      if (result.success) {
        this.resultText = result.text;
        console.log("识别耗时:", result.duration + "ms");
      } else {
        console.error("识别失败:", result.message);
      }
    },
  },
};
</script>

方法

recognize(imagePath: string): Promise\<OCRResult>

识别图片中的文字。

参数:

参数名 类型 必填 说明
imagePath string 图片的本地路径,支持绝对路径和 file:// 协议

返回值: Promise<OCRResult>

示例:

const result = await wgOcr.recognize("/storage/emulated/0/DCIM/test.jpg");

类型定义

OCRResult

识别结果对象。

interface OCRResult {
  success: boolean; // 是否识别成功
  message: string; // 提示信息(成功或错误原因)
  text: string; // 识别出的完整文本
  blocks: OCRBlock[]; // 文本块详细信息数组
  duration: number; // 识别耗时(毫秒)
}

OCRBlock

文本块信息。

interface OCRBlock {
  text: string; // 该文本块的内容
  confidence: number; // 置信度 (0-1)
  left: number; // 文本块左上角 X 坐标
  top: number; // 文本块左上角 Y 坐标
  width: number; // 文本块宽度
  height: number; // 文本块高度
}

📝 注意事项

图片要求

为获得最佳识别效果,建议:

  1. 清晰度:图片清晰,文字边缘锐利
  2. 光线充足:避免过暗或过曝
  3. 角度正确:尽量正面拍摄,避免倾斜
  4. 大小适中:建议宽度 1000-2000px
  5. ⚠️ 格式支持:JPG、PNG(推荐)

隐私、权限声明

1. 本插件需要申请的系统权限列表:

<uses-permission android:name="android.permission.CAMERA" />

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

暂无用户评论。