-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (50 loc) · 1.51 KB
/
index.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
57
58
59
60
61
62
63
64
65
66
67
68
var Decoder = require('./src/dm_decoder.js');
var BitMatrix = require('./src/dm_bitmatrix.js');
function checkL(bits) {
var i;
// check vertical
for(i=0; i < bits.length; i++) {
if(!bits[i][0]) return false;
}
var last = bits[bits.length-1];
// check horizontal
for(i=0; i < last.length; i++) {
if(!last[i]) return false;
}
return true;
}
function checkTiming(bits) {
var i;
var len = bits[0].length;
var prev = 0;
// check vertical
for(i=bits.length-1; i >= 0; i--) {
if(bits[i][len-1] == prev) return false;
prev = bits[i][len-1];
}
prev = 0;
var first = bits[0];
// check horizontal
for(i=0; i < first.length; i++) {
if(first[i] == prev) return false;
prev = first[i];
}
return true;
}
// Takes a 2D boolean array as argument
// where each array element describes one square/element
// of the datamatrix 2d barcode.
// Returns the decoded data.
// Currently only supports ASCII encoding
module.exports = function(bitArray) {
if(!checkL(bitArray)) {
throw new Error("The solid 'L' part of the code (solid part along two edges) is incorrect or the orientation is wrong.");
}
if(!checkTiming(bitArray)) {
throw new Error("The timing part of the code (alternating pattern along two edges) is wrong)");
}
var bm = new BitMatrix(bitArray);
// console.log(bm.ascii());
var decoder = new Decoder();
return decoder.decode(bm);
}