index.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // pages/faceRecognition/index.ts
  2. import { base } from "../../config/index";
  3. import { compareVersion } from "../../utils/util";
  4. let ctx: WechatMiniprogram.CameraContext | undefined = undefined;
  5. let createVKSession: WechatMiniprogram.VKSession | undefined = undefined;
  6. const app = getApp<IAppOption>();
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. isStartVideo: false, // 是否点击开始按钮
  13. isEmpower: false, // 是否授权
  14. isUpLoad: false, // 是否上传中
  15. isTranscribe: false, // 是否录制中
  16. buttomText: "录制"
  17. },
  18. // 开始初始化录制
  19. initCamera() {
  20. if (this.data.isStartVideo) return;
  21. this.setData({
  22. isStartVideo: true
  23. })
  24. let time = Date.now();
  25. wx.nextTick(() => {
  26. this.createVK();
  27. wx.showLoading({
  28. title: ""
  29. })
  30. ctx = wx.createCameraContext();
  31. ctx.startRecord({
  32. timeout: 15, // 录制时长
  33. timeoutCallback: () => {
  34. // 超出录制时长 关闭录屏
  35. this.overVideo();
  36. },
  37. success: () => {
  38. // 调用成功,开始录制
  39. },
  40. fail: () => {
  41. // 调用失败
  42. wx.showToast({
  43. icon: "none",
  44. title: "相机调用失败"
  45. })
  46. },
  47. complete: () => {
  48. wx.hideLoading();
  49. }
  50. })
  51. })
  52. },
  53. // 创建人脸追踪
  54. createVK() {
  55. createVKSession = wx.createVKSession({
  56. version: "v2",
  57. track: {
  58. plane: {
  59. mode: 3
  60. },
  61. face: {
  62. mode: 1
  63. }
  64. },
  65. })
  66. createVKSession.on("updateAnchors", anchors => {
  67. console.log("检测到人脸")
  68. if (!this.data.isTranscribe) {
  69. this.setData({
  70. isTranscribe: true
  71. })
  72. }
  73. })
  74. // 当人脸从相机中离开时,会触发 removeAnchors 事件
  75. createVKSession.on('removeAnchors', () => {
  76. console.log('未检测到人脸');
  77. // 录制失败
  78. this.overVideo();
  79. })
  80. },
  81. // 结束录像
  82. overVideo() {
  83. if (!ctx || !this.data.isStartVideo) return;
  84. wx.showLoading({
  85. title: ""
  86. })
  87. ctx.stopRecord({
  88. compressed: true,
  89. success: res => {
  90. // 成功结束
  91. this.getUrl(res);
  92. },
  93. fail: () => {
  94. // 结束失败
  95. wx.showToast({
  96. icon: "none",
  97. title: "相机关闭失败"
  98. })
  99. },
  100. complete: () => {
  101. wx.hideLoading();
  102. this.setData({
  103. isStartVideo: false
  104. })
  105. }
  106. })
  107. },
  108. // 获取本地临时地址
  109. getUrl(res: WechatMiniprogram.StartRecordTimeoutCallbackResult) {
  110. this.setData({
  111. isUpLoad: true
  112. })
  113. wx.showLoading({
  114. title: ""
  115. })
  116. wx.uploadFile({
  117. // 仅为示例,非真实的接口地址,视频上传
  118. url: base.url + '/v3/upload',
  119. filePath: res.tempVideoPath,
  120. name: 'file', //服务器定义的Key值
  121. formData: {},
  122. header: {
  123. Authorization: wx.getStorageSync("token")
  124. },
  125. success: function (res) {
  126. console.log(res)
  127. wx.showToast({
  128. title: "上传成功"
  129. })
  130. },
  131. fail: function (res) {
  132. console.log(res)
  133. wx.showToast({
  134. title: "上传失败"
  135. })
  136. },
  137. complete: () => {
  138. this.setData({
  139. isUpLoad: false
  140. })
  141. wx.hideLoading();
  142. }
  143. })
  144. },
  145. // 非正常中止
  146. improperStop() { },
  147. // 未授权
  148. unauthorized() { },
  149. // 初始化完成
  150. initialization() { },
  151. /**
  152. * 生命周期函数--监听页面加载
  153. */
  154. onLoad(opt) {
  155. const SDKVersion: string = app.globalData.SystemInfo?.SDKVersion || "";
  156. compareVersion(SDKVersion, '1.9.6');
  157. },
  158. /**
  159. * 生命周期函数--监听页面初次渲染完成
  160. */
  161. onReady() {
  162. },
  163. /**
  164. * 生命周期函数--监听页面显示
  165. */
  166. onShow() {
  167. },
  168. /**
  169. * 生命周期函数--监听页面隐藏
  170. */
  171. onHide() {
  172. // 在录制中退出后台页面隐藏,返回上一页,确保重新进入当前页
  173. },
  174. /**
  175. * 生命周期函数--监听页面卸载
  176. */
  177. onUnload() {
  178. },
  179. /**
  180. * 页面相关事件处理函数--监听用户下拉动作
  181. */
  182. onPullDownRefresh() {
  183. },
  184. /**
  185. * 页面上拉触底事件的处理函数
  186. */
  187. onReachBottom() {
  188. },
  189. /**
  190. * 用户点击右上角分享
  191. */
  192. onShareAppMessage() {
  193. },
  194. })