index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. const _my = require("../../__antmove/api/index.js")(my);
  2. const wx = _my;
  3. const { throttle } = require("./util/throttle");
  4. // components/catchFace/index.js
  5. Component({
  6. options: {
  7. lifetimes: true
  8. },
  9. /**
  10. * 组件的属性列表
  11. */
  12. properties: {
  13. countDown: {
  14. type: Number,
  15. value: 4
  16. },
  17. fake: {
  18. type: Boolean,
  19. value: true
  20. }
  21. },
  22. /**
  23. * 组件的初始数据
  24. */
  25. data: {
  26. tip: "请保持人脸在正中心位置",
  27. src: null,
  28. state: false,
  29. timer: null,
  30. countNumber: null,
  31. interval: null
  32. },
  33. /**
  34. * 组件的方法列表
  35. */
  36. methods: {
  37. // 打开摄像机监听器
  38. async listeningFace() {
  39. const that = this;
  40. await wx.initFaceDetect();
  41. const camera = wx.createCameraContext();
  42. const listener = camera.onCameraFrame(frame => {
  43. wx.faceDetect({
  44. frameBuffer: frame.data,
  45. width: frame.width,
  46. height: frame.height,
  47. enablePoint: true,
  48. enableConf: true,
  49. enableAngle: true,
  50. enableMultiFace: false,
  51. success: faceData => {
  52. that.judgeFace(faceData);
  53. },
  54. fail() {
  55. that.data.state = false;
  56. that.setData({
  57. tip: "未检测到人脸"
  58. });
  59. },
  60. complete() {
  61. that.judgeMove(camera, listener, that.data.countDown);
  62. }
  63. });
  64. });
  65. listener.start();
  66. },
  67. //判断人脸是否符合标准
  68. judgeFace: throttle(function(faceData) {
  69. if (faceData.detectRect.height < 170) {
  70. this.setData({
  71. tip: "请靠近一点"
  72. });
  73. this.data.state = false;
  74. return;
  75. }
  76. if (faceData.detectRect.height > 220) {
  77. this.setData({
  78. tip: "请离远一点"
  79. });
  80. this.data.state = false;
  81. return;
  82. }
  83. if (faceData.confArray.global < 0.8) {
  84. this.setData({
  85. tip: "请勿遮挡脸部"
  86. });
  87. this.data.state = false;
  88. return;
  89. }
  90. if (
  91. faceData.confArray.leftEye < 0.8 ||
  92. faceData.confArray.rightEye < 0.8
  93. ) {
  94. this.setData({
  95. tip: "请勿遮挡眼睛"
  96. });
  97. this.data.state = false;
  98. return;
  99. }
  100. if (
  101. Math.abs(faceData.angleArray.pitch) > 0.2 ||
  102. Math.abs(faceData.angleArray.yaw) > 0.2 ||
  103. Math.abs(faceData.angleArray.roll) > 0.1
  104. ) {
  105. this.setData({
  106. tip: "请正视屏幕"
  107. });
  108. this.data.state = false;
  109. return;
  110. }
  111. this.setData({
  112. tip: "请保持不动,正在抓拍中"
  113. });
  114. this.data.state = true;
  115. return;
  116. }, 500),
  117. //判断人脸是否相对静止,防止拍的照片模糊
  118. judgeMove(camera, listener, duration) {
  119. if (this.data.state === true) {
  120. if (this.data.timer === null) {
  121. this.countDown(duration);
  122. this.data.timer = setTimeout(() => {
  123. this.doTakePhoto(camera, listener);
  124. }, duration * 1000);
  125. }
  126. } else {
  127. clearTimeout(this.data.timer);
  128. clearInterval(this.data.interval);
  129. this.data.timer = null;
  130. this.data.interval = null;
  131. this.setData({
  132. countNumber: null
  133. });
  134. }
  135. },
  136. //抓拍倒计时
  137. countDown(count) {
  138. this.data.interval = setInterval(() => {
  139. if (count <= 0) {
  140. clearInterval(this.data.interval);
  141. this.data.interval = null;
  142. this.setData({
  143. countNumber: null
  144. });
  145. } else {
  146. count--;
  147. this.setData({
  148. countNumber: count
  149. });
  150. }
  151. }, 1000);
  152. },
  153. //抓拍照片
  154. doTakePhoto(camera, listener) {
  155. console.log("okkk");
  156. const that = this;
  157. camera.takePhoto({
  158. success(photo) {
  159. that.setData({
  160. src: photo.tempImagePath,
  161. tip: "抓拍成功",
  162. countNumber: null
  163. });
  164. listener.stop();
  165. that.triggerEvent("getPhoto", photo.tempImagePath);
  166. },
  167. fail(err) {
  168. console.log(err);
  169. }
  170. });
  171. }
  172. },
  173. //钩子
  174. lifetimes: {
  175. attached: function() {
  176. this.listeningFace();
  177. },
  178. detached: function() {}
  179. }
  180. });