|
@@ -1,4 +1,5 @@
|
|
|
-import { getAuthorize, setAuthorize, throttle, checkVersion } from './utils'
|
|
|
+import { getAuthorize, checkVersion } from './utils'
|
|
|
+const app = getApp<IAppOption>();
|
|
|
// 顶部提示信息
|
|
|
// const app = getApp<IAppOption>();
|
|
|
const topTips = {
|
|
@@ -8,9 +9,9 @@ const topTips = {
|
|
|
right: '请向右转头',
|
|
|
}
|
|
|
const bottomTips = {
|
|
|
- recording: '脸部信息采集中...',
|
|
|
- complete: '脸部信息采集成功',
|
|
|
- error: '脸部信息采集失败'
|
|
|
+ recording: '脸部信息录入中...',
|
|
|
+ complete: '脸部信息录入成功',
|
|
|
+ error: '脸部信息录入失败'
|
|
|
}
|
|
|
let ctx: WechatMiniprogram.CameraContext;
|
|
|
let listener: WechatMiniprogram.CameraFrameListener;
|
|
@@ -87,10 +88,12 @@ Component({
|
|
|
|
|
|
// 组件的初始数据
|
|
|
data: {
|
|
|
+ initFace: false,
|
|
|
topTips: topTips.ready, // 顶部提示信息
|
|
|
bottomTips: "", //底部提示信息
|
|
|
gather: 0, // 采集状态:0 -未开始 1 -加载中 2 -录制中 3 -录制结束
|
|
|
- seconds: 0
|
|
|
+ seconds: 0,
|
|
|
+ messageAuto: false
|
|
|
},
|
|
|
|
|
|
/**
|
|
@@ -101,7 +104,7 @@ Component({
|
|
|
readyRecord() {
|
|
|
if (this.data.gather !== 0 && this.data.gather !== 3) return; // 开始采集
|
|
|
this.setData({ bottomTips: bottomTips.recording, gather: 1 }); // 状态转换为加载中
|
|
|
- wx.nextTick(async () => {
|
|
|
+ wx.nextTick(() => {
|
|
|
wx.showLoading({ title: '加载中..', mask: true })
|
|
|
if (!checkVersion('2.18.0', () => this.triggerEvent('cannotUse'))) {
|
|
|
// 基础库不支持 faceDate
|
|
@@ -110,72 +113,97 @@ Component({
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // 调用相机
|
|
|
- await this.openCamera();
|
|
|
- // 准备采集
|
|
|
- startTime = Date.now();
|
|
|
- this.initFace()
|
|
|
- // 开始录制
|
|
|
- this.startRecord()
|
|
|
+ // 获取授权
|
|
|
+ this.getMssage().then(async (r:any) => {
|
|
|
+ this.setData({
|
|
|
+ messageAuto: r || false
|
|
|
+ })
|
|
|
+ // 调用相机
|
|
|
+ await this.openCamera();
|
|
|
+ // 准备采集
|
|
|
+ startTime = Date.now();
|
|
|
+ this.initFace()
|
|
|
+ // 开始录制
|
|
|
+ this.startRecord()
|
|
|
+ })
|
|
|
})
|
|
|
},
|
|
|
- // init 人脸识别能力
|
|
|
- initFace() {
|
|
|
- // 视频帧数据读取节流
|
|
|
- const fn = (frame: any) => {
|
|
|
- const s = 15 - Math.floor((Date.now() - startTime) / 1000);
|
|
|
- if (this.data.gather !== 2 || s % 500 > 100) return;
|
|
|
- let tip = "";
|
|
|
- switch (true) {
|
|
|
- case s > 10:
|
|
|
- tip = topTips.front
|
|
|
- break;
|
|
|
- case s > 5 && s <= 10:
|
|
|
- tip = topTips.left;
|
|
|
- break
|
|
|
- default:
|
|
|
- tip = topTips.right;
|
|
|
- break;
|
|
|
- }
|
|
|
- if (s == 15) innerAudioContext.src = "assets/voice/front.mp3";
|
|
|
- if (s == 10) innerAudioContext.src = "assets/voice/left.mp3";
|
|
|
- if (s == 5) innerAudioContext.src = "assets/voice/right.mp3";
|
|
|
- if (s % 5 === 0 && s !== 0) innerAudioContext.play();
|
|
|
- this.setData({
|
|
|
- seconds: s,
|
|
|
- topTips: tip
|
|
|
- })
|
|
|
- if (s <= 0) {
|
|
|
- // 结束监听
|
|
|
- this.stopRecord(true);// 停止录像逻辑
|
|
|
- this.stopUI(true); // 重置ui逻辑;
|
|
|
- return
|
|
|
- }
|
|
|
- // 识别人脸是否在画面种
|
|
|
- wx.faceDetect({
|
|
|
- frameBuffer: frame.data,
|
|
|
- width: frame.width,
|
|
|
- height: frame.height,
|
|
|
- enableConf: true,
|
|
|
- enableAngle: true,
|
|
|
- success: (res: WechatMiniprogram.FaceDetectSuccessCallbackResult) => this.processFaceData(res),
|
|
|
- fail: (err) => {
|
|
|
- wx.showToast({ title: '未识别到人脸', icon: 'error', duration: 2000 });
|
|
|
- this.setData({
|
|
|
- seconds: 0,
|
|
|
- bottomTips: bottomTips.error
|
|
|
- })
|
|
|
- this.stopRecord();
|
|
|
- this.stopUI();
|
|
|
+ // 获取消息授权
|
|
|
+ getMssage() {
|
|
|
+ return new Promise((resolve) => {
|
|
|
+ let T = wx.getStorageSync("messageAuth") || "";
|
|
|
+ if (T) resolve(T);
|
|
|
+ else wx.requestSubscribeMessage({
|
|
|
+ tmplIds: app.globalData.configPage?.messageID || [],
|
|
|
+ success: (res: WechatMiniprogram.RequestSubscribeMessageSuccessCallbackResult) => {
|
|
|
+ const isAccept = res.p1mpCydIQ6OtxCSa62NaSFiEkQiTsb8KPFaAs1SuKMw === 'accept';
|
|
|
+ wx.setStorageSync("messageAuth", isAccept)
|
|
|
+ resolve(isAccept);
|
|
|
+ },
|
|
|
+ fail: () => {
|
|
|
+ resolve(false);
|
|
|
}
|
|
|
})
|
|
|
- }
|
|
|
+ })
|
|
|
+ },
|
|
|
+ // init 人脸识别能力
|
|
|
+ initFace() {
|
|
|
// 初始化人脸识别
|
|
|
wx.initFaceDetect({
|
|
|
success: () => {
|
|
|
wx.hideLoading();
|
|
|
- listener = ctx.onCameraFrame((frame: any) => fn(frame))
|
|
|
- listener?.start()
|
|
|
+ listener = ctx.onCameraFrame((frame: any) => {
|
|
|
+ const s = 15 - Math.floor((Date.now() - startTime) / 1000);
|
|
|
+ if (this.data.gather !== 2 || s % 500 > 100) return;
|
|
|
+ let tip = "";
|
|
|
+ switch (true) {
|
|
|
+ case s > 10:
|
|
|
+ tip = topTips.front
|
|
|
+ break;
|
|
|
+ case s > 5 && s <= 10:
|
|
|
+ tip = topTips.left;
|
|
|
+ break
|
|
|
+ default:
|
|
|
+ tip = topTips.right;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (s == 15) innerAudioContext.src = "assets/voice/front.mp3";
|
|
|
+ if (s == 10) innerAudioContext.src = "assets/voice/left.mp3";
|
|
|
+ if (s == 5) innerAudioContext.src = "assets/voice/right.mp3";
|
|
|
+ if (s % 5 === 0 && s !== 0) innerAudioContext.play();
|
|
|
+ this.setData({
|
|
|
+ seconds: s,
|
|
|
+ topTips: tip
|
|
|
+ })
|
|
|
+ if (s <= 0) {
|
|
|
+ // 结束监听
|
|
|
+ this.stopRecord(true);// 停止录像逻辑
|
|
|
+ this.stopUI(true); // 重置ui逻辑;
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 识别人脸是否在画面种
|
|
|
+ wx.faceDetect({
|
|
|
+ frameBuffer: frame.data,
|
|
|
+ width: frame.width,
|
|
|
+ height: frame.height,
|
|
|
+ enableConf: true,
|
|
|
+ enableAngle: true,
|
|
|
+ success: (res: WechatMiniprogram.FaceDetectSuccessCallbackResult) => this.processFaceData(res),
|
|
|
+ fail: (err) => {
|
|
|
+ wx.showToast({ title: '未识别到人脸', icon: 'error', duration: 2000 });
|
|
|
+ this.setData({
|
|
|
+ seconds: 0,
|
|
|
+ bottomTips: bottomTips.error
|
|
|
+ })
|
|
|
+ this.stopRecord();
|
|
|
+ this.stopUI();
|
|
|
+ }
|
|
|
+ })
|
|
|
+ })
|
|
|
+ listener?.start();
|
|
|
+ this.setData({
|
|
|
+ initFace: true
|
|
|
+ })
|
|
|
},
|
|
|
fail: () => {
|
|
|
wx.hideLoading();
|
|
@@ -243,7 +271,6 @@ Component({
|
|
|
// 结束录制
|
|
|
stopRecord(isVadeo?: boolean) {
|
|
|
this.setData({ gather: isVadeo ? 3 : 0 });
|
|
|
- console.log('+++++++++>')
|
|
|
ctx?.stopRecord({
|
|
|
compressed: this.properties.compressed,
|
|
|
success: (res) => {
|
|
@@ -254,21 +281,32 @@ Component({
|
|
|
*/
|
|
|
console.log("录制结束", res, isVadeo);
|
|
|
if (!isVadeo) return;
|
|
|
- this.triggerEvent('complete', res.tempVideoPath)
|
|
|
+ this.triggerEvent('complete', { path: res.tempVideoPath, msg: this.data.messageAuto })
|
|
|
+ wx.stopFaceDetect({
|
|
|
+ success: () => {
|
|
|
+ listener?.stop();
|
|
|
+ },
|
|
|
+ fail: () => {
|
|
|
+ listener?.stop();
|
|
|
+ }
|
|
|
+ });
|
|
|
},
|
|
|
fail: (err) => {
|
|
|
console.log("录制失败+++>", err);
|
|
|
wx.hideLoading();
|
|
|
- wx.showToast({ title: "采集失败,请重试", icon: "none" });
|
|
|
+ wx.showToast({ title: "录入失败,请重试", icon: "none" });
|
|
|
this.setData({
|
|
|
seconds: 0,
|
|
|
bottomTips: bottomTips.error
|
|
|
})
|
|
|
- },
|
|
|
- complete: () => {
|
|
|
- console.log("取消录制");
|
|
|
- listener?.stop();
|
|
|
- wx.stopFaceDetect({});
|
|
|
+ wx.stopFaceDetect({
|
|
|
+ success: () => {
|
|
|
+ listener?.stop();
|
|
|
+ },
|
|
|
+ fail: () => {
|
|
|
+ listener?.stop();
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
})
|
|
|
},
|
|
@@ -290,7 +328,7 @@ Component({
|
|
|
} catch (error) {
|
|
|
wx.hideLoading()
|
|
|
wx.showToast({
|
|
|
- title: "设备不支持视频采集",
|
|
|
+ title: "设备不支持视频录入",
|
|
|
icon: "error"
|
|
|
})
|
|
|
this.setData({ gather: 0 });
|