media.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <template>
  2. <div id="">
  3. <el-dialog
  4. width="600px"
  5. class="take-photo"
  6. :visible.sync="takeVideoModal"
  7. :close-on-click-modal="false"
  8. :close-on-press-escape="false"
  9. :show-close="false"
  10. >
  11. <h1 class="h_t">人脸录像抽查检测</h1>
  12. <div class="content">
  13. <video ref="videoPlayer11"
  14. id="video1"
  15. v-show="status === 3"
  16. class="video"
  17. autoplay
  18. controls
  19. :src="url"
  20. ></video>
  21. <video v-show="status !== 3" id="video2" class="video" autoplay></video>
  22. </div>
  23. <div class="footer">
  24. <el-button
  25. :type="status === 3 ? 'success' : 'primary'"
  26. class="pos"
  27. @click="luzhi"
  28. :disabled="status === 2"
  29. >{{
  30. status === 1 ? "开始录制3秒" : status === 2 ? "录制中" : "提交"
  31. }}</el-button
  32. >
  33. <el-button @click="again" v-if="status === 3">重录</el-button>
  34. </div>
  35. </el-dialog>
  36. </div>
  37. </template>
  38. <script>
  39. export default {
  40. data() {
  41. return {
  42. status: 1, //1未录制 2录制中 3录制完成
  43. takeVideoModal: false,
  44. mediaRecorder: null,
  45. videoblob:null,
  46. url: null,
  47. faceUrl:null,
  48. };
  49. },
  50. methods: {
  51. openBoxs() {
  52. this.takeVideoModal = true;
  53. this.status=1;
  54. this.mediaRecorder=null;
  55. this.videoblob=null;
  56. this.url=null;
  57. this.faceUrl=null;
  58. this.$nextTick(() => {
  59. const video = document.getElementById("video2");
  60. navigator.mediaDevices
  61. .getUserMedia({ video: true })
  62. .then((stream) => {
  63. video.srcObject = stream;
  64. this.mediaRecorder = new MediaRecorder(stream);
  65. })
  66. .catch(function (err) {
  67. console.log("err", err);
  68. /* 处理error */
  69. });
  70. });
  71. },
  72. again() {
  73. this.url = null;
  74. this.status = 1;
  75. },
  76. luzhi() {
  77. if (this.status === 3) {
  78. //完成提交
  79. this.loading = true;
  80. this.$emit("returnParameterVideo", this.videoblob,this.faceUrl);
  81. this.takeVideoModal=false;
  82. return;
  83. }
  84. this.onCatchPhoto();
  85. this.mediaRecorder.start();
  86. this.status = 2;
  87. setTimeout(() => {
  88. this.stop();
  89. this.status = 3;
  90. }, 3500);
  91. },
  92. //拍照
  93. onCatchPhoto() {
  94. const canvas = document.createElement("canvas");
  95. const video = document.getElementById("video1");
  96. canvas.width = 500;
  97. canvas.height = 375;
  98. const context = canvas.getContext("2d");
  99. context.drawImage(video, 0, 0, 500, 375);
  100. this.faceUrl = canvas.toDataURL("image/png");
  101. // console.log(this.faceUrl,'faceUrl')
  102. },
  103. stop() {
  104. this.mediaRecorder.stop();
  105. this.mediaRecorder.ondataavailable = (event) => {
  106. this.$message.success("录制完成");
  107. console.log(event.data,'data')
  108. // 尝试转换为mp4类型
  109. const blob = event.data.slice(0, event.data.size, "video/mp4");
  110. this.url = URL.createObjectURL(blob);
  111. this.videoblob =blob;
  112. //console.log(event.data,'url')
  113. };
  114. },
  115. },
  116. };
  117. </script>
  118. <style lang="scss" scoped>
  119. .video {
  120. width: 500px;
  121. height: 375px;
  122. border: 1px solid #333;
  123. }
  124. .h_t {
  125. text-align: center;
  126. font-size: 34px;
  127. color: #333;
  128. margin-bottom: 14px;
  129. }
  130. .content {
  131. text-align: center;
  132. margin-bottom: 20px;
  133. }
  134. .footer {
  135. text-align: center;
  136. }
  137. </style>