-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
75 lines (70 loc) · 2.23 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
<html>
<head>
<title>Zxing.js demo</title>
<style>
#video { display: none; }
</style>
</head>
<body>
<video id="video"></video>
<canvas id="canvas"></canvas>
<div id="not-supported"></div>
<div id="info"></div>
<script src="dist/zxing.js" type="text/javascript"></script>
<script>
var zxing = new Zxing
var video = document.querySelector('#video')
var canvas = document.querySelector('#canvas')
var noSupp = document.querySelector('#not-supported')
var info = document.querySelector('#info')
var ctx = canvas.getContext('2d')
navigator.getUserMedia = navigator.getUserMedia ||
navigator.mozGetUserMedia ||
navigator.webkitGetUserMedia ||
navigator.msGetUserMedia
const constraints = {
advanced: [{
facingMode: "environment"
}]
};
navigator.getUserMedia({
video: constraints,
}, function(stream) {
video.src = window.URL.createObjectURL(stream)
}, function(err) {
noSupp.innerHTML = 'getUserMedia not supported in this browser: ' + err
})
zxing.onReady(function() {
if (!video.videoWidth) return
canvas.setAttribute('width', video.videoWidth)
canvas.setAttribute('height', video.videoHeight)
ctx.drawImage(video, 0, 0)
zxing.decode(ctx)
.then(function(result) {
info.innerHTML = ''
ctx.strokeStyle = '#39ff14'
ctx.lineWidth = 4
result.forEach(function(res) {
info.innerHTML += res.code + ', Format: ' + res.format
info.innerHTML += ', Time: ' + res.time + 'ms\n'
drawPoints(ctx, res.points)
})
})
.catch(function(error) {})
})
function drawPoints(ctx, points) {
points.forEach(function(point, i) {
if (i === 0) {
ctx.beginPath()
ctx.moveTo(point.x, point.y)
} else {
ctx.lineTo(point.x, point.y)
ctx.stroke()
ctx.beginPath()
ctx.moveTo(point.x, point.y)
}
})
}
</script>
</body>
</html>