|
@@ -0,0 +1,238 @@
|
|
|
+// pages/faceRecognition/index.ts
|
|
|
+import { base } from "../../config/index";
|
|
|
+import { compareVersion } from "../../utils/util";
|
|
|
+let ctx: WechatMiniprogram.CameraContext | undefined = undefined;
|
|
|
+let createVKSession: WechatMiniprogram.VKSession | undefined = undefined;
|
|
|
+const app = getApp<IAppOption>();
|
|
|
+Page({
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 页面的初始数据
|
|
|
+ */
|
|
|
+ data: {
|
|
|
+ isStartVideo: false, // 是否点击开始按钮
|
|
|
+ isEmpower: false, // 是否授权
|
|
|
+ isUpLoad: false, // 是否上传中
|
|
|
+ isTranscribe: false, // 是否录制中
|
|
|
+
|
|
|
+ buttomText: "录制"
|
|
|
+ },
|
|
|
+
|
|
|
+ // 开始初始化录制
|
|
|
+ initCamera() {
|
|
|
+ if (this.data.isStartVideo) return;
|
|
|
+ this.setData({
|
|
|
+ isStartVideo: true
|
|
|
+ })
|
|
|
+ wx.nextTick(() => {
|
|
|
+ this.createVK();
|
|
|
+ wx.showLoading({
|
|
|
+ title: ""
|
|
|
+ })
|
|
|
+ ctx = wx.createCameraContext();
|
|
|
+ ctx.startRecord({
|
|
|
+ timeout: 15, // 录制时长
|
|
|
+ timeoutCallback: () => {
|
|
|
+ // 超出录制时长 关闭录屏
|
|
|
+ this.overVideo();
|
|
|
+ },
|
|
|
+ success: () => {
|
|
|
+ // 调用成功,开始录制
|
|
|
+ wx.hideLoading();
|
|
|
+ },
|
|
|
+ fail: () => {
|
|
|
+ // 调用失败
|
|
|
+ wx.hideLoading();
|
|
|
+ wx.showToast({
|
|
|
+ icon: "none",
|
|
|
+ title: "相机调用失败",
|
|
|
+ duration: 2000
|
|
|
+ })
|
|
|
+ },
|
|
|
+ complete: () => { }
|
|
|
+ })
|
|
|
+ })
|
|
|
+ },
|
|
|
+
|
|
|
+ // 创建人脸追踪
|
|
|
+ createVK() {
|
|
|
+ console.log("开始追踪人脸");
|
|
|
+ createVKSession = wx.createVKSession({
|
|
|
+ version: "v1",
|
|
|
+ track: {
|
|
|
+ plane: {
|
|
|
+ mode: 3
|
|
|
+ },
|
|
|
+ face: {
|
|
|
+ mode: 1
|
|
|
+ }
|
|
|
+ },
|
|
|
+ })
|
|
|
+
|
|
|
+ createVKSession.on("updateAnchors", anchors => {
|
|
|
+ console.log("检测到人脸", anchors)
|
|
|
+ if (!this.data.isTranscribe) {
|
|
|
+ this.setData({
|
|
|
+ isTranscribe: true
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ // 当人脸从相机中离开时,会触发 removeAnchors 事件
|
|
|
+ createVKSession.on('removeAnchors', () => {
|
|
|
+ wx.showToast({
|
|
|
+ icon: "none",
|
|
|
+ title: "未检测到人脸,请重新录制",
|
|
|
+ duration: 2000
|
|
|
+ })
|
|
|
+ // 录制失败
|
|
|
+ this.overVideo();
|
|
|
+ })
|
|
|
+
|
|
|
+ createVKSession.start((errno) => {
|
|
|
+ console.log(errno);
|
|
|
+ })
|
|
|
+ },
|
|
|
+
|
|
|
+ // 结束录像
|
|
|
+ overVideo() {
|
|
|
+ if (!ctx || !this.data.isStartVideo) return;
|
|
|
+ wx.showLoading({
|
|
|
+ title: ""
|
|
|
+ })
|
|
|
+ createVKSession?.stop();
|
|
|
+ ctx.stopRecord({
|
|
|
+ compressed: true,
|
|
|
+ success: res => {
|
|
|
+ // 成功结束
|
|
|
+ wx.hideLoading();
|
|
|
+ this.getUrl(res);
|
|
|
+ },
|
|
|
+ fail: () => {
|
|
|
+ // 结束失败
|
|
|
+ wx.hideLoading();
|
|
|
+ wx.showToast({
|
|
|
+ icon: "none",
|
|
|
+ title: "相机关闭失败",
|
|
|
+ duration: 2000
|
|
|
+ })
|
|
|
+ },
|
|
|
+ complete: () => {
|
|
|
+ this.setData({
|
|
|
+ isStartVideo: false
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+
|
|
|
+ // 获取本地临时地址
|
|
|
+ getUrl(res: WechatMiniprogram.StartRecordTimeoutCallbackResult) {
|
|
|
+ this.setData({
|
|
|
+ isUpLoad: true
|
|
|
+ })
|
|
|
+ wx.showLoading({
|
|
|
+ title: ""
|
|
|
+ })
|
|
|
+ wx.uploadFile({
|
|
|
+ // 仅为示例,非真实的接口地址,视频上传
|
|
|
+ url: base.url + '/v3/upload',
|
|
|
+ filePath: res.tempVideoPath,
|
|
|
+ name: 'file', //服务器定义的Key值
|
|
|
+ formData: {},
|
|
|
+ header: {
|
|
|
+ Authorization: wx.getStorageSync("token")
|
|
|
+ },
|
|
|
+ success: function (res) {
|
|
|
+ console.log(res)
|
|
|
+ wx.hideLoading();
|
|
|
+ wx.showToast({
|
|
|
+ title: "上传成功",
|
|
|
+ duration: 2000,
|
|
|
+ icon: "none"
|
|
|
+ })
|
|
|
+ },
|
|
|
+ fail: function (res) {
|
|
|
+ console.log(res)
|
|
|
+ wx.hideLoading();
|
|
|
+ wx.showToast({
|
|
|
+ title: "上传失败",
|
|
|
+ icon: "none",
|
|
|
+ duration: 2000
|
|
|
+ })
|
|
|
+ },
|
|
|
+ complete: () => {
|
|
|
+ this.setData({
|
|
|
+ isUpLoad: false
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+
|
|
|
+ // 非正常中止
|
|
|
+ improperStop() { },
|
|
|
+
|
|
|
+ // 未授权
|
|
|
+ unauthorized() { },
|
|
|
+
|
|
|
+ // 初始化完成
|
|
|
+ initialization() { },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生命周期函数--监听页面加载
|
|
|
+ */
|
|
|
+ onLoad(opt) {
|
|
|
+ console.log(opt)
|
|
|
+ const SDKVersion: string = app.globalData.SystemInfo?.SDKVersion || "";
|
|
|
+ compareVersion(SDKVersion, '1.9.6');
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生命周期函数--监听页面初次渲染完成
|
|
|
+ */
|
|
|
+ onReady() {
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生命周期函数--监听页面显示
|
|
|
+ */
|
|
|
+ onShow() {
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生命周期函数--监听页面隐藏
|
|
|
+ */
|
|
|
+ onHide() {
|
|
|
+ // 在录制中退出后台页面隐藏,返回上一页,确保重新进入当前页
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生命周期函数--监听页面卸载
|
|
|
+ */
|
|
|
+ onUnload() {
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 页面相关事件处理函数--监听用户下拉动作
|
|
|
+ */
|
|
|
+ onPullDownRefresh() {
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 页面上拉触底事件的处理函数
|
|
|
+ */
|
|
|
+ onReachBottom() {
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用户点击右上角分享
|
|
|
+ */
|
|
|
+ onShareAppMessage() {
|
|
|
+
|
|
|
+ },
|
|
|
+})
|
|
|
+
|
|
|
+export { }
|