index.js 4.1 KB

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