-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
161 lines (137 loc) · 5.79 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
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
160
161
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
font-family: 'Courier New', Courier, monospace;
}
</style>
</head>
<body>
<!-- <div id = "sourceSelectPanel">
<select id = "sourceSelect"></select>
</div> -->
<div>
<video id="video" width="300" style="border: 1px solid gray"></video>
</div>
<button id="startButton">Start</button>
<button id="stopButton">Stop</button>
<button id="changeButton">ChangeCamera</button>
<br />
<br />
<button id="clearButton">Clear Image</button>
<input type="file" id="myFile" name="filename" accept="image/png, image/jpeg">
<div id="preview">
</div>
<br />
<button id="decodeButton">Decode Image</button>
<br />
<br />
<div>Camera Decode :</div>
<div id="result" style="margin-top: 20px;"></div>
<div style="margin-top: 40px;">Image Decode :</div>
<div id="resultImg" style="margin-top: 20px;"></div>
<script src="vendor/parse-usdl/parser.js"></script>
<script src="vendor/zxing/index.js"></script>
<script>
window.addEventListener('load', function () {
const video = document.querySelector("#video");
const codeReader = new ZXing.BrowserMultiFormatReader()
console.log('ZXing PDF417 code reader initialized')
document.getElementById('myFile').addEventListener('change', () => {
var files = document.getElementById('myFile').files;
if (files.length != 0) {
while (document.getElementById('preview').firstChild) {
document.getElementById('preview').removeChild(document.getElementById('preview').firstChild);
}
const image = document.createElement('img');
image.src = URL.createObjectURL(files[0]);
image.width = 300;
document.getElementById('preview').appendChild(image);
}
});
var streamObj;
document.getElementById('startButton').addEventListener('click', () => {
startCam();
});
function startCam() {
if (navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({ video: { facingMode: CameraMode } }).then((stream) => {
streamObj = stream;
codeReader.decodeFromStream(stream, video, (result, err) => {
if (result) {
console.log(result);
if(result.format == 10){
console.log(parseCode128(result.text));
document.getElementById('result').innerText = JSON.stringify(parseCode128(result.text), null, 2);
}
else{
document.getElementById('result').innerText = result;
}
}
if (err && !(err instanceof ZXing.NotFoundException)) {
console.error(err)
document.getElementById('result').textContent = err
}
});
})
.catch((err) => {
console.log("Something went wrong!");
});
}
}
document.getElementById('stopButton').addEventListener('click', () => {
stopCam();
});
function stopCam() {
streamObj.getTracks().forEach((track) => {
if (track.readyState == 'live' && track.kind === 'video') {
track.stop();
}
});
}
var counter = 0;
var CameraMode = 'user';
document.getElementById('changeButton').addEventListener('click', () => {
if (streamObj) {
stopCam();
}
counter = (counter + 1) % 2;
console.log(counter);
switch (counter) {
case 0:
CameraMode = 'user';
break;
case 1:
CameraMode = 'environment';
break;
}
startCam();
});
document.getElementById('clearButton').addEventListener('click', () => {
while (document.getElementById('preview').firstChild) {
document.getElementById('preview').removeChild(document.getElementById('preview').firstChild);
}
});
document.getElementById('decodeButton').addEventListener('click', () => {
if (document.getElementById('preview').firstChild.nodeType == Node.ELEMENT_NODE) {
var img = document.getElementById('preview').firstChild.cloneNode(true);
codeReader.decodeFromImage(img)
.then(result => {
console.log(result);
document.getElementById('resultImg').textContent = result.text;
})
.catch(err => {
console.error(err);
document.getElementById('resultImg').textContent = err;
});
}
});
});
</script>
</body>
</html>