-
Notifications
You must be signed in to change notification settings - Fork 3
/
barcode.html
159 lines (143 loc) · 4.94 KB
/
barcode.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="manifest" href="manifest.json">
<title>Barcode Detection PWA</title>
</head>
<body>
<video id="player" muted autoplay></video>
<div>
Barcode text : <span id="rawValue"></span>
</div>
<button id="start">Start</button>
<button id="stop">Stop</button>
<script>
if ('serviceWorker' in navigator)
{
window.addEventListener('load', () =>
{
navigator.serviceWorker.register('sw.js')
.then(registration =>
{
console.log(`Service Worker registered! Scope: ${registration.scope}`);
})
.catch(err =>
{
console.error(`Service Worker registration failed: ${err}`);
});
});
}
const video = document.getElementById('player');
let videoTracks;
let localStream = null;
let captureTimer = null;
const fps = 10;
let medias = {
video:
{
width: 320,
height: 240,
facingMode:
{
exact: "environment"
}
},
audio: false
};
const startBtn = document.getElementById('start');
const stopBtn = document.getElementById('stop');
startBtn.onclick = function()
{
document.getElementById(
'rawValue'
).innerHTML = "";
navigator.mediaDevices.getUserMedia(medias)
.then(function(stream)
{
video.srcObject = stream;
localStream = stream;
if (window.BarcodeDetector)
{
const detector = new BarcodeDetector();
captureTimer = setInterval(function()
{
detector.detect(video).then(
function(barcodes)
{
let barcode = null;
for (barcode of barcodes)
{
console.log(
"value : " +
barcode.rawValue
);
console.log(
barcode);
document.getElementById(
'rawValue'
).innerHTML =
convertLink(
barcode.rawValue
);
}
if (barcode) stopVideo();
}).catch(function(err)
{
console.log(err);
});
}, 1000 / fps);
}
else
{
console.error(
'BarcodeDetection is not enable!');
}
startBtn.disabled = true;
stopBtn.disabled = false;
}).catch(function(err)
{
console.log(err);
medias = {
video:
{
width: 320,
height: 240
},
audio: false
};
});
};
function stopVideo()
{
clearInterval(captureTimer);
localStream.getTracks().forEach(function(track)
{
track.stop();
});
localStream = null;
video.srcObject = null;
startBtn.disabled = false;
stopBtn.disabled = true;
};
stopBtn.onclick = function()
{
stopVideo();
};
function convertLink(str)
{
const regexpUrl =
/((h?)(ttps?:\/\/[a-zA-Z0-9.\-_@:/~?%&;=+#',()*!]+))/g; // ']))/;
const regexpLink = function(all, url, h, href)
{
return '<a href="h' + href + '">' + url + '</a>';
}
return str.replace(regexpUrl, regexpLink);
};
// load時にstart状態にする
startBtn.click();
</script>
</body>
</html>