FaceRecognition.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. <template>
  2. <div>
  3. <video
  4. id="video"
  5. ref="video"
  6. :width="width"
  7. :height="height"
  8. autoplay
  9. ></video>
  10. <div
  11. v-if="showRecognizeButton"
  12. style="position: absolute; width: 400px; text-align: center; margin-top: -50px; color: #232323;"
  13. >
  14. <button
  15. :class="['verify-button', disableSnap && 'disable-verify-button']"
  16. :disabled="disableSnap"
  17. @click="snap"
  18. >
  19. {{ msg }}
  20. </button>
  21. </div>
  22. </div>
  23. </template>
  24. <script>
  25. import MD5 from "js-md5";
  26. import { mapState as globalMapState } from "vuex";
  27. import { createNamespacedHelpers } from "vuex";
  28. const { mapState, mapMutations } = createNamespacedHelpers("examingHomeModule");
  29. export default {
  30. name: "FaceRecognition",
  31. props: {
  32. width: { type: String, default: "400" },
  33. height: { type: String, default: "300" },
  34. showRecognizeButton: Boolean,
  35. closeCamera: Boolean, // optional
  36. },
  37. data() {
  38. return { disableSnap: true, msg: "开始识别" };
  39. },
  40. computed: {
  41. ...globalMapState(["user"]),
  42. ...mapState(["snapNow"]),
  43. },
  44. watch: {
  45. snapNow(val) {
  46. if (val) {
  47. if (!this.lastSnapTime || Date.now() - this.lastSnapTime > 60 * 1000) {
  48. this.lastSnapTime = Date.now();
  49. this.snapTimer();
  50. } else {
  51. // this.serverLog(
  52. // "debug/S-002001",
  53. // "上次的抓拍未超过1分钟,本次抓拍指令取消"
  54. // );
  55. window._hmt.push([
  56. "_trackEvent",
  57. "摄像头框",
  58. "上次的抓拍未超过1分钟,本次抓拍指令取消",
  59. ]);
  60. this.decreaseSnapCount();
  61. }
  62. this.toggleSnapNow();
  63. }
  64. },
  65. closeCamera: function(newValue) {
  66. if (newValue) {
  67. console.log("关闭摄像头");
  68. if (this.$refs.video.srcObject) {
  69. this.$refs.video.srcObject.getTracks().forEach(function(track) {
  70. track.stop();
  71. });
  72. this.$refs.video.srcObject.srcObject = null;
  73. }
  74. } else {
  75. this.openCamera();
  76. }
  77. },
  78. },
  79. async mounted() {
  80. this.openCamera();
  81. },
  82. beforeDestroy() {
  83. clearTimeout(this.retrySnapTimeout);
  84. clearTimeout(this.showSnapResultTimeout);
  85. if (this.$refs.video.srcObject) {
  86. this.$refs.video.srcObject.getTracks().forEach(function(track) {
  87. track.stop();
  88. });
  89. this.$refs.video.srcObject.srcObject = null;
  90. }
  91. },
  92. methods: {
  93. ...mapMutations(["toggleSnapNow", "decreaseSnapCount"]),
  94. async openCamera() {
  95. const video = this.$refs.video;
  96. if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
  97. try {
  98. console.log("启动摄像头");
  99. const stream = await navigator.mediaDevices.getUserMedia({
  100. video: {
  101. facingMode: "user",
  102. // width: 400,
  103. // height: this.showRecognizeButton ? 300 : 250
  104. },
  105. });
  106. if (stream) {
  107. video.srcObject = stream;
  108. try {
  109. await video.play();
  110. this.disableSnap = false;
  111. } catch (error) {
  112. console.log("摄像头没有正常启用", error);
  113. this.$Message.error({
  114. content: "摄像头没有正常启用",
  115. duration: 15,
  116. closable: true,
  117. });
  118. }
  119. } else {
  120. this.$Message.error({
  121. content: "没有可用的视频流",
  122. duration: 15,
  123. closable: true,
  124. });
  125. window._hmt.push([
  126. "_trackEvent",
  127. "摄像头框",
  128. "摄像头状态",
  129. "没有可用的视频流",
  130. ]);
  131. }
  132. } catch (error) {
  133. console.log("无法启用摄像头", error);
  134. this.$Message.error({
  135. content: "无法启用摄像头",
  136. duration: 15,
  137. closable: true,
  138. });
  139. window._hmt.push([
  140. "_trackEvent",
  141. "摄像头框",
  142. "摄像头状态",
  143. "无法启用摄像头",
  144. ]);
  145. }
  146. } else {
  147. this.$Message.error({
  148. content: "没有找到可用的摄像头",
  149. duration: 15,
  150. closable: true,
  151. });
  152. window._hmt.push([
  153. "_trackEvent",
  154. "摄像头框",
  155. "摄像头状态",
  156. "没有找到可用的摄像头",
  157. ]);
  158. }
  159. },
  160. async snapTimer() {
  161. try {
  162. const examRecordDataId = this.$route.params.examRecordDataId;
  163. const captureBlob = await this.getSnapShot();
  164. this.videoStartPlay();
  165. console.log("抓拍照片的大小:" + captureBlob.size);
  166. // this.serverLog("debug/S-004001", "抓拍照片的大小:" + captureBlob.size);
  167. const [captureFilePath, signIdentifier] = await this.uploadToServer(
  168. captureBlob
  169. );
  170. await this.faceCompare(
  171. captureFilePath,
  172. signIdentifier,
  173. examRecordDataId
  174. );
  175. } catch (error) {
  176. // FIXME: more processing
  177. console.log("定时抓拍流程失败");
  178. window._hmt.push([
  179. "_trackEvent",
  180. "摄像头框",
  181. "定时抓拍流程失败",
  182. (this.lastSnapTime ? "(非初次抓拍)" : "") + "将再次抓拍",
  183. ]);
  184. this.retrySnapTimeout = setTimeout(() => {
  185. this.toggleSnapNow();
  186. }, 60 * 1000);
  187. } finally {
  188. this.videoStartPlay();
  189. this.decreaseSnapCount();
  190. }
  191. },
  192. videoStartPlay() {
  193. const video = this.$refs.video;
  194. video && video.play();
  195. },
  196. async snap() {
  197. // TODO: chrome 70. FaceDetector检测人脸
  198. // var canvas = document.createElement("canvas");
  199. // canvas.width = 220;
  200. // canvas.height = 165;
  201. // var context = canvas.getContext("2d");
  202. // context.drawImage(this.$refs.video, 0, 0, 220, 165);
  203. // var f = new FaceDetector();
  204. // const v = await f.detect(canvas);
  205. // console.log(v);
  206. // return;
  207. this.$Message.destroy();
  208. try {
  209. this.disableSnap = true;
  210. // console.log("disableSnap: " + this.disableSnap);
  211. // await new Promise(resolve =>
  212. // setTimeout(() => {
  213. // console.log(new Date());
  214. // resolve();
  215. // }, 3000)
  216. // );
  217. // return;
  218. // if(this.disableSnap) return; // 避免界面没有更新。
  219. this.msg = "拍照中...";
  220. const captureBlob = await this.getSnapShot();
  221. this.videoStartPlay();
  222. this.msg = "上传照片中...";
  223. const [captureFilePath, signIdentifier] = await this.uploadToServer(
  224. captureBlob
  225. );
  226. this.msg = "人脸比对中...";
  227. await this.faceCompareSync(captureFilePath, signIdentifier);
  228. } catch (error) {
  229. // FIXME: more processing
  230. console.log("同步照片比对流程失败");
  231. throw error;
  232. } finally {
  233. this.videoStartPlay();
  234. this.msg = "开始识别";
  235. this.disableSnap = false;
  236. }
  237. },
  238. async getSnapShot() {
  239. return new Promise((resolve, reject) => {
  240. const video = this.$refs.video;
  241. if (video.readyState !== 4 || !video.srcObject.active) {
  242. this.$Message.error({
  243. content: "摄像头没有正常启用",
  244. duration: 5,
  245. closable: true,
  246. });
  247. window._hmt.push([
  248. "_trackEvent",
  249. "摄像头框",
  250. "摄像头状态",
  251. "摄像头没有正常启用-退出" +
  252. (this.lastSnapTime ? "(非初次抓拍)" : ""),
  253. ]);
  254. reject("摄像头没有正常启用");
  255. this.logout(
  256. "?LogoutReason=" +
  257. "摄像头没有正常启用-退出" +
  258. (this.lastSnapTime ? "(非初次抓拍)" : "")
  259. );
  260. return;
  261. }
  262. video.pause();
  263. var canvas = document.createElement("canvas");
  264. canvas.width = 220;
  265. canvas.height = 165;
  266. var context = canvas.getContext("2d");
  267. context.drawImage(video, 0, 0, 220, 165);
  268. canvas.toBlob(resolve, "image/png", 0.95);
  269. });
  270. },
  271. async uploadToServer(captureBlob) {
  272. async function blobToArray(blob) {
  273. return new Promise(resolve => {
  274. var reader = new FileReader();
  275. reader.addEventListener("loadend", function() {
  276. // reader.result contains the contents of blob as a typed array
  277. resolve(reader.result);
  278. });
  279. reader.readAsArrayBuffer(blob);
  280. });
  281. }
  282. //保存抓拍照片到服务器
  283. let resultUrl, signIdentifier;
  284. try {
  285. const buffer = await blobToArray(captureBlob);
  286. // console.log(buffer);
  287. // var view1 = new Uint8Array(buffer);
  288. // console.log(buffer[0], buffer[1], buffer[429721]);
  289. const fileMd5 = MD5(buffer);
  290. console.log(fileMd5);
  291. const params = new URLSearchParams();
  292. params.append("fileSuffix", "png");
  293. params.append("fileMd5", fileMd5);
  294. const res = await this.$http.get(
  295. "/api/ecs_oe_student/examControl/getCapturePhotoUpYunSign?" + params
  296. );
  297. // console.log(res);
  298. // let myHeaders = new Headers();
  299. // for (let [k, v] of Object.entries(res.data.headers)) {
  300. // // console.log(k, v);
  301. // if (k.includes("tion") || k.includes("Date") || k.includes("MD5")) {
  302. // if (k === "Date") k = "x-date";
  303. // myHeaders.append(k, v);
  304. // }
  305. let myFormData = new FormData();
  306. for (let [k, v] of Object.entries(res.data.formParams)) {
  307. myFormData.append(k, v);
  308. }
  309. myFormData.append("file", captureBlob);
  310. await fetch(res.data.formUrl, {
  311. method: "POST",
  312. body: myFormData,
  313. });
  314. // console.log(response);
  315. resultUrl = res.data.accessUrl;
  316. signIdentifier = res.data.signIdentifier;
  317. // this.serverLog("debug/S-005001", "抓拍照片保存成功:");
  318. window._hmt.push(["_trackEvent", "摄像头框", "抓拍照片保存成功"]);
  319. } catch (e) {
  320. console.log(e);
  321. // this.serverLog("debug/S-006001", "抓拍照片保存失败");
  322. window._hmt.push(["_trackEvent", "摄像头框", "抓拍照片保存失败"]);
  323. this.$Message.error({
  324. content: "保存抓拍照片到服务器失败!",
  325. duration: 15,
  326. closable: true,
  327. });
  328. throw "保存抓拍照片到服务器失败!";
  329. }
  330. // let UPYUN_URL;
  331. // try {
  332. // UPYUN_URL = (await this.$http.get("/api/ecs_oe_student_face/upyun"))
  333. // .data.downloadPrefix;
  334. // } catch (error) {
  335. // this.$Message.error({ content: "获取照片下载前缀失败!", duration: 15, closable: true});
  336. // throw "获取照片下载前缀失败!";
  337. // }
  338. return [resultUrl, signIdentifier];
  339. },
  340. async faceCompareSync(captureFilePath, signIdentifier) {
  341. try {
  342. const res = await this.$http.post(
  343. "/api/ecs_oe_student_face/examCaptureQueue/compareFaceSync?signIdentifier=" +
  344. signIdentifier +
  345. "&fileUrl=" +
  346. encodeURIComponent(captureFilePath)
  347. );
  348. // TODO: 识别成功、失败的通知或跳转
  349. this.$emit("on-recognize-result", {
  350. error: null,
  351. pass: res.data.isPass,
  352. stranger: res.data.isStranger,
  353. });
  354. } catch (e) {
  355. console.log(e);
  356. // this.$Message.error(e.message);
  357. throw "同步照片比较失败!";
  358. }
  359. },
  360. async faceCompare(captureFilePath, signIdentifier, examRecordDataId) {
  361. try {
  362. let cameraInfos;
  363. let hasVirtualCamera = false;
  364. if (typeof nodeRequire != "undefined") {
  365. try {
  366. var fs = window.nodeRequire("fs");
  367. if (fs.existsSync("multiCamera.exe")) {
  368. await new Promise((resolve, reject) => {
  369. window.nodeRequire("node-cmd").get("multiCamera.exe", () => {
  370. try {
  371. cameraInfos = fs.readFileSync("CameraInfo.txt", "utf-8");
  372. if (cameraInfos && cameraInfos.trim()) {
  373. cameraInfos = cameraInfos.trim();
  374. cameraInfos = cameraInfos.replace(/\r\n/g, "");
  375. cameraInfos = cameraInfos.replace(/\n/g, "");
  376. console.log(cameraInfos);
  377. this.serverLog("debug/S-001001", cameraInfos);
  378. }
  379. if (cameraInfos.includes('""')) {
  380. hasVirtualCamera = true;
  381. }
  382. resolve();
  383. } catch (error) {
  384. window._hmt.push([
  385. "_trackEvent",
  386. "摄像头框",
  387. "虚拟摄像头-读取摄像头列表失败",
  388. ]);
  389. reject("读取摄像头列表失败");
  390. }
  391. });
  392. });
  393. }
  394. } catch (error) {
  395. console.log(error);
  396. }
  397. }
  398. let body = {
  399. fileUrl: captureFilePath,
  400. signIdentifier,
  401. examRecordDataId,
  402. };
  403. if (cameraInfos) {
  404. body.cameraInfos = cameraInfos;
  405. body.hasVirtualCamera = hasVirtualCamera;
  406. }
  407. const res = await this.$http.post(
  408. "/api/ecs_oe_student_face/examCaptureQueue/uploadExamCapture",
  409. body
  410. );
  411. const fileName = res.data;
  412. try {
  413. await this.showSnapResult(fileName, examRecordDataId);
  414. } catch (error) {
  415. this.$Message.error({
  416. content: "设置获取抓拍结果失败!",
  417. duration: 15,
  418. closable: true,
  419. });
  420. }
  421. } catch (e) {
  422. console.log(e);
  423. // this.$Message.error(e.message);
  424. throw "异步比较抓拍照片失败";
  425. }
  426. },
  427. async showSnapResult(fileName, examRecordDataId) {
  428. if (!fileName) return; // 交卷后提交照片会得不到照片名称
  429. if (this.$route.name !== "OnlineExamingHome") {
  430. // 非考试页,不显示结果,也不继续查询
  431. return;
  432. }
  433. try {
  434. // 获取抓拍结果
  435. const snapRes =
  436. (await this.$http.get(
  437. "/api/ecs_oe_student_face/examCaptureQueue/getExamCaptureResult?fileName=" +
  438. fileName +
  439. "&examRecordDataId=" +
  440. examRecordDataId
  441. )).data || {};
  442. if (snapRes.isCompleted) {
  443. if (snapRes.isStranger) {
  444. this.$Message.error({
  445. content: "请独立完成考试",
  446. duration: 5,
  447. closable: true,
  448. });
  449. } else if (!snapRes.isPass) {
  450. this.$Message.error({
  451. content: "请调整坐姿,诚信考试",
  452. duration: 5,
  453. closable: true,
  454. });
  455. }
  456. } else {
  457. this.showSnapResultTimeout = setTimeout(
  458. this.showSnapResult.bind(this, fileName, examRecordDataId),
  459. 30 * 1000
  460. );
  461. }
  462. } catch (e) {
  463. console.log(e);
  464. if (this.$route.name !== "OnlineExamingHome") {
  465. // 非考试页,不显示结果,也不继续查询
  466. return;
  467. }
  468. this.$Message.error(e.message);
  469. throw e.message;
  470. }
  471. },
  472. },
  473. };
  474. </script>
  475. <style scoped>
  476. .verify-button {
  477. font-size: 16px;
  478. background-color: #ffcc00;
  479. display: inline-block;
  480. padding: 6px 16px;
  481. border-radius: 6px;
  482. }
  483. .verify-button:hover {
  484. color: #444444;
  485. cursor: pointer;
  486. }
  487. .disable-verify-button {
  488. background-color: #f7f7f7;
  489. color: #c5c8ce;
  490. }
  491. .disable-verify-button:hover {
  492. cursor: not-allowed;
  493. color: #c5c8ce;
  494. }
  495. </style>