首先,创建Camera目录和page
camera.xml的代码如下
<camera device-position="{{show}}" flash="off" binderror="error" style="width: 100%; height: 400px;"></camera>
<view class='weui-cell__ft'>
<switch checked bindchange='switch1Change'></switch>
</view>
<view class="weui-btn-area">
<button class="weui-btn" type="primary" bindtap="takePhoto">拍照 </button>
</view>
camera.js代码
拍照和录像的方法如下
// 拍照
takePhoto() {
this.ctx.takePhoto({
quality: 'high',
success: (res) => {
this.setData({
src: res.tempImagePath
})
wx.uploadFile({
url: '', //仅为示例,非真实的接口地址
filePath: this.data.src,
name: 'file',
formData: {
},
success: function (res) {
// var data = res.data
// console.log(res.data);
//do something
wx.showModal({
title: '提示',
content: res.data,
})
}
})
}
})
},
//开始录像
startRecord() {
this.ctx.startRecord({
success: (res) => {
console.log('startRecord')
}
})
},
//结束录像
stopRecord() {
this.ctx.stopRecord({
success: (res) => {
this.setData({
src: res.tempThumbPath,
videoSrc: res.tempVideoPath
})
}
})
},
后台的代码如下:
public function upload($id=''){
if(empty($id)){
return false;
}
$no=M('student')->where("id=$id")->getField('no');
// echo $no;
// exit;
$dir="./Uploads/studentface/";//上传文件路径
if(!file_exists($dir)){
mkdir($dir,0777,true);
}
$upload = new ThinkUpload();// 实例化上传类
$upload->maxSize = 2048000;// 设置附件上传大小2m
$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) {// 上传错误提示错误信息
// $this->error($upload->getError());
// echo json_encode(array('error'=>true,'msg'=>$upload->getError()),JSON_UNESCAPED_UNICODE);
$this->ajaxReturn(array('error'=>true,'msg'=>$upload->getError()));
}else{// 上传成功
// $this->success('上传成功!');
$file = $dir.$info['savepath'].$info['savename'];
// echo $file;
$image = base64_encode(file_get_contents($file));
// echo $no;
// echo $image;
$this->facevalid($no,$image);
$m=M('head');
$data=$m->where("no='{$no}'")->find();
// print_r($data);
if($data){
//有数据就更新
$m->where("no='{$no}'")->save(array('base64'=>$image));
}else{
//无数据就添加
$m->add(array('no'=>$no,'base64'=>$image,'path'=>$file));
}
return '采集照片成功';
// return $this->ajaxReturn(array('error'=>false,'msg'=>'上传成功'));
}
这就是主要的代码了,你要没有get到呢?如果对你有帮助给个赞吧!

