当前位置: 首页 > 后端技术 > Node.js

最简单的nodejs百度ocr身份证识别demo

时间:2023-04-03 15:04:31 Node.js

创建文件夹安装reqwest的request工具,还需要xhr2代码如下constreqwest=require("reqwest");constpath=require("path");constfs=require("fs");(asyncfunction(){//获取access_toekn有效期为30天letaccess_token=awaitget_access_token()//读取身份证转base64vardata=fs.readFileSync(`./sfz/1.jpg`);data=data.toString('base64');//直接请求,相关api+access_token//参数为必填项,//image:base64图片,//id_card_side:'frontorback'表示前后letres=awaitreqwest({url:'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token='+access_token,contentType:'application/x-www-form-urlencoded',方法:"post",data:{image:data,id_card_side:'front'}})console.log(res)//这里是结果})()//获取access_token但是写方法asyncfunctionget_access_token(){letres=awaitreqwest({方法:'post',类型:'json',url:'https://aip.baidubce.com/oauth/2.0/token',data:{'grant_type':'client_credentials','client_id':'appid',//百度提供'client_secret':'appkey'//百度提供}})returnres.access_token}