扫脸登录php开发,微信小程序实现刷脸登录

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

时间: 2018-05-23

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值