2025-10-11 【小程序】微信小程序OCR服务

1. 结论

使用微信服务市场,“微信 OCR 识别”服务“纯前端”调用,每日 100 次免费调用额度(前期进件量不大能够覆盖日常使用场景)

2. 代码示例

  • 步骤
  1. 选择相册/相机
  2. 上传 CDN(图片太大 256kb 限制,上传到微信 CDN 进行压缩)
  3. 调用服务市场的 OcrAllInOne 服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// 1:身份证;2:银行卡;3:行驶证;4:驾驶证;7:营业执照;8:通用OCR ; 10: 车牌识别

handleImageUploadClick() {
const selft = this;
wx.chooseMedia({
count: 1,
mediaType: ["image"],
sourceType: ["album", "camera"],
maxDuration: 30,
camera: "back",
async success(res) {
let ocr_type = 1;

let img_url = new wx.serviceMarket.CDN({
type: "filePath",
filePath: res.tempFiles[0].tempFilePath,
});
wx.serviceMarket
.invokeService({
service: "wx79ac3de8be320b71",
api: "OcrAllInOne",
data: {
// 用 CDN 方法标记要上传并转换成 HTTP URL 的文件
data_type: 3,
img_url,
ocr_type,
},
})
.then((res) => {
console.log("invokeService success", res);
wx.showModal({
title: "cost",
content: res + "",
});
})
.catch((err) => {
// 错误码107,原因为没有可用资源次数
console.error("invokeService fail", err);
wx.showModal({
title: "fail",
content: err + "",
});
});
},
});
},

3. 问题

  1. 返回错误码 103,原因:未开通 OCR 服务权限
  2. 返回错误码 107,原因:调用次数超限

4. 参考文献


2025-10-11 【小程序】微信小程序OCR服务
https://zhangyingxuan.github.io/2025-10-11 【小程序】微信小程序OCR服务/
作者
blowsysun
许可协议