forked from tonylampada/barcodescanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.js
56 lines (56 loc) · 1.57 KB
/
decoder.js
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
var ctx
var canvas
var workerCount = 0;
var ResultOfDecoding = document.getElementById("dec")
var gimgid;
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]);
}
}
ResultOfDecoding.innerHTML=resultArray.join("<br />");
alert(ResultOfDecoding.innerHTML)
workerCount = 0;
}else {
if(workerCount == 1) {
FlipWorker.postMessage({pixels: ctx.getImageData(0,0,canvas.width,canvas.height).data, cmd: "flip"});
}
}
if(workerCount == 0){
if(resultArray.length === 0) {
ResultOfDecoding.innerHTML="Decoding failed.";
setTimeout(function(){
take_snapshot()
}, 500);
}else {
ResultOfDecoding.innerHTML=resultArray.join("<br />");
}
}
}
var DecodeWorker = new Worker("DecoderWorker.js");
var FlipWorker = new Worker("DecoderWorker.js");
DecodeWorker.onmessage = receiveMessage;
FlipWorker.onmessage = receiveMessage;
var resultArray = [];
function Decode(imgid) {
gimgid = imgid
var img = document.getElementById(imgid);
canvas = document.createElement("canvas");
canvas.width = 640;
canvas.height = 480;
ctx = canvas.getContext("2d");
if(workerCount > 0) return;
workerCount = 2;
ResultOfDecoding.innerHTML='decoding...';
resultArray = [];
ctx.drawImage(img,0,0,canvas.width, canvas.height);
DecodeWorker.postMessage({pixels: ctx.getImageData(0,0,canvas.width,canvas.height).data, cmd: "normal"});
}