liding
3 天以前 6526713bc148013b4a0828bd3d1001db75919021
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<template>
  <div class="qrcode">
    <div id="reader"></div>
    <div class="tipText">将二维码置于此区域</div>
  </div>
</template>
 
<script>
import { Html5Qrcode } from "html5-qrcode";
export default {
  name: 'Html5Qrcode',
  data () {
    return {
      html5QrCode: null
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.getCameras()
    })
  },
 
  beforeDestroy() {
    this.stop();
  },
  methods: {
    getCameras() {
      Html5Qrcode.getCameras()
        .then((devices) => {
          if (devices && devices.length) {
            console.log("Html5Qrcode", devices);
            this.html5QrCode = new Html5Qrcode("reader");
            // console.log(11111111)
            // console.log(11111111)
            // console.log(11111111)
            // console.log(11111111)
            // console.log(11111111)
            // console.log(11111111)
            // console.log(this.html5QrCode)
            // console.log(JSON.stringify(this.html5QrCode))
            // console.log(11111111)
            // console.log(11111111)
            // console.log(11111111)
            // console.log(11111111)
            // console.log(11111111)
            // console.log(11111111)
            this.start();
          }
        })
        .catch((err) => {
          // handle err
          console.log(err);
          console.log(JSON.stringify(err));
          this.html5QrCode = new Html5Qrcode("reader");
          let error = "ERROR: 没有授权啊!您需要授予相机访问权限~";
          this.$emit("err", error);
        });
    },
    start() {
      //environment后置 user前置
      this.html5QrCode
        .start(
          { facingMode: "environment" },
          {
            fps: 30,
            qrbox: { width: 250, height: 250 },
          },
          (decodedText, decodedResult) => {
            console.log("恭喜你~扫描到了结果", decodedText, decodedResult);
            this.$emit("ok", decodedText);
          }
        )
        .catch((err) => {
          console.log(JSON.stringify(err));
          this.$emit("err", err);
        });
    },
    stop() {
      this.html5QrCode
        .stop()
        .then((ignore) => {
          console.log(ignore)
          // QR Code scanning is stopped.
          console.log("QR Code scanning stopped.");
        })
        .catch((err) => {
          console.log(err)
          // Stop failed, handle it.
          console.log("Unable to stop scanning.");
        });
    },
  },
};
</script>
 
<style lang="scss" scoped>
.qrcode {
  position: relative;
  height: 100%;
  width: 100%;
  background: rgba($color: #000000, $alpha: 0.48);
}
 
#reader {
  top: 50%;
  left: 0;
  transform: translateY(-50%);
}
 
.tipText {
  position: absolute;
  left: 50%;
  top: calc(50% + 165px);
  transform: translate(-50%, -50%);
  width: 100%;
  height: 20px;
  color: white;
  font-size: 15px;
  z-index: 999;
  text-align: center;
}
</style>