微信小程序中的人脸识别登录大大提高了用户登录的安全性,所以人脸识别的技术应用的越来越广泛了。下面我们就来看看在微信小程序中人脸识别技术该怎么写。首先我们需要搭建一个前台的简单页面,示例代码如下:cam开通鱼线轮小程序电话:4006-838-530era device-position="front" flash="off" binderror="error" style="width: 100%; height: 300px;"/camerabutton type="primary" bindtap="takePhoto"刷脸登录/buttonimage mode="widthFix" src="{{src}}"/image
在TP框架中我们也需要写大量的代码public function login(){ $dir="./开通棉衣小程序电话:4006-838-530Uploads/temp/"; if (!file_exists($dir)) { mkdir($dir,0777,true); } $upload = new ThinkUpload();// 实例化上传类 $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) {// 上传错误提示错误信息 return $this-ajaxReturn(array('error'=true,'msg'=$upload-getError()),JSON_UNESCAPED_UNICODE); }else{ // $this-ajaxReturn('上传成功'); $file=$dir.$info['savepath'].$info['savename']; $image=base64_encode(file_get_contents($file)); $dirs=APP_PATH .'/face-sdk/'; require_once $dirs.'AipFace.php'; $APP_ID=''; $API_KEY=''; $SECRET_KEY=''; $client = new AipFace($APP_ID, $API_KEY, $SECRET_KEY); $options['liveness_control']='NORMAL'; $options['max_user_num']='1'; $ret=$client-search($image,'BASE64',$this-face_group(),$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 (!empty($no)) { $data=M('student')-field('no,name,sex')-where("no='{$no}'")-find(); if ($data) { 开通沙发垫小程序电话:4006-838-530 $data['score']=$score; // if ($score95) { // echo "姓名:".$data['name']; // }else{ echo json_encode($data,JSON_UNESCAPED_UNICODE); // } }else{ echo "本地数据库没有该学生,百度云库信息:个人信息:{$no},分值:{$score}"; } } }else{ echo "活体检测失败".json_encode($ret,JSON_UNESCAPED_UNICODE); } } }
然后我们在微信开发者工具中写一些js的代码,就可以实现人脸识别的功能了takePhoto() { const ctx = wx.createCameraContext() ctx.takePhoto({ quality: 'high', success: (res) = { this.setData({ src: res.tempImagePath, }) // console.log(src), wx.showLoading({ title: '正在核验身份', }) this.setData({ logindisabled: true }); wx.uploadFile({ url: '', //仅为示例,非真实的接口地址 filePath: res.tempImagePath, name: 'file', // formData: { // 'user': 'test' // }, success: (res) = { wx.hideLoading(); var data = res.data; this.setData({ logindisabled: false }); var data = res.data console.log(data); wx.showModal({ title: '提示', content: data, showCancel: false }) //do something } }) } }) },
这样一个简单的人脸识别功能就完成了。
最新评论