直接上代码:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
</head>
<body>
<input type="file" id="img" onchange="getImg(event)" />
<img id="showImg" src="" />
<!--<script src="js/jquery.min.js" type="text/javascript" charset="utf-8"></script>-->
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
var access_token = "24.e71dad8fba3d53e2daa33592687b9188.2592000.1555582034.282335-15796956";//百度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("请求解析失败");
}
});
}
}
</script>
</body>
</html>
百度access_token自己去生成自己的即可,如果不会的可以去看百度官方文档,还不会的回复我,我教你。
也可以使用我弄好的文件直接用:下载地址:https://download.csdn.net/download/u012442504/11052365