uniapp 前端实现文字识别,身份证识别,营业执照识别 (兼容APP、H5、小程序 不需要任何SDK

APP小程序H5

准备工作

1. 注册百度账号
  1. 前往百度AI开放平台官网,点击“登录”。使用百度账号登录,如果没有可以先注册百度账号。
  2. 登录成功后,点击右上角的“开发者服务”->“API 服务”。
2. 进入开发者平台,创建OCR文字识别应用
  1. 在AI能力服务类目中选择“OCR文字识别”,进入文字识别产品的应用页。
  2. 点击“立即使用”,进入OCR文字识别应用创建页。
3. 获取AppID和AK
  1. 输入应用名称,选择应用类型为“公开应用”或“私有应用”,填写验证码后点击“创建应用”。
  2. 应用创建成功把对应的client_id和client_secret保存好添加到项目里

图片转base64(兼容APP、H5、小程序)

新建一个js文件导出toBase64函数

将图片文件路径转为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
}


具体代码

1. 在data里声明申请好的秘钥
		data() {
			return {
				dataObj: {
					client\_id: '填你自己的',
					client\_secret: '填你自己的',
				}
			}
		},

2. 声明接受参数dataType,用于判断需要识别的类型
		props: {
			dataType: {
				type: String,
				default: 'idcard',
			}
		},

3. 选择本地图片

使用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)
					}
				})
			},

4. 获取AccessToken

调用相应的识别接口
根据需要识别的卡证类型,调用不同的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)
					}
				})
			},

5.通用文字识别(高精度版)
			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) => {


### 文末
篇幅有限没有列举更多的前端面试题,小编把整理的前端大厂面试题PDF分享出来,一共有269页

![](https://img-blog.csdnimg.cn/img_convert/04273c2f50e58b6822663c5b2f379206.png)  

![](https://img-blog.csdnimg.cn/img_convert/081b5236ebd9ff24af676bcd4a51c99e.png)

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值