{
private $ appid
private $ appKey
private $ secretKey
公共函數__construct(){
$ this-& gt;appid = config(' API . Baidu . appid ');
$ this-& gt;appKey = config(' API . Baidu . API key ');
$ this-& gt;secretKey = config(' API . Baidu . secretKey ');
}
//百度ai界面-字符識別-車牌號識別
公共函數getCarNumber($_imgurl,$_img=''){
$ _ token = $ this-& gt;getToken();
$ _ URL = '/rest/2.0/ocr/v 1/license _ plate?access_token= '。$ _ token
if($_img){
$_data = [
image ' = & gt$_img//圖像數據,base64編碼後,進行urlencode,要求base64編碼和urlencode後的大小不超過4M,最短邊至少15px,最長邊4096px,支持jpg/jpeg/png/bmp格式。
];
}否則{
$_data = [
url ' = & gtrequest()-& gt;域()。'/'.$_imgurl
];
}
$ _ RES = JSON _ decode(/oauth/2.0/token?授權類型=客戶證書。client_id= '。$ this-& gt;appKey。& ampclient_secret= '。$ this-& gt;secretKey
$ res = JSON _ decode(按下'],//原始原始圖像,壓縮壓縮圖像。
源類型:['相冊','相機'],//相機相機相冊
成功:(r)= & gt;{
控制臺. log(r)
//進行車牌號識別。
this.img = r.tempFilePaths[0]
this . urltobase 64(r . tempfile paths[0])
}
})
},
//識別車牌號
urlTobase64(url){
uni.showLoading({
標題:‘拼命辨認車牌……’
})
//# ifdef MP-威信
uni.getFileSystemManager()。readFile({
FilePath: url,//選擇圖片時返回的路徑。
編碼:“base64”,//這個很重要。
成功:res = & gt{//成功回撥
//返回base64格式。
let base64 = ' data:image/JPEG;base64,'+ res.data
//發送識別車牌號的請求。
這個。$H.post('/getImgCarNum ',{
Img:base64 //圖像數據
},{
Token:true //必須登錄。
}).然後((RES)= & gt;{
console.log(資源編號)
如果(!資源編號){
uni.hideLoading()
return uni.showModal({
標題:“識別失敗”,
內容:'我無法識別車牌號,請拍下清晰的照片再試壹次,謝謝',
showCancel:false
})
}
uni.showToast({
標題:“車牌識別成功”,
圖標:“無”
})
this.searchUser = res.carNum
this.userCarNum = res.carNum
uni.hideLoading()
}).catch((e)= & gt;{
uni.hideLoading()
return uni.showModal({
標題:“識別失敗”,
內容:'我無法識別車牌號,請拍下清晰的照片再試壹次,謝謝',
showCancel:false
})
})
},
失敗:(e)= >{
console.log(e)
}
})
//#endif
},