經過調用百度OCR的兩個接口,實現身份證圖像識別。javascript
首先要在百度雲註冊帳號,並建立應用,以獲取AppID,API Key,Secret Key。html
官網文字識別模塊地址:https://cloud.baidu.com/product/ocr.htmljava
點擊文字識別jquery
建立應用ajax
以後就可使用相關接口了,這裏咱們使用的是身份證識別json
附上身份證識別文檔地址:https://cloud.baidu.com/doc/OCR/OCR-API.html#.E8.BA.AB.E4.BB.BD.E8.AF.81.E8.AF.86.E5.88.ABapp
代碼:async
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <input type="file" id="img" onchange="getImg(event)" /> <img id="showImg" src="" /> </body> <script type="text/javascript" src="jquery-3.3.1.min.js" ></script> <script> 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/png;base64,",""); $("#showImg").prop("src", "data:image/png;base64," + imageBase); $.ajax({ header: { "Content-Type": "application/x-www-form-urlencoded" }, type: "post", url: "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard", async: true, data: { access_token: access_token, id_card_side: "front", image: imageBase }, dataType: "json", timeout: 30000, success: function (data) { console.log("解析成功"); console.log(data); }, error: function (xhr) { console.log("請求解析失敗"); } }); } } </script> </html>
最終效果圖:ide