当前位置:   article > 正文

vue PC端调用多个摄像头笔记_在vue页面引入大华摄像

在vue页面引入大华摄像

1、谷歌浏览器设置(设置)

 2、代码

 

  1. <template>
  2. <div class="home">
  3. <div class="top" >
  4. <div class="left" v-loading="loading">
  5. <h1>摄像头1</h1>
  6. <video ref="video1" width="640" height="480" autoplay style="background-color: black;"></video>
  7. </div>
  8. <div class="right" v-loading="loading">
  9. <h1>摄像头2</h1>
  10. <video ref="video2" width="640" height="480" autoplay style="background-color: black;"></video>
  11. </div>
  12. </div>
  13. <div class="bottom">
  14. <div class="left">
  15. <h1>照片</h1>
  16. <!--canvas截取流-->
  17. <canvas ref="canvas1" width="640" height="480" style="background-color: black;"></canvas>
  18. </div>
  19. <div class="right">
  20. <h1>照片</h1>
  21. <!--canvas截取流-->
  22. <canvas ref="canvas2" width="640" height="480" style="background-color: black;"></canvas>
  23. </div>
  24. </div>
  25. <div class="btns">
  26. <el-button type="primary" @click="callCamera" style="margin-right: 10px"
  27. >开启摄像头</el-button
  28. >
  29. <!--关闭摄像头-->
  30. <el-button type="primary" @click="closeCamera">关闭摄像头</el-button>
  31. <el-button type="primary" @click="photograph">拍照</el-button>
  32. </div>
  33. </div>
  34. </template>
  35. <script>
  36. export default {
  37. data() {
  38. return {
  39. loading:false,
  40. devicesIds: [], //摄像头数组
  41. };
  42. },
  43. created() {
  44. this.init();
  45. },
  46. methods: {
  47. //
  48. async init() {
  49. try {
  50. let devices = await navigator.mediaDevices.enumerateDevices();
  51. console.log(devices, "设备");
  52. devices.forEach((device) => {
  53. if (device.kind == "videoinput") {
  54. this.devicesIds.push(device.deviceId);
  55. }
  56. });
  57. console.log(this.devicesIds, "摄像头");
  58. } catch (error) {}
  59. },
  60. // 调用摄像头
  61. callCamera() {
  62. this.loading=true
  63. //console.log(this.devicesIds)
  64. this.devicesIds.forEach((item,index)=>{
  65. // H5调用电脑摄像头API
  66. navigator.mediaDevices
  67. .getUserMedia({
  68. video: {
  69. deviceId:{exact:item},
  70. },
  71. })
  72. .then((success) => {
  73. // console.log(success,'流')
  74. // 摄像头开启成功
  75. this.$refs[`video${index+1}`].srcObject = success;
  76. // 实时拍照效果
  77. this.$refs[`video${index+1}`].play();
  78. this.loading=false
  79. })
  80. .catch((error) => {
  81. console.error("摄像头开启失败,请检查摄像头是否可用!");
  82. });
  83. })
  84. },
  85. // 拍照
  86. photograph() {
  87. this.devicesIds.forEach((item,index)=>{
  88. let ctx = this.$refs[`canvas${index+1}`].getContext("2d");
  89. // 把当前视频帧内容渲染到canvas上
  90. ctx.drawImage(this.$refs[`video${index+1}`], 0, 0, 640, 480);
  91. // 转base64格式、图片格式转换、图片质量压缩---支持两种格式image/jpeg+image/png
  92. let imgBase64 = this.$refs[`canvas${index+1}`].toDataURL("image/jpeg", 0.7);
  93. /**------------到这里为止,就拿到了base64位置的地址,后面是下载功能----------*/
  94. // 由字节转换为KB 判断大小
  95. let str = imgBase64.replace("data:image/jpeg;base64,", "");
  96. let strLength = str.length;
  97. let fileLength = parseInt(strLength - (strLength / 8) * 2); // 图片尺寸 用于判断
  98. let size = (fileLength / 1024).toFixed(2);
  99. console.log(size); // 上传拍照信息 调用接口上传图片 .........
  100. // 保存到本地
  101. let ADOM = document.createElement("a");
  102. ADOM.href = imgBase64;
  103. ADOM.download = new Date().getTime() + ".jpeg";
  104. ADOM.click();
  105. })
  106. },
  107. // 关闭摄像头
  108. closeCamera() {
  109. if (!this.$refs["video"].srcObject) return;
  110. let stream = this.$refs["video"].srcObject;
  111. let tracks = stream.getTracks();
  112. tracks.forEach((track) => {
  113. track.stop();
  114. });
  115. this.$refs["video"].srcObject = null;
  116. },
  117. },
  118. };
  119. </script>
  120. <style scoped lang='scss'>
  121. .home{
  122. width: 100%;
  123. background: #ccc;
  124. .top,.bottom{
  125. display: flex;
  126. align-items: center;
  127. justify-content: space-around;
  128. text-align: center;
  129. }
  130. .btns{
  131. padding:40px 0;
  132. display: flex;
  133. align-items: center;
  134. justify-content: center;
  135. }
  136. }
  137. </style>

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/714842
推荐阅读
相关标签
  

闽ICP备14008679号