PHP實現微信小程式Face Service刷臉登入功能

來源:互聯網
上載者:User
本文通過執行個體代碼給大家講解了基於PHP實現小程式Face Service刷臉登入功能,感興趣的朋友一起學習吧

首先我們先確認我們的百度雲人臉庫裡已經上傳了我們的個人資訊照片

然後我們在後台寫刷臉登陸的介面login我們要把拍照擷取的照片儲存到伺服器

public function login(){    // 上傳檔案路徑    $dir = "./Uploads/temp/";    if(!file_exists($dir)){     mkdir($dir,0777,true);    }    $upload = new \Think\Upload();    $upload->maxSize = 2048000 ;// 設定附件上傳大小    $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 設定附件上傳類型    $upload->savepath = '';    $upload->autoSub = false;    $upload->rootPath = $dir; // 設定附件上傳根目錄    // 上傳單個檔案    $info = $upload->uploadOne($_FILES['file']);    if(!$info) {// 上傳錯誤提示錯誤資訊      echo json_encode(array('error'=>true,'msg'=>$upload->getError()),JSON_UNESCAPED_UNICODE);    }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','student',$options);     // echo json_encode($ret,JSON_UNESCAPED_UNICODE);     // exit;     if($ret['error_code']==0){      $user = $ret['result']['user_list'][0];      $no = $user['user_id'];      $score = $user['score'];      if($score>=95){       $data = M('student')->where("no = '{$no}'")->find();       $data['score'] = $score;       // $data['name'] = json_decode($data['name'],true);       // $data['sex'] = json_decode($data['sex'],true);       echo '識別成功' . json_encode($data,JSON_UNESCAPED_UNICODE);      }else{       echo '識別失敗' . $data['score'];      }     }    }   }

然後進行前台設計

<camera device-position="{{device?'back':'front'}}" flash="off" binderror="error" style="width: 100%; height: 300px;"></camera>     <view class="weui-cells__title" >開關</view>     <view class="weui-cells weui-cells_after-title">       <view class="weui-cell weui-cell_switch">         <view class="weui-cell__bd">切換網路攝影機</view>         <view class="weui-cell__ft" >           <switch bindtap="devicePosition" />         </view>       </view>     </view> <button type="primary" bindtap="takePhoto">刷臉登入</button>

我們還可以控制相機的前後鏡頭

devicePosition() { this.setData({  device: !this.data.device, }) console.log("當前相機網路攝影機為:", this.data.device ? "後置" : "前置"); camera() {  let { ctx, type, startRecord } = this.data; }, data: {  src: null, },

在js裡面調用介面

takePhoto() {    const ctx = wx.createCameraContext()    ctx.takePhoto({     quality: 'high',     success: (res) => {      this.setData({       src: res.tempImagePath      })      console.log(res)      wx.uploadFile({       url: '', //僅為樣本,非真實的介面地址       filePath: this.data.src,       name: 'file',       formData: {       },       success: function (res) {        // var data = res.data        // var json = JSON.parse(data)        console.log(res)        wx.showModal({         title: "提示",         content: res.data,         showCancel: false,         confirmText: "確定"        })       }      })     }    })   },

刷臉登入就成功了

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.