FaceDetector.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <template>
  2. <div id="app">
  3. <header></header>
  4. <main class="content">
  5. <Face
  6. v-if="visible"
  7. ref="face"
  8. v-model="resultImage"
  9. :default-image="imageUrl"></Face>
  10. <div class="hidden">
  11. <Face default-image="/face.jpg"></Face>
  12. </div>
  13. </main>
  14. <footer>
  15. <div class="info">
  16. <span v-if="!imageUrl">
  17. 请将人脸移入框内正对摄像头,并保持光线充足。勿佩戴帽子、墨镜
  18. </span>
  19. </div>
  20. <div>
  21. <el-button
  22. size="large"
  23. type="info"
  24. @click="hide()">
  25. 关闭
  26. </el-button>
  27. <el-button
  28. :loading="loading"
  29. size="large"
  30. type="primary"
  31. @click="handleSubmit">
  32. 确定
  33. </el-button>
  34. </div>
  35. </footer>
  36. </div>
  37. </template>
  38. <script>
  39. import { faceidentify11 } from '@/api/face'
  40. import compressImg from '@/assets/compress'
  41. import Face from '@/components/GetFace/FaceDetector'
  42. export default {
  43. data () {
  44. return {
  45. loading: false,
  46. visible: false,
  47. imageUrl: '',
  48. resultImage: null
  49. }
  50. },
  51. mounted () {
  52. const { ipcRenderer, remote } = require('electron')
  53. console.log(remote.getGlobal('windows')['index'])
  54. ipcRenderer.on('showFaceDetector', (event, data, skipCheck) => {
  55. this.visible = true
  56. this.resultImage = null
  57. this.imageUrl = data || ''
  58. this.skipCheck = skipCheck
  59. console.log(data, skipCheck)
  60. this.$nextTick(() => {
  61. this.show()
  62. // 打开摄像头
  63. if (!data) {
  64. this.$refs.face.fnOpen()
  65. }
  66. })
  67. })
  68. },
  69. components: {
  70. Face
  71. },
  72. methods: {
  73. show () {
  74. require('electron').remote.getCurrentWindow().show()
  75. },
  76. hide (data) {
  77. this.visible = false
  78. this.$refs.face && this.$refs.face.fnClose()
  79. data && this.sendResult(data)
  80. require('electron').remote.getCurrentWindow().hide()
  81. },
  82. handleSubmit () {
  83. let msg = ''
  84. if (!this.resultImage) {
  85. this.$message.error('未获取到人脸图像,请重新选择图片或拍照')
  86. return
  87. }
  88. if (this.resultImage.width * this.resultImage.height < 250 * 250) {
  89. msg = '人脸图太小,是否仍然提交?'
  90. }
  91. const submit = async () => {
  92. let validFace = await this.faceidentify11(this.resultImage.url)
  93. if (validFace) {
  94. this.$message.success('人脸校验成功。')
  95. setTimeout(() => {
  96. if (this.resultImage.width * this.resultImage.height > 400 * 400) {
  97. compressImg(this.resultImage.url, 400).then(res => {
  98. this.hide(res)
  99. })
  100. } else {
  101. this.hide(this.resultImage.url)
  102. }
  103. }, 1000)
  104. }
  105. }
  106. if (msg) {
  107. this.$confirm(msg, '提示', {
  108. confirmButtonText: '确定',
  109. cancelButtonText: '取消',
  110. type: 'warning'
  111. }).then(() => {
  112. submit()
  113. }).catch(() => {
  114. })
  115. } else {
  116. submit()
  117. }
  118. },
  119. // 人脸比对API
  120. faceidentify11 (face) {
  121. if (this.skipCheck) return face
  122. this.loading = true
  123. return faceidentify11({ img1: face.split(',')[1], img2: face.split(',')[1] }).then(res => {
  124. if (res.errInfo === '成功') {
  125. return face
  126. } else {
  127. this.$message({
  128. showClose: true,
  129. message: '人脸模糊,请重新拍照',
  130. type: 'error'
  131. })
  132. return false
  133. }
  134. }).catch(() => {
  135. this.$message({
  136. showClose: true,
  137. message: '人脸模糊,请重新拍照',
  138. type: 'error'
  139. })
  140. return false
  141. }).finally(() => {
  142. this.loading = false
  143. })
  144. },
  145. sendResult (data) {
  146. const { ipcRenderer, remote } = require('electron')
  147. ipcRenderer.sendTo(remote.getGlobal('windows')['index'], 'getDectectorResult', data)
  148. }
  149. }
  150. }
  151. </script>
  152. <style lang="scss">
  153. * {
  154. padding: 0; margin: 0;
  155. }
  156. body,html {
  157. height: 100%;
  158. }
  159. #app {
  160. display: flex; height: 100%; flex-direction: column; background: #eee;
  161. }
  162. .content {
  163. display: flex; flex: 1;
  164. }
  165. footer {
  166. padding: 10px; margin: 0 10px 10px; background: #fff; border: 1px solid #ddd; display: flex; justify-content: space-between; align-items: center;
  167. .info{
  168. color: red; font-size: 18px;
  169. }
  170. }
  171. .hidden {
  172. width: 0; height: 0; overflow: hidden;
  173. }
  174. </style>