关于node.js:最简单的-nodejs-百度-ocr身份证识别demo

6次阅读

共计 865 个字符,预计需要花费 3 分钟才能阅读完成。

创立一个文件夹
装置一个 申请工具 我用的 reqwest,还须要 xhr2

代码如下

const reqwest = require("reqwest");
const path = require("path");
const fs = require("fs");

(async function () {
    // 获取 access_toekn 30 天无效
    let access_token = await get_access_token() 
    // 读取身份证,转为 base64
    var data = fs.readFileSync(`./sfz/1.jpg`);
    data = data.toString('base64');
    // 间接申请,相干 api+access_token
    // 参数两个必传项,//image: base64 图片,
    //id_card_side: 'front 或 back' 正反面的意思

        let res = await reqwest({
            url: 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=' + access_token,
            contentType: 'application/x-www-form-urlencoded',
            method: "post",
            data: {
                image: data,
                id_card_side: 'front'
            }
        })
        console.log(res)// 这里就是后果了




})()
// 获取 access_token 但写的办法
async function get_access_token() {
    let res = await reqwest({
        method: 'post',
        type: 'json',
        url: 'https://aip.baidubce.com/oauth/2.0/token',
        data: {
            'grant_type': 'client_credentials',
            'client_id': 'appid',// 百度提供
            'client_secret': 'appkey'// 百度提供
        }
    })
    return res.access_token
}
正文完
 0