index.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <template>
  2. <div>
  3. <div>
  4. <div v-show="showContainer">
  5. <video id="video" ref="refVideo" width="300" height="200" preload autoPlay loop muted />
  6. <canvas id="canvas" ref="refCanvas" width="300" height="200" />
  7. </div>
  8. <div>
  9. <img v-show="!showContainer" :src="imgUrl" width="300" height="200">
  10. </div>
  11. </div>
  12. <div class="scanTip">
  13. <div v-if="faceInfo.faceToken==null || faceInfo.faceToken===''">
  14. {{ scanTip }}
  15. </div>
  16. <div v-else style="color: #1AAC1A; text-align: left">
  17. <div>姓名:{{ faceInfo.realName }}</div>
  18. <div>身份证:{{ faceInfo.idCard }}</div>
  19. <div>识别成功,请继续下一步操作</div>
  20. </div>
  21. </div>
  22. </div>
  23. </template>
  24. <script>
  25. // 引入tracking
  26. import 'tracking/build/tracking-min.js'
  27. import 'tracking/build/data/face-min.js'
  28. import { faceCheck } from '@/api/user'
  29. export default {
  30. name: 'FaceCam',
  31. data() {
  32. return {
  33. showContainer: true,
  34. tracker: null,
  35. tipFlag: false, // 提示用户已经检测到
  36. flag: false, // 判断是否已经拍照
  37. context: null, // canvas上下文
  38. removePhotoID: null, // 停止转换图片
  39. scanTip: '请将人脸置于摄像头中央...', // 提示文字
  40. imgUrl: '',
  41. canvas: null,
  42. video: null,
  43. streamIns: null, // 视频流
  44. // 人脸校验结果
  45. faceInfo: {
  46. id: ''
  47. }
  48. }
  49. },
  50. mounted() {
  51. this.playVideo()
  52. },
  53. methods: {
  54. // 访问用户媒体设备
  55. getUserMedia(constrains, success, error) {
  56. if (navigator.mediaDevices.getUserMedia) {
  57. // 最新标准API
  58. navigator.mediaDevices.getUserMedia(constrains).then(success).catch(error)
  59. } else if (navigator.webkitGetUserMedia) {
  60. // webkit内核浏览器
  61. navigator.webkitGetUserMedia(constrains).then(success).catch(error)
  62. } else if (navigator.mozGetUserMedia) {
  63. // Firefox浏览器
  64. navigator.mozGetUserMedia(constrains).then(success).catch(error)
  65. } else if (navigator.getUserMedia) {
  66. // 旧版API
  67. navigator.getUserMedia(constrains).then(success).catch(error)
  68. } else {
  69. this.scanTip = '你的浏览器不支持访问用户媒体设备'
  70. }
  71. },
  72. success(stream) {
  73. this.streamIns = stream
  74. // webkit内核浏览器
  75. this.URL = window.URL || window.webkitURL
  76. if ('srcObject' in this.$refs.refVideo) {
  77. this.$refs.refVideo.srcObject = stream
  78. } else {
  79. this.$refs.refVideo.src = this.URL.createObjectURL(stream)
  80. }
  81. this.$refs.refVideo.onloadedmetadata = e => {
  82. this.$refs.refVideo.play()
  83. }
  84. },
  85. error(e) {
  86. this.scanTip = '摄像头开启失败,请检查您的摄像头是否正常:' + e.name + ',' + e.message
  87. },
  88. playVideo() {
  89. this.getUserMedia({
  90. video: {
  91. width: 300, height: 200, facingMode: 'user'
  92. } /* 前置优先 */
  93. }, this.success, this.error)
  94. this.video = document.getElementById('video')
  95. this.canvas = document.getElementById('canvas')
  96. this.context = this.canvas.getContext('2d')
  97. // eslint-disable-next-line no-undef
  98. this.tracker = new tracking.ObjectTracker('face')
  99. this.tracker.setInitialScale(4)
  100. this.tracker.setStepSize(2)
  101. this.tracker.setEdgesDensity(0.1)
  102. // eslint-disable-next-line no-undef
  103. tracking.track('#video', this.tracker, { camera: true })
  104. this.tracker.on('track', this.handleTracked)
  105. },
  106. handleTracked(event) {
  107. this.context.clearRect(0, 0, this.canvas.width, this.canvas.height)
  108. if (event.data.length === 0) {
  109. this.scanTip = '未识别到人脸,请将人脸置于摄像头中央!'
  110. } else {
  111. if (!this.tipFlag) {
  112. this.scanTip = '识别成功,请保持这个姿势!'
  113. }
  114. // 1秒后拍照,仅拍一次
  115. if (!this.flag) {
  116. this.scanTip = '正在拍照对比...'
  117. this.flag = true
  118. this.removePhotoID = setTimeout(() => {
  119. this.tackPhoto()
  120. this.tipFlag = true
  121. },
  122. 1000
  123. )
  124. }
  125. event.data.forEach(this.plot)
  126. }
  127. },
  128. plot(rect) {
  129. this.context.strokeStyle = '#eb652e'
  130. this.context.strokeRect(rect.x, rect.y, rect.width, rect.height)
  131. this.context.font = '11px Helvetica'
  132. this.context.fillStyle = '#fff'
  133. this.context.fillText('x: ' + rect.x + 'px', rect.x + rect.width + 5, rect.y + 11)
  134. this.context.fillText('y: ' + rect.y + 'px', rect.x + rect.width + 5, rect.y + 22)
  135. },
  136. // 拍照
  137. tackPhoto() {
  138. this.context.drawImage(this.$refs.refVideo, 0, 0, 300, 200)
  139. // 保存为base64格式
  140. this.imgUrl = this.saveAsPNG(this.$refs.refCanvas)
  141. this.scanTip = '比对中,请稍等..'
  142. // 去除png文件头
  143. const base64 = this.imgUrl.substring(22, this.imgUrl.length)
  144. faceCheck({ base64: base64 }).then(res => {
  145. console.log('识别结果', res)
  146. this.faceInfo = res.data
  147. if (this.faceInfo.faceToken != null && this.faceInfo.faceToken !== '') {
  148. this.$message.success('人脸比对成功!')
  149. this.$emit('success', res.data)
  150. this.showContainer = false
  151. } else {
  152. this.$emit('fail')
  153. this.flag = false
  154. this.tipFlag = false
  155. this.showContainer = true
  156. }
  157. }).catch(() => {
  158. this.$emit('fail')
  159. this.flag = false
  160. this.tipFlag = false
  161. this.showContainer = true
  162. })
  163. },
  164. // 保存为png,base64格式图片
  165. saveAsPNG(c) {
  166. return c.toDataURL('image/png', 0.3)
  167. },
  168. // 关闭并清理资源
  169. close() {
  170. this.video.srcObject.getTracks()[0].stop()
  171. this.flag = false
  172. this.tipFlag = false
  173. this.showContainer = false
  174. // eslint-disable-next-line no-undef
  175. this.tracker && this.tracker.removeListener('track', this.handleTracked) && tracking.track('#video', this.tracker, { camera: false })
  176. this.tracker = null
  177. this.context = null
  178. this.scanTip = ''
  179. clearTimeout(this.removePhotoID)
  180. if (this.streamIns) {
  181. this.streamIns.enabled = false
  182. this.streamIns.getTracks()[0].stop()
  183. this.streamIns.getVideoTracks()[0].stop()
  184. }
  185. this.streamIns = null
  186. }
  187. }
  188. }
  189. </script>
  190. <style scoped>
  191. .form-box {
  192. font-size: 14px;
  193. display: flex;
  194. flex-wrap: wrap;
  195. align-items: center;
  196. flex-direction: column;
  197. justify-content: center;
  198. line-height: 42px;
  199. }
  200. video, canvas, img {
  201. position: absolute;
  202. left: 10px;
  203. top: 10px;
  204. }
  205. /deep/
  206. .scanTip {
  207. position: absolute;
  208. text-align: center;
  209. top: 220px;
  210. width: 300px;
  211. left: 10px;
  212. font-size: 14px;
  213. font-weight: 700;
  214. line-height: 26px;
  215. }
  216. </style>