前言:
微信OCR能力已全面接入服务平台计费系统。除服务平台接入方式外,原内测API,插件接入方式也均已接入计费系统。2020.4.1起,已接入内测的开发者,免费额度会统一调整为100次/天。更强的能力需求,可以走服务市场调用。
目前使用需要到微信服务平台进行购买,购买后才能使用免费额度
实操:
这个比较方便,直接在前端搞定,不需要开发者服务器再进行API访问。一共就两个步骤:购买,加前端
小程序基础库:2.19.4
wxml部分:
<camera device-position="back" flash="off" binderror="error" style="width: 100%; height: 300px;"></camera>
<view style="flex-direction: row;text-align: center;">
<button size="mini" type="default" bindtap="photoAlbum">相册</button>
</view>
<view style="text-align: center;">预览</view>
<image mode="widthFix" src="{{src}}"></image>
js部分:
/**
* 相册
*/
photoAlbum() {
const thas = this;
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success(res) {
// tempFilePath可以作为img标签的src属性显示图片
const tempFilePath = res.tempFilePaths[0]
const fileSys = wx.getFileSystemManager();
const base64 = fileSys.readFileSync(tempFilePath, "base64");
thas.setData({
src: tempFilePath,
base64: base64
})
thas.ocr(tempFilePaths);
}
})
},
/**
* OCR 识别
* @param {*} temFilePath
*/
ocr(temFilePath){
const invokeRes = wx.serviceMarket.invokeService({
service: 'wx79ac3de8be320b71',
api: 'OcrAllInOne',
data: {
// 用 CDN 方法标记要上传并转换成 HTTP URL 的文件
img_url: new wx.serviceMarket.CDN({
type: 'filePath',
filePath: tempFilePath,
}),
data_type: 3,
ocr_type: 1
},
success:function(res){
wx.showModal({
title: 'success',
content: JSON.stringify(res),
})
}
})
},