123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- // 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
- })
- let time = Date.now();
- wx.nextTick(() => {
- this.createVK();
- wx.showLoading({
- title: ""
- })
- ctx = wx.createCameraContext();
- ctx.startRecord({
- timeout: 15, // 录制时长
- timeoutCallback: () => {
- // 超出录制时长 关闭录屏
- this.overVideo();
- },
- success: () => {
- // 调用成功,开始录制
- },
- fail: () => {
- // 调用失败
- wx.showToast({
- icon: "none",
- title: "相机调用失败"
- })
- },
- complete: () => {
- wx.hideLoading();
- }
- })
- })
- },
- // 创建人脸追踪
- createVK() {
- console.log("开始追踪人脸");
- createVKSession = wx.createVKSession({
- version: "v2",
- track: {
- plane: {
- mode: 3
- },
- face: {
- mode: 1
- }
- },
- })
- createVKSession.on("updateAnchors", anchors => {
- console.log("检测到人脸")
- if (!this.data.isTranscribe) {
- this.setData({
- isTranscribe: true
- })
- }
- })
- // 当人脸从相机中离开时,会触发 removeAnchors 事件
- createVKSession.on('removeAnchors', () => {
- wx.showToast({
- icon: "none",
- title: "未检测到人脸,请重新录制"
- })
- // 录制失败
- 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 => {
- // 成功结束
- this.getUrl(res);
- },
- fail: () => {
- // 结束失败
- wx.showToast({
- icon: "none",
- title: "相机关闭失败"
- })
- },
- complete: () => {
- wx.hideLoading();
- 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.showToast({
- title: "上传成功"
- })
- },
- fail: function (res) {
- console.log(res)
- wx.showToast({
- title: "上传失败"
- })
- },
- complete: () => {
- this.setData({
- isUpLoad: false
- })
- wx.hideLoading();
- }
- })
- },
- // 非正常中止
- improperStop() { },
- // 未授权
- unauthorized() { },
- // 初始化完成
- initialization() { },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad(opt) {
- const SDKVersion: string = app.globalData.SystemInfo?.SDKVersion || "";
- compareVersion(SDKVersion, '1.9.6');
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady() {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow() {
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide() {
- // 在录制中退出后台页面隐藏,返回上一页,确保重新进入当前页
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload() {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh() {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom() {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage() {
- },
- })
|