<script>
export default {
name: 'index',
components: {
},
data() {
return {
tipsText: '', // 错误文案提示
tempImg: '', // 本地图片路径
cameraEngine: null, // 相机引擎
devicePosition: false, // 摄像头朝向
isAuthCamera: true, // 是否拥有相机权限
}
},
onLoad(options) {
this.initData()
},
methods: {
// 初始化相机引擎
initData() {
// #ifdef MP-WEIXIN
// 1、初始化人脸识别
wx.initFaceDetect()
// 2、创建 camera 上下文 CameraContext 对象
this.cameraEngine = wx.createCameraContext()
// 3、获取 Camera 实时帧数据
const listener = this.cameraEngine.onCameraFrame((frame) => {
if (this.tempImg) {
return;
}
// 4、人脸识别,使用前需要通过 wx.initFaceDetect 进行一次初始化,推荐使用相机接口返回的帧数据
wx.faceDetect({
frameBuffer: frame.data,
width: frame.width,
height: frame.height,
enablePoint: true,
enableConf: true,
enableAngle: true,
enableMultiFace: true,
success: (faceData) => {
let face = faceData.faceInfo[0]
if (faceData.x == -1 || faceData.y == -1) {
this.tipsText = '检测不到人'
}
if (faceData.faceInfo.length > 1) {
this.tipsText = '请保证只有一个人'
} else {
const {
pitch,
roll,
yaw
} = face.angleArray;
const standard = 0.5
if (Math.abs(pitch) >= standard || Math.abs(roll) >= standard ||
Math.abs(yaw) >= standard) {
this.tipsText = '请平视摄像头'
} else if (face.confArray.global <= 0.8 || face.confArray.leftEye <=
0.8 || face.confArray.mouth <= 0.8 || face.confArray.nose <= 0.8 ||
face.confArray.rightEye <= 0.8) {
this.tipsText = '请勿遮挡五官'
} else {
this.tipsText = '请拍照'
// 这里可以写自己的逻辑了
}
}
},
fail: (err) => {
if (err.x == -1 || err.y == -1) {
this.tipsText = '检测不到人'
} else {
this.tipsText = err.errMsg || '网络错误,请退出页面重试'
}
},
})
})
// 5、开始监听帧数据
listener.start()
// #endif
},
// 切换设备镜头
handleChangeCameraClick() {
this.devicePosition = !this.devicePosition;
},
// 图片上传
handleChooseImage() {
uni.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album'],
success: (res) => {
if (res.errMsg === 'chooseImage:ok') {
uni.showLoading({
title: '照片上传中...'
})
console.log("===========:", res.tempFilePaths[0])
this.handleOkClick()
}
},
fail: (res) => {
},
});
},
// 拍照点击
handleTakePhotoClick() {
if (this.tipsText != "" && this.tipsText != "请拍照") {
return;
}
uni.getSetting({
success: (res) => {
if (!res.authSetting['scope.camera']) {
this.isAuthCamera = false
uni.openSetting({
success: (res) => {
if (res.authSetting['scope.camera']) {
this.isAuthCamera = true;
}
}
})
}
}
})
this.cameraEngine.takePhoto({
quality: "high",
success: ({
tempImagePath
}) => {
this.tempImg = tempImagePath
console.log("=======tempImg:", this.tempImg)
}
})
},
// 点击确定上传
handleOkClick() {
uni.showLoading({
mask: true,
title: '校验中...'
})
// 更新人脸识别图片请求协议:传七牛图片链接
setTimeout(function() {
uni.hideLoading()
uni.showToast({
icon: "none",
title: "假装图片上传成功",
duration: 2000,
})
}, 3000);
},
// 点击 - 取消上传
handleCancelClick() {
this.tempImg = ''
},
// 点击 - 人脸安全保障按钮
handleJumpSecurityClick() {
uni.showToast({
icon: "none",
title: "假装跳转人脸安全保障",
duration: 2000,
})
},
}
}
</script>