第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > 调用百度OCR接口识别身份证 微信小程序

调用百度OCR接口识别身份证 微信小程序

时间:2020-01-16 06:04:46

相关推荐

调用百度OCR接口识别身份证 微信小程序

申请百度AI账号获取百度OCR接口API Key和Secret Key

申请地址:

/?track=cp:aipinzhuan|pf:pc|pp:AIpingtai|pu:title|ci:|kw:10005792

除了身份证识别,百度还提供了人脸识别、车牌识别等接口,有兴趣都可以申请试试,日请求量不超过500好像是免费的。

身份证识别接口相关文档地址/ai-doc/OCR/rk3h7xzck,申请过程文档基本上有说明

Access Token

本文档主要针对HTTP API调用者,百度AIP开放平台使用OAuth2.0授权调用开放API,调用API时必须在URL中带上access_token参数,获取Access Token的流程如下:

向授权服务地址/oauth/2.0/token发送请求(推荐使用POST),并在URL中带上以下参数:

grant_type: 必须参数,固定为client_credentials;

client_id: 必须参数,应用的API Key;

client_secret: 必须参数,应用的Secret Key;

例如

/oauth/2.0/token?grant_type=client_credentials&client_id=Va5yQRHlA4Fq5eR3LT0vuXV4&client_secret=0rDSjzQ20XUj5itV6WRtznPQSzr5pVw2&

获取access_token示例代码

#!/bin/bashcurl -i -k '/oauth/2.0/token?grant_type=client_credentials&client_id=【百度云应用的AK】&client_secret=【百度云应用的SK】'

**百度云应用的AK(API Key)和SK(Secret Key)获取参考:/doc/Reference/s/9jwvz2egb

服务器返回的JSON文本参数如下:

access_token: 要获取的Access Token; expires_in: Access

Token的有效期(秒为单位,有效期30天);

其他参数忽略,暂时不用;

例如:

{"refresh_token": "25.b55fe1d287227ca97aab219bb249b8ab.315360000.1798284651.282335-8574074","expires_in": 2592000,"scope": "public wise_adapt","session_key": "9mzdDZXu3dENdFZQurfg0Vz8slgSgvvOAUebNFzyzcpQ5EnbxbF+hfG9DQkpUVQdh4p6HbQcAiz5RmuBAja1JJGgIdJI","access_token": "24.6c5e1ff107f0e8bcef8c46d3424a0e78.2592000.1485516651.282335-8574074","session_secret": "dfac94a3489fe9fca7c3221cbf7525ff"}

若请求错误,服务器将返回的JSON文本包含以下参数:

例如认证失败返回:

鉴权认证错误码

{"error": "invalid_client","error_description": "unknown client id"}

error: 错误码;关于错误码的详细信息请参考下方鉴权认证错误码。 error_description:

错误描述信息,帮助理解和解决发生的错误。

以下是整体调用代码

function OcrIdCard(access_token) {return new Promise(function (resolve, reject) {//识别身份证wx.chooseImage({//选择身份证图片/拍照count: 1,sizeType: ['original', 'compressed'],sourceType: ['camera'],success: function (res) {//调用照片选择成功的回调函数console.log(res.tempFilePaths)//图片编码部分核心代码wx.getFileSystemManager().readFile({filePath: res.tempFilePaths[0],encoding: 'base64', //编码格式success(ans) {// console.log(ans.data)wx.showLoading({title: '识别中' })//ans.data:保存了图片转码之后的数据// 请求获取tokenwx.request({//url中的&client_id=client-i&client_secret=client—s中的参数client-i和client—s需要申请百度身份证识别的账号和密码,具体申请流程参考上面url: '/oauth/2.0/token?grant_type=client_credentials&client_id=client_id&client_secret=client_secret',data: {},//请求参数,此处没有参数,则置空header: {'content-type': 'application/x-www-form-urlencoded' // 默认值},success(rep) {var access_token = rep.data.access_token;console.log("access_token:", access_token)//带着token与转码后的图片编码请求百度OCR接口,对身份证信息进行识别wx.request({url: '/rest/2.0/ocr/v1/idcard?access_token=' + access_token,method: 'POST',header: {'Content-Type': 'application/x-www-form-urlencoded'},data: {image: ans.data, //ans.data:图片编码id_card_side: 'front'},success(_res) {wx.hideLoading();resolve(_res)console.log("身份证识别成功:", _res)}, fail(_res) {wx.hideLoading();wx.showToast({title: '请求出错',icon:'none'})reject(_res)}})},fail(rep) {wx.hideLoading();wx.showToast({title: '请求出错',icon:'none'})reject(rep)}});},fail(res) {wx.hideLoading();wx.showToast({title: '所选图片编码失败,请重试',icon:'none'})reject(res)}})},fail(res) {wx.hideLoading();wx.showToast({title: '图片选择失败,请重试',icon:'none'})reject(res)}})})}module.exports = {OcrIdCard: OcrIdCard}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。