index.ts 4.8 KB

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