{
private $appid;
private $appKey;
private $secretKey;
public function __construct(){
$this->appid= config('api.baidu.appid');
$this->appKey = config('api.baidu.apikey');
$this->secretKey = config('api.baidu.secretkey');
}
//百度ai接口--文字識別--車牌號識別
public function getCarNumber($_imgurl,$_img=''){
$_token = $this->getToken();
$_url = '/rest/2.0/ocr/v1/license_plate?access_token='.$_token;
if($_img){
$_data = [
'image'=>$_img//圖像數據,base64編碼後進行urlencode,要求base64編碼和urlencode後大小不超過4M,最短邊至少15px,最長邊最大4096px,支持jpg/jpeg/png/bmp格式
];
}else{
$_data = [
'url'=>request()->domain().'/'.$_imgurl
];
}
$_res = json_decode(/oauth/2.0/token?grant_type=client_credentials&client_id='.$this->appKey.'&client_secret='.$this->secretKey;
$res = json_decode(pressed'],//original 原圖,compressed 壓縮圖
sourceType: ['album','camera'],//camera 相機 album相冊
success:(r)=>{
console.log(r)
//執行識別車牌號碼
this.img = r.tempFilePaths[0]
this.urlTobase64(r.tempFilePaths[0])
}
})
},
//識別車牌號碼
urlTobase64(url){
uni.showLoading({
title:'拼命識別車牌中..'
})
//#ifdef MP-WEIXIN
uni.getFileSystemManager().readFile({
filePath: url, //選擇圖片時返回的路徑
encoding: "base64",//這個是很重要的
success: res => { //成功的回調
//返回base64格式
let base64 = 'data:image/jpeg;base64,' + res.data
//發送請求,識別車牌號碼
this.$H.post('/getImgCarNum',{
img:base64 //圖片數據
},{
token:true //必須登錄
}).then((res)=>{
console.log(res.carNum)
if(!res.carNum){
uni.hideLoading()
return uni.showModal({
title:'識別失敗',
content:'沒能識別到車牌號碼,請拍張清晰的圖片再試哦,謝謝',
showCancel:false
})
}
uni.showToast({
title:'識別車牌成功',
icon:'none'
})
this.searchUser = res.carNum
this.userCarNum = res.carNum
uni.hideLoading()
}).catch((e)=>{
uni.hideLoading()
return uni.showModal({
title:'識別失敗',
content:'沒能識別到車牌號碼,請拍張清晰的圖片再試哦,謝謝',
showCancel:false
})
})
},
fail:(e)=>{
console.log(e)
}
})
//#endif
},