• 如何使用php实现微信小程序刷脸登录


    1.这里我们以百度云的人脸库为准(其他的也可以),确保里面有我们的照片:

    在后台写刷脸登陆的接口login我们要把拍照获取的照片存储到服务器

    public function login(){ 
    
       // 上传文件路径 
    
       $dir = "./Uploads/temp/"; 
    
       if(!file_exists($dir)){ 
    
        mkdir($dir,0777,true); 
    
       } 
    
       $upload = new ThinkUpload(); 
    
       $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']; 
    
         } 
    
        } 
    
       } 
    
      }
    public function login(){ 
    
       // 上传文件路径 
    
       $dir = "./Uploads/temp/"; 
    
       if(!file_exists($dir)){ 
    
        mkdir($dir,0777,true); 
    
       } 
    
       $upload = new ThinkUpload(); 
    
       $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=" 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: "确定"
    
           }) 
    
          } 
    
         }) 
    
        } 
    
       }) 
    
      },

    这样刷脸登录就完成了

  • 相关阅读:
    HDU_5372 树状数组 (2015多校第7场1004)
    《 字典树模板_递归 》
    《神、上帝以及老天爷》
    《Crazy tea party》
    UVA_ Overflow
    UVA_If We Were a Child Again
    UVA_Product
    UVA_Integer Inquiry
    你也可以屌到爆的这样敲代码当黑客!
    大数相加_原创
  • 原文地址:https://www.cnblogs.com/mo3408/p/13191046.html
Copyright © 2020-2023  润新知