微信小程式-拍照(人臉識別)
阿新 • • 發佈:2019-01-11
<camera device-position="{{value}}" flash="off" binderror="error" style="width: 90%; height: 300px;"></camera> <view class="weui-cell__ft"> <switch checked bindchange='switchChange' /> </view> <button type="primary" bindtap="takePhoto">拍照</button> <view>預覽</view> <image mode="widthFix" src="{{src}}"></image>
在js裡
Page({ /** * 頁面的初始資料 */ data: { value: '', src: null }, switchChange: function (e) { console.log(e); if (e.detail.value) { this.setData({ value: 'front' }) } else { this.setData({ value: 'back' }) } }, takePhoto() { const ctx = wx.createCameraContext() ctx.takePhoto({ quality: 'high', success: (res) => { this.setData({ src: res.tempImagePath }) wx.showLoading({ title: '正在核驗身份...', }) this.setData({ logindisabled: true }); wx.uploadFile({ // url:app.globalData.url.login, url: 'http://www.anyangjie.top/server/index.php/home/index/login',
filePath: res.tempImagePath, name: 'file', success: (res) => { var data = res.data console.log(res) wx.hideLoading(); this.setData({ logindisabled: false }); // var data = res.data // // console.log(data); wx.showModal({ title: '提示', content: data, showCancel: false }) } }) } }) }, error(e) { console.log(e.detail); },})
在後臺裡寫圖片上傳upload方法
//接受檔案上傳
public function upload($id=''){
$no = M('student')->where("id = '$id'")->getField('no');
$dir = "./Uploads/studentface/";
if(!file_exists($dir)){
mkdir($dir,0777,true);
}
$upload = new \Think\Upload();// 例項化上傳類
$upload->maxSize = 2048000 ;// 設定附件上傳大小
$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 設定附件上傳型別
$upload->rootPath = $dir; // 設定附件上傳根目錄
$upload->savePath = ''; // 設定附件上傳(子)目錄
$upload->saveName = $no;
$upload->replace = true;//覆蓋
$upload->autoSub = false; //阻止建資料夾
$info = $upload->uploadOne($_FILES['file']); // 上傳檔案
if(!$info) {
// 上傳錯誤提示錯誤資訊
return $this->ajaxReturn(array('error'=>true,'msg'=>$upload->getError()));
}else{
// 上傳成功 獲取上傳檔案資訊
// return $this->ajaxReturn(array('error'=>false,'msg'=>$info['savepath'].$info['savename'],'id'=>$id));
$path = $dir.$info['savepath'].$info['savename'];
$file = file_get_contents($path);
$img = base64_encode($file);
$this->facevaild($no,$img);
$m = M('head');
$model = $m->where("no = {$no}")->find();
if($model){
$data['base64'] = $img;
$data['path'] = $path;
$ret = M('head')->where("no = {$no}")->save($data);
}else{
$data['no'] = $no;
$data['base64'] = $img;
$data['path'] = $path;
$ret = M('head')->add($data);
}
echo "資訊採集成功";
}
}