一砌滞,獲取token
注冊baidu AL云賬號,登錄選擇圖像識別,創(chuàng)建應用
image.png
拿到API Key 和Secret Key,請求百度獲取Token接口
fetchToken: async function() {
let vm = this;
let params = {
grant_type:'client_credentials',
client_id:'Dt79Wwpqd4GmqPVZ9NmdkXYo',
client_secret:'VPyB2M7RdptgDkkjjP4dL6BbXwUB2qPa'
};
const res = await vm.http.get('/oauth/2.0/token', params);
if(!res){
return
}
let result = res.data;
vm.token = result.access_token;
},
ps:,如果是vue應用库说,一定要配置代理休玩,調用接口的url去掉域名前綴著淆,域名的配置寫到`vue.config.js`文件里面
'/oauth/*': {
target: 'https://aip.baidubce.com/',
changeOrigin: true,
secure: false
},
二,身份證信息識別
<input type="file" id="file" class="filepath" @change="changepic" accept="image/*"><br>
通過file
標簽上傳/拍照圖片拴疤,change
事件里面拿到base64
文件流永部,再調用身份識別接口
changepic() {
let vm = this;
let reads = new FileReader();
let f = document.getElementById('file').files[0];
reads.readAsDataURL(f);
reads.onload = function (e) {
vm.image = this.result;
vm.spinShow = true;
vm.fetchInfo();
};
},
/**
* 識別圖片信息
*/
fetchInfo() {
let vm = this;
$.ajax({
type: "POST",
url: "/rest/2.0/ocr/v1/idcard",
data: {
access_token:vm.token,
image:vm.image.replace("data:image/jpeg;base64,",""),
id_card_side:'front'
},
dataType: "json",
beforeSend: function(request) {
request.setRequestHeader("Content-Type", 'application/x-www-form-urlencoded');
},
success: function(data){
vm.spinShow = false;
console.log(data.words_result);
vm.result = data.words_result;
},
error: function(data){
vm.spinShow = false;
}
});
},
這樣就可以拿到身份證文字信息
三,圖像文字信息識別
先上傳呐矾,后識別苔埋,調用識別接口
fetchInfo1() {
let vm = this;
$.ajax({
type: "POST",
url: "/rest/2.0/ocr/v1/webimage",
data: {
access_token:vm.token,
image:vm.image1.replace("data:image/jpeg;base64,",""),
},
dataType: "json",
beforeSend: function(request) {
request.setRequestHeader("Content-Type", 'application/x-www-form-urlencoded');
},
success: function(data){
console.log(data);
vm.spinShow = false;
console.log(data.words_result);
vm.result1 = data.words_result;
},
error: function(data){
vm.spinShow = false;
}
});
}