1.首先你要有一个可以就行人脸识别的服务器,然后就是上传到百度云,百度云人脸识别的API接口全面升级到V3版本,并进行开放测试
2.wxml代码
<cameradevice-position="{{sxt}}"flash="off"binderror="error"style="width:100%;height:300px;"></camera><viewclass="weui-cellsweui-cells_after-title"><viewclass="weui-cellweui-cell_switch"><viewclass="weui-cell__bd"></view><!--<viewclass="weui-cell__ft">--><inputname='sex'value='{{sxt}}'>{{sxt}}</input><switchcheckedbindchange='switch1Change'/><!--</view>--></view></view><buttontype="primary"bindtap="takePhoto">拍照</button>
3.js代码
data:{sxt:'前置'},switch1Change:function(e){console.log(e)varfront='前置'varback='后置'if(this.data.sxt=='前置'){this.setData({sxt:back})}elseif(this.data.sxt=='后置'){this.setData({sxt:front})}},takePhoto(){constctx=wx.createCameraContext()ctx.takePhoto({quality:'high',success:(res)=>{//console.log(res);this.setData({src:res.tempImagePath})wx.showLoading({title:'正在核验身份...',})this.setData({logindisabled:true});wx.uploadFile({url:'http://www.swinder.top/server/index.php/home/index/login',filePath:res.tempImagePath,name:'file',success:(res)=>{wx.hideLoading();this.setData({logindisabled:false});vardata=res.dataconsole.log(data);wx.showModal({title:'提示',content:data,showCancel:false})}})}})}
4.后台php代码
privatefunctioninit_face(){$APP_ID='';//在百度云上查看自己的信息$API_KEY='';$SECRET_KEY='';$dir=APP_PATH.'/face-sdk/';require_once$dir.'AipFace.php';returnnew\AipFace($APP_ID,$API_KEY,$SECRET_KEY);}publicfunctionlogin(){//上传文件路径$dir="./Uploads/temp/";if(!file_exists($dir)){mkdir($dir,0777,true);}$upload=new\Think\Upload();//实例化上传类$upload->maxSize=2048000;//设置附件上传大小2m$upload->exts=array('jpg','gif','png','jpeg');//设置上传类型$upload->rootPath=$dir;//设置附件上传根目录$upload->savePath='';//设置附件上传(子)目录$upload->autoSub=false;//上传文件$info=$upload->uploadOne($_FILES['file']);if(!$info){//上传错误提示信息echojson_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',$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;echojson_encode($data,JSON_UNESCAPED_UNICODE);}else{//本地库不存在此学号echo"本地数据库没有该学生,百度云库信息:个人信息:{$no},分值:{$score}";}}}else{echo"活体检测失败,".json_encode($ret,JSON_UNESCAPED_UNICODE);}}}