FaceRecognition.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <template>
  2. <div>
  3. <video
  4. id="video"
  5. ref="video"
  6. :width="width"
  7. :height="height"
  8. autoplay
  9. >
  10. </video>
  11. <div
  12. v-if="showRecognizeButton"
  13. style="position: absolute; width: 400px; text-align: center; margin-top: -50px; color: #232323;"
  14. >
  15. <button
  16. :class="['verify-button', !disableSnap && 'disable-verify-button']"
  17. @click="snap"
  18. :disabled="disableSnap"
  19. >{{msg}}</button>
  20. </div>
  21. </div>
  22. </template>
  23. <script>
  24. import { mapState as globalMapState } from "vuex";
  25. import { createNamespacedHelpers } from "vuex";
  26. const { mapState, mapMutations } = createNamespacedHelpers("examingHomeModule");
  27. export default {
  28. name: "FaceRecognition",
  29. data() {
  30. return { disableSnap: true, msg: "开始识别" };
  31. },
  32. props: {
  33. width: String,
  34. height: String,
  35. showRecognizeButton: Boolean,
  36. closeCamera: Boolean // optional
  37. },
  38. async mounted() {
  39. this.openCamera();
  40. },
  41. watch: {
  42. snapNow(val) {
  43. if (val) {
  44. if (!this.lastSnapTime || Date.now() - this.lastSnapTime > 60 * 1000) {
  45. this.lastSnapTime = Date.now();
  46. this.snapTimer();
  47. } else {
  48. this.serverLog(
  49. "debug/S-002001",
  50. "上次的抓拍未超过1分钟,本次抓拍指令取消"
  51. );
  52. this.decreaseSnapCount();
  53. }
  54. this.toggleSnapNow();
  55. }
  56. },
  57. closeCamera: function(newValue) {
  58. if (newValue) {
  59. console.log("关闭摄像头");
  60. this.$refs.video.srcObject.getTracks().forEach(function(track) {
  61. track.stop();
  62. });
  63. } else {
  64. this.openCamera();
  65. }
  66. }
  67. },
  68. beforeDestroy() {
  69. this.$refs.video.srcObject.getTracks().forEach(function(track) {
  70. track.stop();
  71. });
  72. },
  73. methods: {
  74. ...mapMutations(["toggleSnapNow", "decreaseSnapCount"]),
  75. async openCamera() {
  76. const video = this.$refs.video;
  77. if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
  78. try {
  79. console.log("启动摄像头");
  80. const stream = await navigator.mediaDevices.getUserMedia({
  81. video: {
  82. facingMode: "user"
  83. // width: 400,
  84. // height: this.showRecognizeButton ? 300 : 250
  85. }
  86. });
  87. if (stream) {
  88. video.srcObject = stream;
  89. try {
  90. await video.play();
  91. this.disableSnap = false;
  92. } catch (error) {
  93. this.$Message.error({
  94. content: "摄像头没有正常启用",
  95. duration: 5
  96. });
  97. }
  98. } else {
  99. this.$Message.error("没有可用的视频流");
  100. window._hmt.push([
  101. "_trackEvent",
  102. "摄像头框",
  103. "摄像头状态",
  104. "没有可用的视频流"
  105. ]);
  106. }
  107. } catch (error) {
  108. this.$Message.error("无法启用摄像头");
  109. window._hmt.push([
  110. "_trackEvent",
  111. "摄像头框",
  112. "摄像头状态",
  113. "无法启用摄像头"
  114. ]);
  115. }
  116. } else {
  117. this.$Message.error("没有找到可用的摄像头");
  118. window._hmt.push([
  119. "_trackEvent",
  120. "摄像头框",
  121. "摄像头状态",
  122. "没有找到可用的摄像头"
  123. ]);
  124. }
  125. },
  126. async snapTimer() {
  127. try {
  128. const examRecordDataId = this.$route.params.examRecordDataId;
  129. const captureBlob = await this.getSnapShot();
  130. this.videoStartPlay();
  131. // console.log(captureBlob.size);
  132. // this.serverLog("debug/S-004001", "抓拍照片的大小:" + captureBlob.size);
  133. const captureFilePath = await this.uploadToServer(captureBlob);
  134. await this.faceCompare(captureFilePath, examRecordDataId);
  135. } catch (error) {
  136. // FIXME: more processing
  137. console.log("定时抓拍流程失败");
  138. } finally {
  139. this.videoStartPlay();
  140. this.decreaseSnapCount();
  141. }
  142. },
  143. videoStartPlay() {
  144. const video = this.$refs.video;
  145. video && video.play();
  146. },
  147. async snap() {
  148. // TODO: chrome 70. FaceDetector检测人脸
  149. // var canvas = document.createElement("canvas");
  150. // canvas.width = 220;
  151. // canvas.height = 165;
  152. // var context = canvas.getContext("2d");
  153. // context.drawImage(this.$refs.video, 0, 0, 220, 165);
  154. // var f = new FaceDetector();
  155. // const v = await f.detect(canvas);
  156. // console.log(v);
  157. // return;
  158. this.$Message.destroy();
  159. try {
  160. this.disableSnap = true;
  161. // console.log("disableSnap: " + this.disableSnap);
  162. // await new Promise(resolve =>
  163. // setTimeout(() => {
  164. // console.log(new Date());
  165. // resolve();
  166. // }, 3000)
  167. // );
  168. // return;
  169. // if(this.disableSnap) return; // 避免界面没有更新。
  170. this.msg = "拍照中...";
  171. const captureBlob = await this.getSnapShot();
  172. this.videoStartPlay();
  173. this.msg = "上传照片中...";
  174. const captureFilePath = await this.uploadToServer(captureBlob);
  175. this.msg = "人脸比对中...";
  176. await this.faceCompareSync(captureFilePath);
  177. } catch (error) {
  178. // FIXME: more processing
  179. console.log("同步照片比对流程失败");
  180. throw error;
  181. } finally {
  182. this.videoStartPlay();
  183. this.msg = "开始识别";
  184. this.disableSnap = false;
  185. }
  186. },
  187. async getSnapShot() {
  188. return new Promise((resolve, reject) => {
  189. const video = this.$refs.video;
  190. if (video.readyState !== 4 || !video.srcObject.active) {
  191. this.$Message.error({ content: "摄像头没有正常启用", duration: 5 });
  192. window._hmt.push([
  193. "_trackEvent",
  194. "摄像头框",
  195. "摄像头状态",
  196. "摄像头没有正常启用-退出" +
  197. (this.lastSnapTime ? "(非初次抓拍)" : "")
  198. ]);
  199. reject("摄像头没有正常启用");
  200. this.logout();
  201. return;
  202. }
  203. video.pause();
  204. var canvas = document.createElement("canvas");
  205. canvas.width = 220;
  206. canvas.height = 165;
  207. var context = canvas.getContext("2d");
  208. context.drawImage(video, 0, 0, 220, 165);
  209. canvas.toBlob(resolve, "image/png", 0.95);
  210. });
  211. },
  212. async uploadToServer(captureBlob) {
  213. //保存抓拍照片到服务器
  214. // var fileName = new Date().getTime() + ".png";
  215. // var fileUrl = "/api/exchange/inner/upyun/put/capturePhoto/" + fileName;
  216. var fileUrl = "/api/exchange/inner/upyun/put/capturePhoto/png";
  217. let resultUrl;
  218. try {
  219. const res = await this.$http.put(fileUrl, captureBlob, {
  220. headers: {
  221. "Content-Type": "image/png"
  222. }
  223. });
  224. resultUrl = res.data;
  225. this.serverLog("debug/S-005001", "抓拍照片保存成功:" + resultUrl);
  226. } catch (e) {
  227. console.log(e);
  228. this.serverLog("debug/S-006001", "抓拍照片保存失败");
  229. this.$Message.error("保存抓拍照片到服务器失败!");
  230. throw "保存抓拍照片到服务器失败!";
  231. }
  232. // let UPYUN_URL;
  233. // try {
  234. // UPYUN_URL = (await this.$http.get("/api/ecs_oe_student_face/upyun"))
  235. // .data.downloadPrefix;
  236. // } catch (error) {
  237. // this.$Message.error("获取照片下载前缀失败!");
  238. // throw "获取照片下载前缀失败!";
  239. // }
  240. return resultUrl;
  241. },
  242. async faceCompareSync(captureFilePath) {
  243. try {
  244. const res = await this.$http.post(
  245. "/api/ecs_oe_student_face/examCaptureQueue/compareFaceSync?fileUrl=" +
  246. captureFilePath
  247. );
  248. // TODO: 识别成功、失败的通知或跳转
  249. this.$emit("on-recognize-result", {
  250. error: null,
  251. pass: res.data.isPass,
  252. stranger: res.data.isStranger
  253. });
  254. } catch (e) {
  255. console.log(e);
  256. // this.$Message.error(e.message);
  257. throw "同步照片比较失败!";
  258. }
  259. },
  260. async faceCompare(captureFilePath, examRecordDataId) {
  261. try {
  262. const res = await this.$http.post(
  263. "/api/ecs_oe_student_face/examCaptureQueue/uploadExamCapture?fileUrl=" +
  264. captureFilePath +
  265. "&examRecordDataId=" +
  266. examRecordDataId
  267. );
  268. const fileName = res.data;
  269. try {
  270. await this.showSnapResult(fileName, examRecordDataId);
  271. } catch (error) {
  272. this.$Message.error("设置获取抓拍结果失败!");
  273. }
  274. } catch (e) {
  275. console.log(e);
  276. this.$Message.error(e.message);
  277. throw "异步比较抓拍照片失败";
  278. }
  279. },
  280. async showSnapResult(fileName, examRecordDataId) {
  281. if (!fileName) return; // 交卷后提交照片会得不到照片名称
  282. try {
  283. // 获取抓拍结果
  284. const snapRes =
  285. (await this.$http.get(
  286. "/api/ecs_oe_student_face/examCaptureQueue/getExamCaptureResult?fileName=" +
  287. fileName +
  288. "&examRecordDataId=" +
  289. examRecordDataId
  290. )).data || {};
  291. if (this.$route.name !== "OnlineExamingHome") {
  292. // 非考试页,不显示结果,也不继续查询
  293. return;
  294. }
  295. if (snapRes.isCompleted) {
  296. if (snapRes.isStranger) {
  297. this.$Message.error({
  298. content: "请独立完成考试",
  299. duration: 5,
  300. closable: true
  301. });
  302. } else if (!snapRes.isPass) {
  303. this.$Message.error({
  304. content: "请调整坐姿,诚信考试",
  305. duration: 5,
  306. closable: true
  307. });
  308. }
  309. } else {
  310. setTimeout(
  311. this.showSnapResult.bind(this, fileName, examRecordDataId),
  312. 30 * 1000
  313. );
  314. }
  315. } catch (e) {
  316. console.log(e);
  317. if (this.$route.name !== "OnlineExamingHome") {
  318. // 非考试页,不显示结果,也不继续查询
  319. return;
  320. }
  321. this.$Message.error(e.message);
  322. throw e.message;
  323. }
  324. }
  325. },
  326. computed: {
  327. ...globalMapState(["user"]),
  328. ...mapState(["snapNow"])
  329. }
  330. };
  331. </script>
  332. <style scoped>
  333. .verify-button {
  334. font-size: 16px;
  335. background-color: #ffcc00;
  336. display: inline-block;
  337. padding: 6px 16px;
  338. border-radius: 6px;
  339. }
  340. .verify-button:hover {
  341. color: #444444;
  342. cursor: pointer;
  343. }
  344. .disable-verify-button {
  345. background-color: #f7f7f7;
  346. color: #c5c8ce;
  347. }
  348. .disable-verify-button:hover {
  349. cursor: not-allowed;
  350. color: #c5c8ce;
  351. }
  352. </style>