作者:王意之 | 来源:互联网 | 2023-08-29 15:02
在百度云中存入照片
- chooseImage:function(){
- var that=this
- wx.chooseImage({
- count: 1,
- sizeType: ['original', 'compressed'],
- sourceType: ['album', 'camera'],
- success: function (res) {
-
- var imageList = res.tempFilePaths;
- that.setData({
- imageList: res.tempFilePaths
- })
- console.log(imageList);
- }
- })
- },
- previewImage: function (e) {
- var current = e.target.dataset.src
- wx.previewImage({
- current: current,
- urls: this.data.imageList
- })
- },
- upload:function(){
- wx.uploadFile({
- url: 'http://bestwangyuan.top/face/index.php/home/index/upload', //仅为示例,非真实的接口地址
- filePath: this.data.imageList[0],
- name: 'file',
- formData: {
- 'user': 'test'
- },
- success: function (res) {
- var data = res.data
- console.log(data);
- var json=JSON.parse(res.data);
-
- wx.showToast({
- title: json.msg,
- icon: 'none',
- duration: 3000,
- })
- }
- })
- }
在用php把照片上传到照片库里面
方法如下
- public function sdk(){
- $file='./Uploads/111.jpg';
- if(!file_exists($file)){
- die('文件不存在');
- }
- $dir=APP_PATH .'/face-sdk/';
- require_once $dir .'AipFace.php';
- $APP_ID='';
- $API_KEY='';
- $SECRET_KEY='';
- $client=new \AipFace($APP_ID,$API_KEY,$SECRET_KEY);
-
- $image=file_get_contents($file);
- $image=base64_encode($image);
-
- $imageType='BASE64';
- $options=array();
- $options["max_face_num"]=10;
- $ret=$client->detect($image,$imageType,$options);
- print_r($ret);
-
-
-
- }
然后我们在后台写刷脸登陆的接口login我们要把拍照获取的照片存储到服务器
- public function login(){
-
- $dir = "./Uploads/temp/";
- if(!file_exists($dir)){
- mkdir($dir,0777,true);
- }
- $upload = new \Think\Upload();
- $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);
-
-
- 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;
-
-
- echo '识别成功' . json_encode($data,JSON_UNESCAPED_UNICODE);
- }else{
- echo '识别失败' . $data['score'];
- }
- }
- }
- }
在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: "确定"
- })
- }
- })
- }
- })
- },