首页 > 建站教程 > CSS3+HTML5 >  HTML5基于百度ORC api识别身份证和驾驶证正文

HTML5基于百度ORC api识别身份证和驾驶证

工作中遇到了HTML5公众号,需要拾取图片,识别图片中的身份证号、驾驶证。也就是ORC。注册了百度的ORC,将图片转为base64,直接使用百度的ORC接口实现了。估计将来可能还会遇到,于是记录一下:

html代码:
<input type="file" id="img" onchange="getImg(event)" />
<img id="showImg" src="" />
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
javascript代码:
var access_token = "****************";//百度access_token
// 监听图片选择事件
function getImg (event) {
    var imageBase = "";
    var reader = new FileReader();
    reader.readAsDataURL(event.target.files[0]);
    reader.onload = function (e) {
        imageBase = e.target.result.replace("data:image/jpeg;base64,","");
        $("#showImg").prop("src", "data:image/jpeg;base64,"+ imageBase);
        $.ajax({
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            type: "post",
            //url: "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard",//身份证
            url: "https://aip.baidubce.com/rest/2.0/ocr/v1/driving_license",//驾驶证
            async: true,
            data: {
                access_token: access_token,
                //id_card_side: "front",
                //id_card_side: "back",
                detect_direction:true,
                unified_valid_period:true,
                image: imageBase
            },
            dataType: "json",
            timeout: 30000,
            success: function (data) {
                console.log("解析成功");
                console.log(data);
            },
            error: function (xhr) {
                console.log("请求解析失败");
            }
        });
    }
}