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("请求解析失败"); } }); } }