微信小程序实现刷脸登录 微信小程序实现刷脸登录

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

微信小程序实现刷脸登录 微信小程序实现刷脸登录

bonnie_o   2021-03-31 我要评论

微信小程序本身并不具有刷脸登录的功能,实现时需要借助百度云的人脸识别的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调用后台代码即可。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们