接下來就是在寫一個頁面读串,是刷臉頁面,通過這個頁面你將自己的臉拍照撒妈,傳入到自己的服務器上恢暖,去與存入百度云上面的照片進行對比。下面是代碼狰右。
前臺代碼:wxml
<camera device-position="front" flash="off" binderror="error" style="width: 100%; height: 300px;"></camera>
<view class="weui-cell weui-cell_switch">
? ? ? ? ? ? ? ? <view class="weui-cell__ft">
? ? ? ? ? ? ? ? ? ? <switch checked bindchange="switch1Change" />
? ? ? ? ? ? ? ? </view>
? ? ? ? ? ? </view>
<button type="primary" bindtap="takePhoto">刷臉登錄</button>
JS:
data: {
? ? // switch1Change:true
? ? path:null,
? ? status:'front'
? },
? switch1Change: function (e) {
? ? if(e.detail.value){
? ? ? ? ? this.setData({status:'back'})
? ? }else{
? ? ? ? ? this.setData({status:'front'})
? ? }
? },
? takePhoto() {
? ? const ctx = wx.createCameraContext()
? ? ctx.takePhoto({
? ? ? quality: 'high',
? ? ? success: (res) => {
? ? ? ? this.setData({
? ? ? ? ? src: res.tempImagePath
? ? ? ? })
? ? ? ? wx.uploadFile({
? ? ? ? ? url: 'http://www.anweimin.top/miniprgram-php/server/index.php/home/index/login',
? ? ? ? ? filePath: this.data.src,
? ? ? ? ? name: 'file',
? ? ? ? ? success: (res) => {
? ? ? ? ? ? var data = res.data;
? ? ? ? ? ? console.log(data);
? ? ? ? ? }
? ? ? ? })
? ? ? }
? ? })
? },
后臺代碼杰捂,這個方法是將你刷臉是拍的照片上傳到服務器上與百度云對比,并返回數(shù)據(jù)棋蚌,返回的數(shù)據(jù)中嫁佳,其中一個是兩張照片的相似度,相似度在百分之九十五以上谷暮,則是本人蒿往,
public function login(){
? ? ? ? //上傳文件路徑
? ? ? $dir ="./Upload/temp/";
? ? ? if(!file_exists($dir)){
? ? ? ? ? mkdir($dir,0777,true);
? ? ? }
? ? ? ? $upload = new \Think\Upload();// 實例化上傳類
? ? ? ? $upload->maxSize = 2048000 ;// 設置附件上傳大小
? ? ? ? $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 設置附件上傳類型
? ? ? ? $upload->rootPath = $dir; // 設置附件上傳根目錄
? ? ? ? $upload->savePath = ''; // 設置附件上傳(子)目錄
? ? ? ? //阻止建文件夾
? ? ? ? $upload->autoSub = false;
? ? ? ? // 上傳文件
? ? ? ? $info =$upload->uploadOne($_FILES['file']);
? ? ? ? if(!$info){
? ? ? ? ? //上傳錯誤提示信息
? ? ? ? ? ? echo json_encode(array('error'=>true,'msg'=>$uplaod->getError()),JSON_UNESCAPED_UNICODE);
? ? ? ? }else{//上傳成功
? ? ? ? ? ? // $this->success('上傳成功');
? ? ? ? ? ? $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','pingjiao',$options);
? ? ? ? ? ? echo json_encode($ret,JSON_UNESCAPED_UNICODE);
? ? ? ? ? ? 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;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? }