-
Notifications
You must be signed in to change notification settings - Fork 7
/
Reader.html
157 lines (140 loc) · 4.68 KB
/
Reader.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
<!DOCTYPE html>
<meta charset=utf-8>
<html lang="en">
<head>
<title>BarcodeReader</title>
<style>
canvas {
display: none;
}
</style>
<script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.js"></script>
</head>
<body style="background-color: black; ">
<video id="video"></video>
<button id="startbutton">Scan barcode</button>
<canvas id="canvas"></canvas>
<img src="Sample-images/EAN-13/23.JPG" id="photo" alt="photo">
<p id="textbit" style="font-family:arial;color:red;font-size:20px;"></p>
<script type="text/javascript">
var sound = new Audio("barcode.wav");
(function() {
var streaming = false,
Result = document.querySelector("#textbit"),
video = document.querySelector('#video'),
canvas = document.querySelector('#canvas'),
photo = document.querySelector('#photo'),
startbutton = document.querySelector('#startbutton'),
width = 400,
height = 400;
navigator.getMedia = ( navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);
navigator.getMedia(
{
video: true,
audio: false
},
function(stream) {
if (navigator.mozGetUserMedia) {
video.mozSrcObject = stream;
} else {
var vendorURL = window.URL || window.webkitURL;
video.src = vendorURL.createObjectURL(stream);
}
video.play();
},
function(err) {
console.log("An error occured! " + err);
}
);
video.addEventListener('canplay', function(ev){
if (!streaming) {
//height = video.videoHeight / (video.videoWidth/width);
video.setAttribute('width', width);
video.setAttribute('height', height);
canvas.setAttribute('width', width);
canvas.setAttribute('height', height);
streaming = true;
}
}, false);
function takepicture() {
canvas.width = width;
canvas.height = height;
canvas.getContext('2d').drawImage(video, 0, 0, width, height);
var data = canvas.toDataURL('image/png');
// photo.setAttribute('src', data);
photo.setAttribute('src', data);
var resultArray = [];
ctx = canvas.getContext('2d');
var workerCount = 0;
function receiveMessage(e) {
if(e.data.success === "log") {
console.log(e.data.result);
return;
}
workerCount--;
if(e.data.success){
var tempArray = e.data.result;
for(var i = 0; i < tempArray.length; i++) {
if(resultArray.indexOf(tempArray[i]) == -1) {
resultArray.push(tempArray[i]);
}
}
Result.innerHTML=resultArray.join("<br />");
sound.play();
}else{
if(resultArray.length === 0 && workerCount === 0) {
Result.innerHTML="Decoding failed.";
}
}
}
var DecodeWorker = new Worker("DecoderWorker.js");
var RightWorker = new Worker("DecoderWorker.js");
var LeftWorker = new Worker("DecoderWorker.js");
var FlipWorker = new Worker("DecoderWorker.js");
DecodeWorker.onmessage = receiveMessage;
RightWorker.onmessage = receiveMessage;
LeftWorker.onmessage = receiveMessage;
FlipWorker.onmessage = receiveMessage;
DecodeBar();
//setInterval(function(){takepicture()}, 3000);
//function drawGraphics() {
// ctxg.strokeStyle = config.strokeColor;
// ctxg.lineWidth = 3;
// ctxg.beginPath();
// ctxg.moveTo(dimensions.start, dimensions.height * 0.5);
// ctxg.lineTo(dimensions.end, dimensions.height * 0.5);
// ctxg.stroke();
//}
function DecodeBar(){
//photo.onload = function(){
ctx.drawImage(photo,0,0,canvas.width,canvas.height);
processImage();
//}
}
function processImage() {
resultArray = [];
workerCount = 4;
Result.innerHTML="";
DecodeWorker.postMessage({pixels: ctx.getImageData(0,0,canvas.width,canvas.height).data, width: canvas.width, height: canvas.height, cmd: "normal"});
RightWorker.postMessage({pixels: ctx.getImageData(0,0,canvas.width,canvas.height).data, width: canvas.width, height: canvas.height, cmd: "right"});
LeftWorker.postMessage({pixels: ctx.getImageData(0,0,canvas.width,canvas.height).data, width: canvas.width, height: canvas.height, cmd: "left"});
FlipWorker.postMessage({pixels: ctx.getImageData(0,0,canvas.width,canvas.height).data, width: canvas.width, height: canvas.height, cmd: "flip"});
}
}
startbutton.addEventListener('click', function(ev){
takepicture();
ev.preventDefault();
}, false);
})();
setTimeout(function() {
$('#startbutton').each(function(){
var button = $(this);
setInterval(function() {button.click();}, 5000);
});
}, 5000);
</script>
</body>
</html>