VideoLocalWindow copy.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <template>
  2. <div>
  3. <video ref="video" autoplay muted playsinline></video>
  4. <!-- <el-button @click="startCamera">开启摄像头</el-button>
  5. <el-button @click="stopCamera" :disabled="!isCameraActive"
  6. >关闭摄像头</el-button
  7. > -->
  8. </div>
  9. </template>
  10. <script>
  11. import { GetPersonByFace, GetPersonByFaceBase64 } from "@/API/custom";
  12. export default {
  13. props: {
  14. processStep: {
  15. type: String,
  16. require: true,
  17. },
  18. isUpload: {
  19. type: String,
  20. require: false,
  21. },
  22. cameraData: {
  23. type: Object,
  24. default: () => ({}),
  25. },
  26. },
  27. watch: {
  28. processStep: {
  29. handler(step) {
  30. if (step == 0 || step == 9) {
  31. this.startCamera();
  32. } else {
  33. this.stopCamera();
  34. }
  35. },
  36. deep: true,
  37. immediate: true,
  38. },
  39. isUpload: {
  40. handler(val) {
  41. console.log("上传子级开关", val);
  42. if (val) {
  43. this.uploadImage();
  44. } else {
  45. this.isUpload = false;
  46. }
  47. },
  48. deep: true,
  49. immediate: true,
  50. },
  51. },
  52. data() {
  53. return {
  54. isCameraActive: false,
  55. stream: null,
  56. captureInterval: null, // 新增定时器变量
  57. };
  58. },
  59. methods: {
  60. async startCamera() {
  61. try {
  62. const stream = await navigator.mediaDevices.getUserMedia({
  63. video: {
  64. width: { ideal: 1280 },
  65. height: { ideal: 800 },
  66. facingMode: "user",
  67. },
  68. });
  69. this.stream = stream;
  70. this.isCameraActive = true;
  71. this.$refs.video.srcObject = stream;
  72. if (this.isUpload) {
  73. setTimeout(() => {
  74. this.uploadImage();
  75. }, 1000);
  76. } else {
  77. // 新增定时截图功能
  78. this.captureInterval = setInterval(() => {
  79. this.captureImage();
  80. }, 3000);
  81. }
  82. } catch (err) {
  83. console.error("摄像头访问错误:", err);
  84. alert(`摄像头访问失败: ${err.message}`);
  85. }
  86. },
  87. stopCamera() {
  88. if (this.stream) {
  89. this.stream.getTracks().forEach((track) => track.stop());
  90. this.isCameraActive = false;
  91. this.$refs.video.srcObject = null;
  92. this.stream = null;
  93. // 停止定时器
  94. if (this.captureInterval) {
  95. clearInterval(this.captureInterval);
  96. this.captureInterval = null;
  97. }
  98. }
  99. },
  100. // 新增截图方法
  101. // async captureImage() {
  102. // const video = this.$refs.video;
  103. // video.currentTime = 1;
  104. // const canvas = document.createElement("canvas");
  105. // canvas.width = video.videoWidth;
  106. // canvas.height = video.videoHeight;
  107. // canvas
  108. // .getContext("2d")
  109. // .drawImage(video, 0, 0, canvas.width, canvas.height);
  110. // // 下载到本地
  111. // // const link = document.createElement("a");
  112. // // link.download = `capture_${new Date()
  113. // // .toISOString()
  114. // // .replace(/[:.]/g, "-")}.png`;
  115. // // link.href = canvas.toDataURL("image/png");
  116. // // link.click();
  117. // // 将canvas转为Blob对象
  118. // canvas.toBlob(async (blob) => {
  119. // if (blob) {
  120. // console.log("blob数据", blob);
  121. // const formData = new FormData();
  122. // formData.append("timestamp", new Date().toISOString());
  123. // formData.append("file", blob, "screenshot.png");
  124. // console.log("上传数据", formData);
  125. // try {
  126. // // const res = await GetPersonByFace(formData);
  127. // // console.log("上传反馈", res);
  128. // // const response = await fetch("System/Person/GetPersonByFace", {
  129. // // method: "POST",
  130. // // body: formData,
  131. // // });
  132. // } catch (err) {
  133. // console.error("上传过程中出错:", err);
  134. // }
  135. // }
  136. // }, "image/png");
  137. // },
  138. async captureImage() {
  139. const video = this.$refs.video;
  140. const canvas = document.createElement("canvas");
  141. canvas.width = video.videoWidth;
  142. canvas.height = video.videoHeight;
  143. canvas.getContext("2d").drawImage(video, 0, 0);
  144. try {
  145. const base64Data = canvas.toDataURL("image/png");
  146. // console.log("上传数据:Base64", base64Data);
  147. // 替换YOUR_SERVER_URL为你的实际服务器地址
  148. const response = await fetch(
  149. "/yapi/System/Person/GetPersonByFaceBase64",
  150. {
  151. method: "POST",
  152. headers: {
  153. token: localStorage.getItem("token"),
  154. "Content-Type": "application/json",
  155. },
  156. body: JSON.stringify({
  157. data: base64Data,
  158. fileModule: 2,
  159. }),
  160. }
  161. );
  162. const result = await response.json();
  163. // console.log("上传反馈", response, result);
  164. if (result.code === 20000) {
  165. console.log("服务器返回数据:", result);
  166. this.$emit("output", result.data);
  167. setTimeout(() => {
  168. this.stopCamera();
  169. }, 200);
  170. }
  171. } catch (err) {
  172. console.error("上传过程中出错:", err);
  173. }
  174. },
  175. async uploadImage() {
  176. const video = this.$refs.video;
  177. const canvas = document.createElement("canvas");
  178. canvas.width = video.videoWidth;
  179. canvas.height = video.videoHeight;
  180. canvas.getContext("2d").drawImage(video, 0, 0);
  181. try {
  182. const base64Data = canvas.toDataURL("image/png");
  183. // console.log("上传数据:Base64", base64Data);
  184. // 替换YOUR_SERVER_URL为你的实际服务器地址
  185. const response = await fetch("/yapi/File/UploadBase64", {
  186. method: "POST",
  187. headers: {
  188. token: localStorage.getItem("token"),
  189. "Content-Type": "application/json",
  190. },
  191. body: JSON.stringify({
  192. data: base64Data,
  193. fileModule: 1,
  194. }),
  195. });
  196. const result = await response.json();
  197. console.log("上传反馈", response, result);
  198. if (result.code === 20000) {
  199. console.log("服务器上传返回数据:", result);
  200. this.$emit("outputPath", result.data);
  201. setTimeout(() => {
  202. this.stopCamera();
  203. }, 200);
  204. }
  205. this.isUpload = false;
  206. } catch (err) {
  207. console.error("上传过程中出错:", err);
  208. }
  209. },
  210. },
  211. created() {
  212. this.startCamera();
  213. },
  214. beforeDestroy() {
  215. this.stopCamera();
  216. },
  217. };
  218. </script>
  219. <style scoped>
  220. video {
  221. height: 800px;
  222. width: 1280px;
  223. /* width: 100%;
  224. max-width: 500px; */
  225. background: #000;
  226. }
  227. </style>