本文将介绍如何使用uniapp和百度AI开放平台的OCR(光学字符识别)API实现身份证、营业执照等卡证的识别和文字识别功能。
将图片文件路径转为base64格式
/** * @description 本地图片转base64方法(兼容APP、H5、小程序) * @param {number} path 图片本地路径 * @returns Promise对象 */ const toBase64 = (path) => { return new Promise((resolve, reject) => { // #ifdef APP-PLUS plus.io.resolveLocalFileSystemURL(path, (entry) => { entry.file((file) => { let fileReader = new plus.io.FileReader() fileReader.readAsDataURL(file) fileReader.onloadend = (evt) => { let base64 = evt.target.result.split(",")[1] resolve(base64) } }) }) // #endif // #ifdef H5 uni.request({ url: path, responseType: 'arraybuffer', success: (res) => { resolve(uni.arrayBufferToBase64(res.data)) } }) // #endif // #ifdef MP-WEIXIN uni.getFileSystemManager().readFile({ filePath: path, encoding: 'base64', success: (res) => { resolve(res.data) } }) // #endif }) } export { toBase64 }
data() { return { dataObj: { client_id: '填你自己的', client_secret: '填你自己的', } } },
props: { dataType: { type: String, default: 'idcard', } },
使用uni.chooseImage()选择需要识别的图片:
// 选择本地图片 chooseImage() { let self = this uni.chooseImage({ count: 1, success: (ress) => { uni.showLoading({ title: '正在识别中...' }) // 下面将图片本地路径转base64 convert.toBase64(ress.tempFilePaths[0]).then((res) => { self.getAccessToken(res, ress) }) }, fail(err) { uni.hideLoading() console.log(err) } }) },
调用相应的识别接口
根据需要识别的卡证类型,调用不同的OCR识别接口,传入图片base64和token进行识别:
请求百度AI开放平台的token接口,获取后续接口的访问凭证:
getAccessToken(path, ress) { let self = this uni.request({ url: 'https://aip.baidubce.com/oauth/2.0/token', data: { grant_type: 'client_credentials', client_id: self.dataObj.client_id, client_secret: self.dataObj.client_secret }, method: 'POST', header: { 'Content-Type': 'application/x-www-form-urlencoded' }, success: (res) => { if (self.dataType == 'idcard') { self.uploadImage(path, res.data.access_token, ress) } else { self.uploadlicense(path, res.data.access_token, ress) } uni.hideLoading() }, fail(err) { uni.hideLoading() console.log(err) } }) },
uploadImage(path, token) { uni.request({ url: 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic', data: { image: path, access_token: token }, method: 'POST', header: { 'Content-Type': 'application/x-www-form-urlencoded' }, success: (res) => { this.$emit('end', { animal: false, words: res.data.words_result }) } }) },
正反面都可以使用这个,请求返回内容不一样。
// 身份证识别 uploadImage(path, token, ress) { let self = this uni.request({ url: 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard', data: { image: path, access_token: token, id_card_side: 'back' }, method: 'POST', header: { 'Content-Type': 'application/x-www-form-urlencoded' }, success: (res) => { uni.hideLoading() self.$emit('end', { path: ress, animal: false, words: res.data }) }, fail(err) { uni.hideLoading() console.log(err) } }) },
// 营业执照识别Business license uploadlicense(path, token, ress) { let self = this uni.request({ url: 'https://aip.baidubce.com/rest/2.0/ocr/v1/business_license', data: { image: path, access_token: token, }, method: 'POST', header: { 'Content-Type': 'application/x-www-form-urlencoded', }, success: (res) => { uni.hideLoading() self.$emit('end', { path: ress, animal: false, words: res.data }) }, fail(err) { uni.hideLoading() console.log(err) } }) },
// 通用文字识别(高精度版) uploadImage(path, token) { uni.request({ url: 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic', data: { image: path, access_token: token }, method: 'POST', header: { 'Content-Type': 'application/x-www-form-urlencoded' }, success: (res) => { this.$emit('end', { animal: false, words: res.data.words_result }) } }) },
以上就是uniapp使用百度AI平台OCR API实现卡证识别和文字识别的整体实现过程全部内容了,有不懂的,或者我代码有误的地方,希望大家多多交流。具体详细代码示例可以私信问我要哈!