因公司业务需求以及方便用户,需要用户上传身份证后自动识别其中姓名身份证号码信息,方便用户操作,不需要上传身份证后再去重新输入姓名以及身份证号码。因此记录一下
微信小程序ocr可识别银行卡、身份证、驾驶证、行驶证、营业执照等,这里仅以身份证识别为例。
接口文档: ocr文档
不多说,直接贴代码
wx.uploadFile({
url: `https://api.weixin.qq.com/cv/ocr/idcard?type=Front&access_token=access_token`, //仅为示例
filePath: path,
name: 'img',
formData: {
contentType: 'image/png',
value: "",
},
success: (a) => {
let data = JSON.parse(a.data)
if (data.errcode == 0) {
} else if (data.errcode == '101001') {
uni.showToast({
title: "图片中无法找到证件!",
icon: 'none',
duration: 2000
});
} else if (data.errcode == '-1') {
uni.showToast({
title: "系统错误,请稍后重试!",
icon: 'none',
duration: 2000
});
} else if (data.errcode == '101002') {
uni.showToast({
title: "图片数据无效!",
icon: 'none',
duration: 2000
});
} else if (data.errcode == '101000') {
uni.showToast({
title: "图片URL错误或拉取URL图像错误!",
icon: 'none',
duration: 2000
});
}
}
})
})
这里没有使用img的url 而是formData参数,使用的wx.uploadFile