zoukankan      html  css  js  c++  java
  • vue 实现 扫二维码 功能

    前段时间一直在研究,如何通过 vue 调用 相机 实现 扫一扫的功能,但是查看文档发现,需要获取 getUserMedia 的属性值,但存在兼容性问题。

    退而求其次,通过 h5plus 来实现。

    1.QrScanner.vue

    <!-- 扫描二维码 20180109 -->
    <template>
      <div>
        <!-- 内容部分 -->
        <video id="qr-vedio" class="v" autoplay=""></video>
        <canvas id="qr-canvas" width="800" height="600" style=" 800px; height: 600px;display:none;"></canvas>
        <p v-show="result != ''">{{result}}</p>
        <p v-show="errorMes != ''">{{errorMes}}</p>
      </div>
    </template>
    
    <script>
      export default {
        props: {
          //
        },
        data () {
          return {
            vedio: '',
            canvas: '',
            context: '',
            stopScan: null,
            errorMes: '',
            result: ''
          }
        },
        mounted(){
          console.log(1);
          var _that = this;
          window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
          navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
    
          this.vedio = document.getElementById('qr-vedio');
          this.canvas = document.getElementById('qr-canvas');
          this.context = this.canvas.getContext('2d');
    
          console.log(2);
          // Call the getUserMedia method with our callback functions
          if (navigator.getUserMedia) {
            console.log(3);
            var videoSource = [];
            navigator.mediaDevices.enumerateDevices().then((function (sourceInfos) {
              var i;
              for (i = 0; i != sourceInfos.length; ++i) {
                var sourceInfo = sourceInfos[i];
                if (sourceInfo.kind === 'videoinput' && sourceInfo.label.indexOf('back') != -1) {
                  videoSource.push(sourceInfo.deviceId);
                }
              }
              var successCallback = function (stream) {
                _that.vedio.src = (window.URL && window.URL.createObjectURL(stream)) || stream;
                window.localMediaStream = stream;
                _that.vedio.addEventListener("loadstart", function () {
                  _that.vedio.play();
                }, false);
                _that.stopScan = setInterval(_that.scan, 500);
              }
    
              navigator.getUserMedia({
                video: {
                  optional: [{ sourceId: videoSource[0] }]
                }
              }, successCallback, function (e) {
                console.log(e);
              });
            }));
    
          } else {
            this.errorMes = 'Native web camera streaming (getUserMedia) not supported in this browser.';
          }
    
          qrcode.callback = function (data) {
            _that.result = data;
            console.log(data)
            if (window.localMediaStream && window.localMediaStream.stop) {
              window.localMediaStream.stop();
            }
            if (_that.stopScan) {
              clearInterval(_that.stopScan);
            }
          };
        },
        methods: {
          scan() {
            if (window.localMediaStream) {
              this.context.drawImage(this.vedio, 0, 0, 100, 100);
            }
            try {
              qrcode.decode();
            } catch (e) {
              console.log('decode has error');
            }
          }
        }
      }
    </script>
    
    <style lang="less" scoped>
      .v {
         320px;
        height: 240px;
      }
    
      #qr-canvas {
         800px;
        height: 800px;
      }
    </style>

    2.效果图

  • 相关阅读:
    python-day49--前端 css-层叠样式表
    python-day49--前端 html
    python-day48--mysql之视图、触发器、事务、存储过程、函数
    python-day47--pymysql模块
    python-day47--mysql数据备份与恢复
    python-day46--前端基础之html
    python-day45--mysql索引
    window系统下远程部署Tomcat
    tomcat下部署应用helloworld
    tomcat配置文件context.xml和server.xml分析
  • 原文地址:https://www.cnblogs.com/crazycode2/p/8241030.html
Copyright © 2011-2022 走看看