微信小程序本身并不具有刷臉登錄的功能,實現時需要借助百度云的人臉識別的sdk,當然現在百度云是暫時免費的,我們可以登錄自行下載使用。
下載地址
后臺代碼我們使用thinkPHP框架實現,將下載好的sdk導入thinkPHP框架中,就可以寫方法使用了。
我們需要將圖片上傳到自己服務器和百度云人臉庫中。
public function login(){ $dir="./***";//自己定義的文件名 if(!file_exists($dir)){ mkdir($dir,0777,true); } $upload = new /Think/Upload();// 實例化上傳類 $upload->maxSize = 3145728 ;// 設置附件上傳大小 $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 設置附件上傳類型 $upload->rootPath = $dir; // 設置附件上傳根目錄 $upload->savepath=''; // 上傳單個文件 $info = $upload->uploadOne($_FILES['file']); if(!$info) {// 上傳錯誤提示錯誤信息 return $this->ajaxReturn(array('error'=>true,'msg'=>$upload->getError())); }else{// 上傳成功 獲取上傳文件信息 $file = $dir . $info['savepath'] . $info['savename']; $image = base64_encode(file_get_contents($file)); $client = $this->init_face(); $options['liveness_control']='NORMAL'; $options['max_user_num']='1'; $ret = $client->search($image,'BASE64',$this->face_group(),$options); if($ret['error_code']==0){ $user = $ret['result']['user_list'][0]; $no = $user['user_id']; $score = $user['score']; if(!empty($no)){ $data = M('student')->field('no,name,sex')->where("no='{$no}'")->find(); if($data){ //查到此學號 $data['score'] = $score; echo json_encode($data,JSON_UNESCAPED_UNICODE); }else{ //本地庫不存在此學號 echo "本地數據庫沒有該學生,百度云庫信息:個人信息:{$no},分值:{$score}"; } } }else{ echo "活體檢測失敗,".json_encode($ret,JSON_UNESCAPED_UNICODE); } } }
里面的search方法是sdk的方法
public function search($image, $imageType, $groupIdList, $options=array()){ $data = array(); $data['image'] = $image; $data['image_type'] = $imageType; $data['group_id_list'] = $groupIdList; $data = array_merge($data, $options); return $this->request($this->searchUrl, json_encode($data), array( 'Content-Type' => 'application/json', )); }
前三個參數是必填的,后面參數根據自己的實際情況選即可。
微信小程序中需要使用camera組件以及wx.createCameraContext()方法,之后在js調用后臺代碼即可。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
|
新聞熱點
疑難解答