index.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. console.log("开始追踪人脸");
  56. createVKSession = wx.createVKSession({
  57. version: "v2",
  58. track: {
  59. plane: {
  60. mode: 3
  61. },
  62. face: {
  63. mode: 1
  64. }
  65. },
  66. })
  67. createVKSession.on("updateAnchors", anchors => {
  68. console.log("检测到人脸")
  69. if (!this.data.isTranscribe) {
  70. this.setData({
  71. isTranscribe: true
  72. })
  73. }
  74. })
  75. // 当人脸从相机中离开时,会触发 removeAnchors 事件
  76. createVKSession.on('removeAnchors', () => {
  77. wx.showToast({
  78. icon: "none",
  79. title: "未检测到人脸,请重新录制"
  80. })
  81. // 录制失败
  82. this.overVideo();
  83. })
  84. createVKSession.start((errno) => {
  85. console.log(errno);
  86. })
  87. },
  88. // 结束录像
  89. overVideo() {
  90. if (!ctx || !this.data.isStartVideo) return;
  91. wx.showLoading({
  92. title: ""
  93. })
  94. createVKSession?.stop();
  95. ctx.stopRecord({
  96. compressed: true,
  97. success: res => {
  98. // 成功结束
  99. this.getUrl(res);
  100. },
  101. fail: () => {
  102. // 结束失败
  103. wx.showToast({
  104. icon: "none",
  105. title: "相机关闭失败"
  106. })
  107. },
  108. complete: () => {
  109. wx.hideLoading();
  110. this.setData({
  111. isStartVideo: false
  112. })
  113. }
  114. })
  115. },
  116. // 获取本地临时地址
  117. getUrl(res: WechatMiniprogram.StartRecordTimeoutCallbackResult) {
  118. this.setData({
  119. isUpLoad: true
  120. })
  121. wx.showLoading({
  122. title: ""
  123. })
  124. wx.uploadFile({
  125. // 仅为示例,非真实的接口地址,视频上传
  126. url: base.url + '/v3/upload',
  127. filePath: res.tempVideoPath,
  128. name: 'file', //服务器定义的Key值
  129. formData: {},
  130. header: {
  131. Authorization: wx.getStorageSync("token")
  132. },
  133. success: function (res) {
  134. console.log(res)
  135. wx.showToast({
  136. title: "上传成功"
  137. })
  138. },
  139. fail: function (res) {
  140. console.log(res)
  141. wx.showToast({
  142. title: "上传失败"
  143. })
  144. },
  145. complete: () => {
  146. this.setData({
  147. isUpLoad: false
  148. })
  149. wx.hideLoading();
  150. }
  151. })
  152. },
  153. // 非正常中止
  154. improperStop() { },
  155. // 未授权
  156. unauthorized() { },
  157. // 初始化完成
  158. initialization() { },
  159. /**
  160. * 生命周期函数--监听页面加载
  161. */
  162. onLoad(opt) {
  163. const SDKVersion: string = app.globalData.SystemInfo?.SDKVersion || "";
  164. compareVersion(SDKVersion, '1.9.6');
  165. },
  166. /**
  167. * 生命周期函数--监听页面初次渲染完成
  168. */
  169. onReady() {
  170. },
  171. /**
  172. * 生命周期函数--监听页面显示
  173. */
  174. onShow() {
  175. },
  176. /**
  177. * 生命周期函数--监听页面隐藏
  178. */
  179. onHide() {
  180. // 在录制中退出后台页面隐藏,返回上一页,确保重新进入当前页
  181. },
  182. /**
  183. * 生命周期函数--监听页面卸载
  184. */
  185. onUnload() {
  186. },
  187. /**
  188. * 页面相关事件处理函数--监听用户下拉动作
  189. */
  190. onPullDownRefresh() {
  191. },
  192. /**
  193. * 页面上拉触底事件的处理函数
  194. */
  195. onReachBottom() {
  196. },
  197. /**
  198. * 用户点击右上角分享
  199. */
  200. onShareAppMessage() {
  201. },
  202. })