-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
95 lines (90 loc) · 3.75 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html5-qrcode/2.3.8/html5-qrcode.min.js"></script>
</head>
<body>
<div>
这是测试内容
</div>
<div style="width: 500px; height: 200;" id="reader"></div>
<button onclick="start()"> 开扫 </button>
<button onclick="stop()"> 结束 </button>
<div id="content"></div>
<div id="content2"></div>
<script>
const content = document.getElementById("content");
const content2 = document.getElementById("content2");
function onScanSuccess(decodedText, decodedResult) {
// Handle on success condition with the decoded text or result.
console.log(`Scan result: ${decodedText}`, decodedResult);
}
// var html5QrcodeScanner = new Html5QrcodeScanner(
// "reader", { fps: 10, qrbox: 250 });
// html5QrcodeScanner.render(onScanSuccess);
let html5QrCode = new Html5Qrcode("reader")
const start = () => {
html5QrCode.start(
// environment后置摄像头 user前置摄像头
{ facingMode: "environment" },
{
fps: 10, // 可选,每秒帧扫描二维码
qrbox: { width: "100%", height: 250 } // 可选,如果你想要有界框UI
// aspectRatio: 1.777778 // 可选,视频馈送需要的纵横比,(4:3--1.333334, 16:9--1.777778, 1:1--1.0)传递错误的纵横比会导致视频不显示
},
(decodedText, decodedResult) => {
alert(`${decodedText},${JSON.stringify(decodedResult)}`)
// do something when code is read
content.innerHTML = decodedText
content2.innerHTML = decodedResult
console.log('decodedText', decodedText)
console.log('decodedResult', decodedResult)
// this.$emit("goBack", decodedText)
}
)
.catch((err) => {
alert(`出错了 ${err}`)
console.log('扫码错误信息', err)
// 错误信息处理仅供参考,具体情况看输出!!!
if (typeof err == 'string') {
// this.$toast(err)
} else {
// if (err.name == 'NotAllowedError') return this.$toast("您需要授予相机访问权限")
// if (err.name == 'NotFoundError') return this.$toast('这个设备上没有摄像头')
// if (err.name == 'NotSupportedError') return this.$toast('摄像头访问只支持在安全的上下文中,如https或localhost')
// if (err.name == 'NotReadableError') return this.$toast('相机被占用')
// if (err.name == 'OverconstrainedError') return this.$toast('安装摄像头不合适')
// if (err.name == 'StreamApiNotSupportedError') return this.$toast('此浏览器不支持流API')
}
})
}
const getCameras = () => {
Html5Qrcode.getCameras()
.then((devices) => {
if (devices && devices.length) {
html5QrCode = new Html5Qrcode("reader")
start()
}
})
.catch((err) => {
// handle err
html5QrCode = new Html5Qrcode("reader")
// this.$toast('您需要授予相机访问权限')
})
}
const stop = () => {
html5QrCode.stop().then((ignore) => {
// QR Code scanning is stopped.
console.log("QR Code scanning stopped.")
})
.catch((err) => {
// Stop failed, handle it.
console.log("Unable to stop scanning.")
})
}
</script>
</body>
</html>