door.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>人脸识别</title>
  6. <!-- 按当前 HTML 所在目录设置 base,避免 web-view 下 ./js 解析到错误路径导致 face.js 未执行 -->
  7. <script>
  8. (function () {
  9. try {
  10. var p = window.location.pathname || "";
  11. var i = p.lastIndexOf("/");
  12. if (i >= 0) {
  13. document.write('<base href="' + p.substring(0, i + 1).replace(/"/g, "") + '">');
  14. }
  15. } catch (e) {}
  16. })();
  17. </script>
  18. <script type="text/javascript" src="./js/tracking.js"></script>
  19. <script type="text/javascript" src="./js/face_data/face.js"></script>
  20. <script type="text/javascript" src="./js/face_data/eye.js"></script>
  21. <script type="text/javascript" src="./js/face_data/mouth.js"></script>
  22. <script type="text/javascript" src="./js/jquery-2.2.1.min.js"></script>
  23. <!-- VUE3 的 SDK -->
  24. <script type="text/javascript" src="./js/vue.global.prod.js"></script>
  25. <!-- uni 的 SDK -->
  26. <script type="text/javascript" src="./js/uni.webview.1.5.4.js"></script>
  27. <!-- 全局区域样式 -->
  28. <link rel="stylesheet" href="./css/door_homeCard.css">
  29. <!-- 顶部区域样式 -->
  30. <link rel="stylesheet" href="./css/door_homeCardTop.css">
  31. <!-- 中心区域样式 -->
  32. <link rel="stylesheet" href="./css/door_homeCardCenter.css">
  33. <!-- 底部区域样式 -->
  34. <link rel="stylesheet" href="./css/door_homeCardFooter.css">
  35. <!-- 引入阿里iconfont库 -->
  36. <link rel="stylesheet" href="https://at.alicdn.com/t/c/font_4400427_yp2hzj2yvbf.css">
  37. <style>
  38. .home-card-footer .btnArea .iconfont {
  39. font-family: "iconfont" !important;
  40. font-size: 2rem;
  41. font-style: normal;
  42. -webkit-font-smoothing: antialiased;
  43. -webkit-text-stroke-width: 0.2px;
  44. -moz-osx-font-smoothing: grayscale;
  45. color: white;
  46. }
  47. .home-card-footer .btnArea .iconfont:nth-child(2n) {
  48. margin-left: 1rem;
  49. }
  50. .workRed {
  51. background-color: #f11e16;
  52. }
  53. .workYellow {
  54. background-color: #E6A23C;
  55. }
  56. .workGreen {
  57. background-color: #67C23A;
  58. }
  59. @media (min-width: 768px) {
  60. .home-card-top {
  61. width: calc(100% - 5rem) !important;
  62. margin: 1rem !important;
  63. padding: 2rem 1.5rem !important;
  64. }
  65. .home-card-top .date .time1 {
  66. margin-right: 1.5rem !important;
  67. font-size: 5rem !important;
  68. }
  69. .home-card-top .date .time2 {
  70. font-size: 2.5rem !important;
  71. }
  72. .home-card-top .title {
  73. font-size: 2rem !important;
  74. }
  75. .home-card-center>.date {
  76. font-size: 4rem;
  77. }
  78. .home-card-center>.date .title {
  79. margin-bottom: 5rem;
  80. }
  81. .home-card-center>.date .remark {
  82. font-size: 2rem;
  83. margin-bottom: 5rem;
  84. }
  85. .home-card-center>.date .workStatus {
  86. font-size: 3rem;
  87. padding: 10px 30px;
  88. }
  89. .home-card-footer {
  90. font-size: 2.5rem !important;
  91. margin-bottom: 4rem !important;
  92. }
  93. .home-card-footer .date .time1 {
  94. font-size: 3rem !important;
  95. margin-right: 2rem !important;
  96. }
  97. .home-card-footer .date .time2 {
  98. font-size: 2rem !important;
  99. }
  100. .home-card-footer .date .title {
  101. margin-top: 1rem !important;
  102. }
  103. .home-card-footer .btnArea .iconfont {
  104. font-size: 5.5rem;
  105. }
  106. .home-card-footer .btnArea .iconfont:nth-child(2n) {
  107. margin-left: 3rem;
  108. }
  109. }
  110. </style>
  111. <script>
  112. // 提前占位,防止宿主在页面初始化前调用 receiveData 导致未定义
  113. window.__pendingReceiveData = [];
  114. window.receiveData = function (msg) {
  115. window.__pendingReceiveData.push(msg);
  116. };
  117. </script>
  118. </head>
  119. <body>
  120. <div id="face-container" class="face-container home-card">
  121. <!-- 背景图片 -->
  122. <div class="home-card-image"
  123. :style="{backgroundImage: `url('${state.imgPath || 'img/face_bg.png'}')`, backgroundSize: '100% 100%', backgroundRepeat: 'no-repeat'}">
  124. </div>
  125. <!-- 顶部内容区域 -->
  126. <div class="home-card-top">
  127. <div class="date" id="configDialog2">
  128. <span class="time1">{{ state.dateTime }}</span>
  129. <span class="time2">{{ state.date }}</span>
  130. </div>
  131. <div class="title" v-if="state.openMode && state.openMode.includes('人脸')">{{ state.doorName || '未绑定门禁' }}</div>
  132. </div>
  133. <!-- 人脸内容区域 -->
  134. <div class="home-card-center">
  135. <div class="date" id="configDialog1" v-if="!state.openMode || !state.openMode.includes('人脸')">
  136. <div class="title">{{ state.doorName || '未绑定门禁' }}</div>
  137. <div class="remark" v-if="state.remark">{{ state.remark }}</div>
  138. <div class="workStatus" :class="state.workClass" v-if="state.workName">{{ state.workName }}</div>
  139. </div>
  140. <div class="face" v-if="state.openMode && state.openMode.includes('人脸')">
  141. <!-- height="1564" -->
  142. <video id="video" width="300" height="300" style="width:40vh;height:40vh" preload autoplay loop
  143. muted></video>
  144. <canvas id="myCanvas" width="300" height="300" style="width:40vh;height:40vh"></canvas>
  145. <!-- 人脸特效区域 -->
  146. <div id="specialEffects" class="specialEffects"></div>
  147. </div>
  148. </div>
  149. <!-- 底部内容区域 -->
  150. <div class="home-card-footer">
  151. <div class="btnArea">
  152. <i class="iconfont oa-dianji" @click="parentMessage('点击开门')" v-if="state.openMode && state.openMode.includes('点击开门')"></i>
  153. <i class="iconfont oa-mima" @click="parentMessage('密码')" v-if="state.openMode && state.openMode.includes('密码')"></i>
  154. </div>
  155. </div>
  156. </div>
  157. <script>
  158. // 创建Vue实例
  159. Vue.createApp({
  160. components: {},
  161. emits: [],
  162. props: {},
  163. data() {
  164. return {
  165. flag: true,
  166. time: 1000,
  167. tracker: null,
  168. trackerTask: null,
  169. state: {
  170. date: null,
  171. dateTime: null,
  172. doorName: null,
  173. imgPath: null,
  174. openMode: "",
  175. remark: "",
  176. workName: "",
  177. workClass: ""
  178. },
  179. timeOutEvent: 0,
  180. inter: {
  181. dateDom: null
  182. },
  183. workStatusData: [
  184. { label: "办公", value: 1, class: 'workRed' },
  185. { label: "会客", value: 2, class: 'workRed' },
  186. { label: "外出", value: 3, class: 'workGreen' },
  187. { label: "勿扰", value: 4, class: 'workRed' },
  188. ],
  189. };
  190. },
  191. computed: {},
  192. methods: {
  193. // 初始化数据
  194. initData(event) {
  195. var that = this;
  196. that.state.doorName = event.deviceName
  197. that.state.imgPath = event.imgPath
  198. that.state.openMode = event.openMode
  199. that.state.remark = event.remark
  200. var workStatusItem = that.workStatusData.find(item => item.value == event.workStatus);
  201. if (workStatusItem) {
  202. that.state.workName = workStatusItem.label;
  203. that.state.workClass = workStatusItem.class;
  204. } else {
  205. that.state.workName = '';
  206. that.state.workClass = '';
  207. }
  208. if (event.openMode && event.openMode.includes("人脸")) {
  209. that.tracker == null ? that.initVido() : undefined
  210. } else {
  211. that.tracker != null ? that.closeFace() : undefined
  212. }
  213. },
  214. // 初始化事件
  215. initHandle() {
  216. var that = this;
  217. $("#configDialog1,#configDialog2").on({
  218. touchstart: function (e) {
  219. that.timeOutEvent = setTimeout(() => {
  220. that.parentMessage('打开门禁配置')
  221. that.timeOutEvent = 0
  222. }, 1000);
  223. e.preventDefault();
  224. },
  225. touchmove: function () {
  226. clearTimeout(that.timeOutEvent);
  227. that.timeOutEvent = 0;
  228. },
  229. touchend: function () {
  230. clearTimeout(that.timeOutEvent);
  231. if (that.timeOutEvent != 0) {
  232. console.log("你这是点击,不是长按");
  233. }
  234. return false;
  235. }
  236. })
  237. },
  238. // 初始化摄像头
  239. initVido() {
  240. var that = this;
  241. var video = document.getElementById("video");//视频dom
  242. video.style.transform = 'scaleX(-1)';//视频翻转(1.水平翻转-scaleX(-1) 2.垂直翻转-scaleY(-1))
  243. var canvas = document.getElementById('myCanvas');//画布dom
  244. canvas.style.transform = 'scaleX(-1)';//画布翻转(1.水平翻转-scaleX(-1) 2.垂直翻转-scaleY(-1))
  245. var context = canvas.getContext('2d');
  246. that.tracker = new tracking.ObjectTracker(['face']);//'face', 'eye', 'mouth'
  247. that.tracker.setInitialScale(4); //设置识别的放大比例
  248. that.tracker.setStepSize(2);//设置步长
  249. that.tracker.setEdgesDensity(0.1);//边缘密度
  250. //启动摄像头,并且识别视频内容
  251. that.trackerTask = tracking.track('#video', that.tracker, {
  252. camera: true,
  253. });
  254. that.tracker.on('track', function (event) {
  255. // console.log(event.data.length)
  256. if (that.flag) {
  257. // console.log("拍照");
  258. that.state.faceImgState = false;
  259. context.drawImage(video, 0, 0, video.width, video.height);
  260. that.saveAsLocalImage()
  261. // that.capturePartialImage(rect.x, rect.y, rect.width, rect.height);
  262. context.clearRect(0, 0, canvas.width, canvas.height);
  263. that.flag = false;
  264. } else {
  265. //console.log("冷却中");
  266. }
  267. if (event.data.length === 0) {
  268. // console.log('未检测到人脸')
  269. context.clearRect(0, 0, canvas.width, canvas.height);
  270. } else if (event.data.length > 1) {
  271. // console.log('检测到多张人脸')
  272. context.clearRect(0, 0, canvas.width, canvas.height);
  273. } else {
  274. context.clearRect(0, 0, canvas.width, canvas.height);
  275. event.data.forEach(function (rect) {
  276. context.strokeStyle = '#409eff';
  277. context.strokeRect(rect.x, rect.y, rect.width, rect.height);
  278. context.fillStyle = "#409eff";
  279. context.lineWidth = 1.5;
  280. });
  281. }
  282. });
  283. },
  284. // 向父页面推送数据
  285. parentMessage(type, data) {
  286. var message = {
  287. funcName: type,
  288. data: data,
  289. };
  290. //APP-PLUS
  291. uni.postMessage({
  292. data: message
  293. });
  294. //H5
  295. if (window.parent) {
  296. window.parent.postMessage(message, '*');
  297. }
  298. },
  299. // 当需要抓拍部分画布时
  300. capturePartialImage(x, y, width, height) {
  301. // 创建一个新的canvas,用于抓拍部分画布
  302. const canvas = document.getElementById('myCanvas');//画布dom
  303. const croppedCanvas = document.createElement('canvas');
  304. croppedCanvas.width = width;
  305. croppedCanvas.height = height;
  306. const croppedCtx = croppedCanvas.getContext('2d');
  307. // 只抓取需要的部分
  308. croppedCtx.drawImage(canvas, x, y, width, height, 0, 0, width, height);
  309. var image = croppedCanvas.toDataURL("image/png")
  310. that.parentMessage('人脸识别', { imageBase: image })
  311. },
  312. // 获取图片bold
  313. saveAsLocalImage() {
  314. var that = this
  315. // var myCanvas = document.getElementById("myCanvas");
  316. // var image = myCanvas.toDataURL("image/png")
  317. // that.parentMessage('人脸识别', { imageBase: image })
  318. // 创建一个新的canvas,用于抓拍部分画布
  319. const canvas = document.getElementById('myCanvas');//画布dom
  320. const croppedCanvas = document.createElement('canvas');
  321. croppedCanvas.width = 200;
  322. croppedCanvas.height = 200;
  323. const croppedCtx = croppedCanvas.getContext('2d');
  324. // 只抓取需要的部分
  325. croppedCtx.drawImage(canvas, 0, 0, 150, 150);
  326. var image = croppedCanvas.toDataURL("image/png");
  327. that.parentMessage('人脸识别', { imageBase: image })
  328. },
  329. // 人脸冷却
  330. faceCooling() {
  331. var that = this
  332. setTimeout(() => {
  333. that.flag = true
  334. that.state.faceImgState = false;
  335. }, that.time);
  336. },
  337. // 解析数据
  338. analysisData(event) {
  339. console.log(event.funcName)
  340. if ("funcName" in event) {
  341. if (event.funcName == "初始化数据") {
  342. this.initData(JSON.parse(event.data));
  343. } else if (event.funcName == "开启摄像头" && document.getElementById("video")) {
  344. this.initVido();//调用初始化摄像头
  345. } else if (event.funcName == "关闭摄像头") {
  346. this.closeFace();
  347. } else if (event.funcName == "人脸冷却") {
  348. this.faceCooling();
  349. }
  350. }
  351. },
  352. // 监听页面是否隐藏
  353. handleVisibilityChange() {
  354. if (document.visibilityState === 'visible') {
  355. // 页面变为可见时的处理逻辑
  356. console.log('页面变为可见');
  357. this.tracker == null && document.getElementById("video") ? this.initVido() : undefined
  358. } else if (document.visibilityState === 'hidden') {
  359. // 页面变为不可见时的处理逻辑
  360. console.log('页面变为不可见');
  361. this.tracker != null && document.getElementById("video") ? this.closeFace() : undefined
  362. }
  363. },
  364. // 关闭摄像头
  365. closeFace() {
  366. try {
  367. this.tracker = null
  368. // 关闭摄像头
  369. let video = document.getElementById('video')
  370. video.srcObject.getTracks()[0].stop()
  371. // 停止侦测
  372. this.trackerTask.stop()
  373. } catch (error) { }
  374. },
  375. /**
  376. * @获取年月日时分
  377. * @returns
  378. */
  379. getFormatterDate(time3) {
  380. var date = new Date(time3);
  381. var Y = date.getFullYear() + "/";
  382. var M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "/";
  383. var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
  384. var h = (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
  385. var m = (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes())
  386. var strDate = Y + M + D + h + m;
  387. return strDate;
  388. },
  389. },
  390. created() {
  391. var that = this
  392. // APP-PLUS || H5(接收父页面传过来的值)
  393. window.receiveData = (msg) => {
  394. that.analysisData(msg)
  395. }
  396. window.addEventListener("message", function (event) {
  397. that.analysisData(event.data)
  398. });
  399. },
  400. mounted() {
  401. document.addEventListener('visibilitychange', this.handleVisibilityChange);
  402. if (!this.inter.dateDom) {
  403. this.inter.dateDom = setInterval(() => {
  404. this.state.date = this.getFormatterDate(new Date()).split(' ')[0]
  405. this.state.dateTime = this.getFormatterDate(new Date()).split(' ')[1]
  406. }, 1000);
  407. }
  408. this.initHandle();//初始化事件dom
  409. },
  410. beforeDestroy() {
  411. // 移除window方法
  412. window.receiveData = null;
  413. clearInterval(inter.dateDom); //销毁之前定时器
  414. },
  415. watch: {},
  416. }).mount('#face-container');
  417. </script>
  418. </body>
  419. </html>