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
<template>
<div>
<van-popup v-model="show" position="right" :style="{ height: '100%',width: '100%' }">
<div class="all">
<video style="display: none" ref="video"></video>
<canvas style="width: 90vw; margin-top: 20vh;height: 40vh" id="canvas"></canvas>
<canvas style="display: none" id="2d"></canvas>
</div>
</van-popup>
</div>
</template>
<script>
import jsQR from "../../utils/jsQR";
export default {
name: 'analyzeQRCode',
props: {
show: {
type: Boolean,
default: false
}
},
data() {
return {
streams: null,
}
},
mounted() {
this.$nextTick(() => {
this.anaQR()
})
},
methods: {
//解析二维码
anaQR() {
// if (this.streams) {
// this.streams.getTracks().forEach((track) => track.stop());
// }
// video.srcObject = null;
let that = this
var video = document.createElement("video");
var canvasElement = document.getElementById("canvas");
var canvas = canvasElement.getContext("2d");
console.log(navigator.mediaDevices);
// 尝试打开手机上安装后置摄像头
navigator.mediaDevices
.getUserMedia({
video: {facingMode: "environment"},
})
.then((stream) => {
this.streams = stream;
video.srcObject = stream;
// 阻止IOS视频全屏
video.setAttribute("playsinline", true);
video.play();
requestAnimationFrame(tick);
});
function tick() {
if (video.readyState === video.HAVE_ENOUGH_DATA) {
canvasElement.hidden = false;
canvasElement.height = video.videoHeight;
canvasElement.width = video.videoWidth;
canvas.drawImage(video, 0, 0, canvasElement.width, canvasElement.height);
var imageData = canvas.getImageData(
0,
0,
canvasElement.width,
canvasElement.height
);
// QR码解析
var code = jsQR(
imageData.data, // 图像数据
imageData.width, // 宽度
imageData.height, // 高度
{
inversionAttempts: "dontInvert",
}
);
if (code) {
console.log(code.data);
//输出解析后的二维码
getQR(code.data)
}
}
requestAnimationFrame(tick);
}
function getQR(val) {
that.$emit("getQRCode", val)
}
},
}
}
</script>
<style lang="less" scoped>
.all {
background: black;
height: 100vh;
display: flex;
justify-content: space-around;
}
</style>