CameraUpload.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <div class="camera-upload">
  3. <svg-icon icon-class="paizhao" size="80" @click="openMedia" />
  4. <svg-icon icon-class="bendishangchuan" size="80" />
  5. </div>
  6. <el-dialog
  7. id="carmer-dialog"
  8. v-model="visible"
  9. :close-on-click-modal="false"
  10. :title="null"
  11. close-icon="null"
  12. >
  13. <video id="video" autoplay="autoplay" height="500px" width="500px"></video>
  14. <canvas id="canvas" height="500px" width="500px"></canvas>
  15. <img id="imgTag" alt="imgTag" src="" />
  16. <div class="bottom-btns">
  17. <el-button class="cancelBtn" @click="closeMedia">关闭</el-button>
  18. <el-button class="sureBtn" type="primary" @click="takePhoto"
  19. >拍照
  20. </el-button>
  21. </div>
  22. </el-dialog>
  23. </template>
  24. <script setup>
  25. import { uploadFileApi } from "@/api/file";
  26. let mediaStreamTrack = null; // 视频对象(全局)
  27. let video;
  28. const cameraEmit = defineEmits(["uploadFinish"]);
  29. const openMedia = async () => {
  30. visible.value = true;
  31. nextTick(() => {
  32. let constraints = {
  33. video: { width: 500, height: 500 },
  34. audio: false,
  35. };
  36. //获得video摄像头
  37. video = document.getElementById("video");
  38. navigator.mediaDevices
  39. .getUserMedia(constraints)
  40. .then((mediaStream) => {
  41. // mediaStreamTrack = typeof mediaStream.stop === 'function' ? mediaStream : mediaStream.getTracks()[1];
  42. mediaStreamTrack = mediaStream.getVideoTracks();
  43. video.srcObject = mediaStream;
  44. video.play();
  45. })
  46. .catch((error) => {
  47. console.log("Error: " + error);
  48. ElMessage.error("没有找到设备,或者没有权限");
  49. });
  50. });
  51. };
  52. function dataURItoBlob(base64Data) {
  53. var byteString;
  54. if (base64Data.split(",")[0].indexOf("base64") >= 0)
  55. byteString = atob(base64Data.split(",")[1]);
  56. else byteString = unescape(base64Data.split(",")[1]);
  57. var mimeString = base64Data.split(",")[0].split(":")[1].split(";")[0];
  58. var ia = new Uint8Array(byteString.length);
  59. for (var i = 0; i < byteString.length; i++) {
  60. ia[i] = byteString.charCodeAt(i);
  61. }
  62. return new Blob([ia], { type: mimeString });
  63. }
  64. // 拍照
  65. function takePhoto() {
  66. //获得Canvas对象
  67. let video = document.getElementById("video");
  68. let canvas = document.getElementById("canvas");
  69. let ctx = canvas.getContext("2d");
  70. ctx.drawImage(video, 0, 0, 500, 500);
  71. // toDataURL --- 可传入'image/png'---默认, 'image/jpeg'
  72. let base64Data = document.getElementById("canvas").toDataURL();
  73. let blob = dataURItoBlob(base64Data);
  74. let file = new File([blob], "filename.png", { type: "image/png" });
  75. uploadFileApi(file).then((res) => {
  76. console.log("上传图片", res);
  77. cameraEmit("uploadFinish", res);
  78. closeMedia();
  79. });
  80. }
  81. // 关闭摄像头
  82. function closeMedia() {
  83. let stream = document.getElementById("video").srcObject;
  84. let tracks = stream.getTracks();
  85. tracks.forEach(function (track) {
  86. track.stop();
  87. });
  88. document.getElementById("video").srcObject = null;
  89. visible.value = false;
  90. }
  91. const visible = ref(false);
  92. </script>
  93. <style lang="scss" scoped>
  94. .camera-upload {
  95. display: flex;
  96. justify-content: space-evenly;
  97. align-items: center;
  98. width: 292px;
  99. height: 292px;
  100. border-radius: 16px 16px 16px 16px;
  101. border: 2px dashed rgba(0, 0, 0, 0.2);
  102. .icon-back:nth-child(1) {
  103. margin-right: 58px;
  104. }
  105. }
  106. </style>
  107. <style lang="scss" scoped>
  108. #carmer-dialog {
  109. background: #f1f3f5;
  110. box-shadow: 0px 0px 80px 10px rgba(0, 0, 0, 0.25);
  111. border-radius: 16px 16px 16px 16px;
  112. width: 924px;
  113. max-height: 80vh;
  114. .top-title {
  115. width: 100%;
  116. height: 38px;
  117. font-weight: 500;
  118. font-size: 38px;
  119. color: rgba(0, 0, 0, 0.9);
  120. text-align: center;
  121. }
  122. .center-content {
  123. margin-top: 24px;
  124. width: 100%;
  125. //height: 200px;
  126. display: flex;
  127. justify-content: center;
  128. align-items: center;
  129. font-size: 24px;
  130. color: rgba(0, 0, 0, 0.9);
  131. }
  132. .body {
  133. width: calc(100% - 240px);
  134. max-height: calc(50vh - 80px);
  135. margin: 0 auto;
  136. overflow-y: auto;
  137. .box {
  138. width: 100%;
  139. height: 100px;
  140. }
  141. }
  142. .bottom-btns {
  143. display: flex;
  144. justify-content: center;
  145. margin-top: 20px;
  146. margin-bottom: 20px;
  147. .button {
  148. margin-right: 20px;
  149. }
  150. .cancelBtn {
  151. width: 292px;
  152. height: 80px;
  153. background: rgba(0, 0, 0, 0.06);
  154. border-radius: 76px 76px 76px 76px;
  155. }
  156. .sureBtn {
  157. width: 292px;
  158. height: 80px;
  159. background: #0a59f7;
  160. border-radius: 76px 76px 76px 76px;
  161. }
  162. }
  163. }
  164. </style>