小程序获取OCR识别结果,示例代码
//获取OCR识别结果,示例代码--start
wx.chooseMedia({
count: 1,
mediaType: ['image'],
sourceType: ['album', 'camera'],
maxDuration: 30,
camera: 'back',
success: (res) => {
if(res.errMsg=="chooseMedia:ok"){
let tempImgPath = res.tempFiles[0].tempFilePath;
// const imgBase64 = uni.getFileSystemManager().readFileSync(tempImgPath, "base64");
wx.serviceMarket.invokeService({
service: 'wx79ac3de8be320b71', // '固定为服务商OCR的appid,非小程序appid',
api: 'OcrAllInOne',//服务名称
data: {
img_url: new wx.serviceMarket.CDN({
type: 'filePath',
filePath: tempImgPath// 使用选择的第一张图片的路径
}),
data_type: 3,//1:二进制;2:base64字符串;3:图片url
ocr_type: 1,//1:身份证;2:银行卡;3:行驶证;4:驾驶证;7:营业执照;8:通用OCR ; 10: 车牌识别
},
}).then(res => {
console.log('invokeService success', res)
//res 成功返回正面示例报文:{"errMsg":"invokeService:ok","data":{"idcard_res":{"type":0,"name":{"text":"崔效伟"},"gender":{"text":"男"},"nationality":{"text":"汉"},"birth":{"text":"2000-03-10"},"address":{"text":"山西省忻州市忻府区解原乡东社村029号"},"id":{"text":"142201200003104873"},"card_position":{"pos":{"left_top":{"x":503.8359375,"y":0},"right_top":{"x":503.8359375,"y":776},"right_bottom":{"x":6.0703125,"y":776},"left_bottom":{"x":6.0703125,"y":0}}},"image_width":516,"image_height":777,"card_property":4}},"requestId":"MLzHwxChFkPT-6Yqi_YFXN3exxf6uL9wYiaRFKet9xeqaM6da2lzO3HF4vsI-j8RxbQ"};
// wx.showModal({
// title: 'cost',
// content: (Date.now() - d) + ''
// })
}).catch(err => {
console.error('invokeService fail', err)
// wx.showModal({
// title: 'fail',
// content: err + '',
// })
})
}else{
uni.showToast({
title: res.errMsg,
icon: "none",
position: 'center',
duration: 3000
});
}
},fail: (res) => {
console.log("选择身份证照片失败:"+res.errMsg);
}
});
//获取OCR识别结果,示例代码--end
参考地址:https://blog.csdn.net/weixin_36754290/article/details/151318521

浙公网安备 33010602011771号