• ref版的 摄像头 读取 因为id的时候,id不能重复 还要用时间戳,比较麻烦


    <!--
    * @description 摄像头vue版实例
    * @fileName cameraObject.vue
    * @author 彭成刚
    * @date 2019/03/18 10:46:55
    * @version V1.0.0
    !-->
    <template>
      <div id="cameraObject">
        <!-- <div style="505px;float:left;">
    
        </div> -->
        <video ref="video"
               width="480"
               height="320">
        </video>
        <Button type='primary'
                @click="handleCamera"
                icon="md-camera"
                style="margin-bottom:5px;">拍照</Button>
        <canvas ref="canvas"
                width="480"
                height="320"></canvas>
        <!-- <div style="100px;float:left;">
    
        </div>
        <div style="480px;float:left;">
    
        </div> -->
    
      </div>
    </template>
    
    <script>
    export default {
      data () {
        return {
          timer: new Date().getTime().toString(),
          picIsHave: false
        }
      },
    
      components: {},
    
      computed: {},
    
      mounted () {
        if (navigator.mediaDevices.getUserMedia || navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia) {
          // 调用用户媒体设备, 访问摄像头
          this.getUserMedia({ video: {  480, height: 320 } }, this.success, this.error)
        } else {
          alert('不支持访问用户媒体')
        }
    
        // document.getElementById('capture').addEventListener('click', function () {
        //   context.drawImage(video, 0, 0, 480, 320)
        // })
      },
    
      methods: {
        getPicBase64 () {
          // let canvas = document.getElementById('canvas')
          let canvas = this.$refs['canvas']
          let base64 = canvas.toDataURL('image/png', 1)
          let ret = {
            picIsHave: this.picIsHave,
            base64: base64
          }
          return ret
        },
        handleCamera () {
          this.picIsHave = true
          // let canvas = document.getElementById('canvas' + this.timer)
          let canvas = this.$refs['canvas']
          // let video = document.getElementById('video' + this.timer)
          let video = this.$refs['video']
          let context = canvas.getContext('2d')
          context.drawImage(video, 0, 0, 480, 320)
        },
    
        // 访问用户媒体设备的兼容方法
        getUserMedia (constraints, success, error) {
          if (navigator.mediaDevices.getUserMedia) {
            // 最新的标准API
            navigator.mediaDevices.getUserMedia(constraints).then(success).catch(error)
          } else if (navigator.webkitGetUserMedia) {
            // webkit核心浏览器
            navigator.webkitGetUserMedia(constraints, success, error)
          } else if (navigator.mozGetUserMedia) {
            // firfox浏览器
            navigator.mozGetUserMedia(constraints, success, error)
          } else if (navigator.getUserMedia) {
            // 旧版API
            navigator.getUserMedia(constraints, success, error)
          }
        },
        success (stream) {
          // 兼容webkit核心浏览器
          // let CompatibleURL = window.URL || window.webkitURL
          // 将视频流设置为video元素的源
          // console.log('stream', stream)
          // console.info('success')
    
          // video.src = CompatibleURL.createObjectURL(stream);
          // let video = document.getElementById('video' + this.timer)
          let video = this.$refs['video']
          video.srcObject = stream
          video.play()
        },
    
        error (error) {
          console.log(`访问用户媒体设备失败${error.name}, ${error.message}`)
        }
      }
    }
    
    </script>
    <style lang='less'>
    #cameraObjectVideo {
      transition: rotate(90deg);
    }
    </style>
  • 相关阅读:
    数组中出现次数超过一半的数字
    Trie字典树算法
    字符串匹配算法 之 基于DFA(确定性有限自动机)
    实现栈最小元素的min函数
    有关有环链表的问题
    浅谈C中的malloc和free
    undefined reference to 'pthread_create'问题解决
    用两个栈实现队列
    resf规范
    单例模式
  • 原文地址:https://www.cnblogs.com/pengchenggang/p/10553728.html
Copyright © 2020-2023  润新知