@sythos/js_barcode_universal 1.5.8 → 1.5.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +5 -4
- package/NOTICE.md +2 -2
- package/README.md +17 -13
- package/bundle/sythos-barcode.esm.js +11658 -11244
- package/bundle/sythos-barcode.js +11658 -11244
- package/package.json +10 -3
- package/src/index.d.ts +1 -1
- package/src/index.js +508 -516
- package/src/js/aztec/decoder.js +208 -199
- package/src/js/aztec/detector.js +153 -126
- package/src/js/aztec/encoder.js +172 -153
- package/src/js/aztec/high-level.js +106 -101
- package/src/js/aztec/index.js +1 -12
- package/src/js/aztec/tables.js +106 -120
- package/src/js/aztecrune/decoder.js +76 -85
- package/src/js/aztecrune/detector.js +107 -93
- package/src/js/aztecrune/encoder.js +50 -68
- package/src/js/aztecrune/index.js +1 -16
- package/src/js/aztecrune/tables.js +59 -54
- package/src/js/compactpdf417/decoder.js +69 -74
- package/src/js/compactpdf417/detector.js +74 -75
- package/src/js/compactpdf417/encoder.js +68 -81
- package/src/js/compactpdf417/index.js +1 -13
- package/src/js/compactpdf417/tables.js +71 -80
- package/src/js/core/bit-buffer.js +106 -122
- package/src/js/core/bit-matrix.js +196 -194
- package/src/js/core/errors.js +13 -15
- package/src/js/core/galois-field.js +119 -133
- package/src/js/core/index.js +2 -19
- package/src/js/core/reed-solomon.js +195 -204
- package/src/js/databar/codec.js +93 -108
- package/src/js/databar/decoder.js +149 -140
- package/src/js/databar/encoder.js +47 -55
- package/src/js/databar/gs1.js +122 -123
- package/src/js/databar/index.js +4 -36
- package/src/js/databar/patterns.js +81 -85
- package/src/js/databar/tables.js +51 -52
- package/src/js/datamatrix/decoder.js +216 -182
- package/src/js/datamatrix/detector.js +161 -132
- package/src/js/datamatrix/encoder.js +192 -133
- package/src/js/datamatrix/index.js +1 -9
- package/src/js/datamatrix/tables.js +68 -69
- package/src/js/frameqr/decoder.js +102 -125
- package/src/js/frameqr/detector.js +101 -94
- package/src/js/frameqr/encoder.js +67 -80
- package/src/js/frameqr/index.js +1 -10
- package/src/js/frameqr/tables.js +145 -163
- package/src/js/image/binarizer.js +146 -162
- package/src/js/image/grid-sampler.js +63 -79
- package/src/js/image/index.js +0 -2
- package/src/js/image/luminance.js +129 -140
- package/src/js/image/perspective.js +112 -149
- package/src/js/micropdf417/compaction.js +64 -65
- package/src/js/micropdf417/decoder.js +106 -109
- package/src/js/micropdf417/detector.js +73 -70
- package/src/js/micropdf417/encoder.js +129 -154
- package/src/js/micropdf417/error-correction.js +6 -11
- package/src/js/micropdf417/index.js +2 -16
- package/src/js/micropdf417/tables.js +112 -99
- package/src/js/microqr/decoder.js +177 -182
- package/src/js/microqr/detector.js +240 -256
- package/src/js/microqr/encoder.js +239 -194
- package/src/js/microqr/index.js +0 -2
- package/src/js/microqr/tables.js +208 -200
- package/src/js/oned/addons.js +167 -191
- package/src/js/oned/index.js +27 -58
- package/src/js/oned/patterns.js +194 -213
- package/src/js/oned/reader.js +1003 -984
- package/src/js/oned/writers.js +411 -466
- package/src/js/pdf417/compaction.js +315 -222
- package/src/js/pdf417/decoder.js +71 -36
- package/src/js/pdf417/detector.js +377 -361
- package/src/js/pdf417/encoder.js +57 -46
- package/src/js/pdf417/error-correction.js +5 -8
- package/src/js/pdf417/tables.js +248 -250
- package/src/js/qr/decoder.js +311 -347
- package/src/js/qr/detector.js +378 -395
- package/src/js/qr/encoder.js +514 -599
- package/src/js/qr/index.js +0 -2
- package/src/js/qr/tables.js +375 -422
- package/src/js/render/image-data.js +57 -62
- package/src/js/render/index.js +21 -23
- package/src/js/render/options.js +82 -90
- package/src/js/render/png.js +144 -169
- package/src/js/render/svg.js +41 -44
- package/src/js/render/webgl.js +103 -109
- package/src/js/render/webgpu.js +205 -217
- package/src/js/rmqr/decoder.js +161 -53
- package/src/js/rmqr/detector.js +73 -41
- package/src/js/rmqr/encoder.js +253 -104
- package/src/js/rmqr/index.js +1 -5
- package/src/js/rmqr/tables.js +108 -85
- package/src/ts/aztec/decoder.ts +317 -0
- package/src/ts/aztec/detector.ts +224 -0
- package/src/ts/aztec/encoder.ts +257 -0
- package/src/ts/aztec/high-level.ts +211 -0
- package/src/ts/aztec/index.ts +45 -0
- package/src/ts/aztec/tables.ts +210 -0
- package/src/ts/aztecrune/decoder.ts +155 -0
- package/src/ts/aztecrune/detector.ts +166 -0
- package/src/ts/aztecrune/encoder.ts +121 -0
- package/src/ts/aztecrune/index.ts +49 -0
- package/src/ts/aztecrune/tables.ts +137 -0
- package/src/ts/compactpdf417/decoder.ts +128 -0
- package/src/ts/compactpdf417/detector.ts +139 -0
- package/src/ts/compactpdf417/encoder.ts +140 -0
- package/src/ts/compactpdf417/index.ts +17 -0
- package/src/ts/compactpdf417/tables.ts +173 -0
- package/src/ts/core/bit-buffer.ts +174 -0
- package/src/ts/core/bit-matrix.ts +241 -0
- package/src/ts/core/errors.ts +61 -0
- package/src/ts/core/galois-field.ts +207 -0
- package/src/ts/core/index.ts +56 -0
- package/src/ts/core/reed-solomon.ts +327 -0
- package/src/ts/databar/codec.ts +181 -0
- package/src/ts/databar/decoder.ts +215 -0
- package/src/ts/databar/encoder.ts +96 -0
- package/src/ts/databar/gs1.ts +177 -0
- package/src/ts/databar/index.ts +68 -0
- package/src/ts/databar/patterns.ts +143 -0
- package/src/ts/databar/tables.ts +124 -0
- package/src/ts/datamatrix/decoder.ts +262 -0
- package/src/ts/datamatrix/detector.ts +225 -0
- package/src/ts/datamatrix/encoder.ts +191 -0
- package/src/ts/datamatrix/index.ts +42 -0
- package/src/ts/datamatrix/tables.ts +123 -0
- package/src/ts/frameqr/decoder.ts +239 -0
- package/src/ts/frameqr/detector.ts +192 -0
- package/src/ts/frameqr/encoder.ts +156 -0
- package/src/ts/frameqr/index.ts +42 -0
- package/src/ts/frameqr/tables.ts +270 -0
- package/src/ts/image/binarizer.ts +270 -0
- package/src/ts/image/grid-sampler.ts +164 -0
- package/src/ts/image/index.ts +40 -0
- package/src/ts/image/luminance.ts +196 -0
- package/src/ts/image/perspective.ts +195 -0
- package/src/ts/index.d.ts +1 -1
- package/src/ts/index.ts +790 -0
- package/src/ts/micropdf417/compaction.ts +116 -0
- package/src/ts/micropdf417/decoder.ts +183 -0
- package/src/ts/micropdf417/detector.ts +149 -0
- package/src/ts/micropdf417/encoder.ts +209 -0
- package/src/ts/micropdf417/error-correction.ts +55 -0
- package/src/ts/micropdf417/index.ts +49 -0
- package/src/ts/micropdf417/tables.ts +184 -0
- package/src/ts/microqr/decoder.ts +245 -0
- package/src/ts/microqr/detector.ts +355 -0
- package/src/ts/microqr/encoder.ts +269 -0
- package/src/ts/microqr/index.ts +36 -0
- package/src/ts/microqr/tables.ts +316 -0
- package/src/ts/oned/addons.ts +420 -0
- package/src/ts/oned/index.ts +106 -0
- package/src/ts/oned/patterns.ts +384 -0
- package/src/ts/oned/reader.ts +1364 -0
- package/src/ts/oned/writers.ts +746 -0
- package/src/ts/pdf417/compaction.ts +298 -0
- package/src/ts/pdf417/decoder.ts +75 -0
- package/src/ts/pdf417/detector.ts +468 -0
- package/src/ts/pdf417/encoder.ts +91 -0
- package/src/ts/pdf417/error-correction.ts +47 -0
- package/src/ts/pdf417/index.ts +6 -0
- package/src/ts/pdf417/tables.ts +317 -0
- package/src/ts/qr/decoder.ts +575 -0
- package/src/ts/qr/detector.ts +630 -0
- package/src/ts/qr/encoder.ts +958 -0
- package/src/ts/qr/index.ts +44 -0
- package/src/ts/qr/tables.ts +737 -0
- package/src/ts/render/image-data.ts +125 -0
- package/src/ts/render/index.ts +130 -0
- package/src/ts/render/options.ts +160 -0
- package/src/ts/render/png.ts +295 -0
- package/src/ts/render/svg.ts +120 -0
- package/src/ts/render/webgl.ts +206 -0
- package/src/ts/render/webgpu.ts +369 -0
- package/src/ts/rmqr/decoder.ts +101 -0
- package/src/ts/rmqr/detector.ts +90 -0
- package/src/ts/rmqr/encoder.ts +172 -0
- package/src/ts/rmqr/index.ts +37 -0
- package/src/ts/rmqr/tables.ts +154 -0
|
@@ -27,7 +27,6 @@
|
|
|
27
27
|
*
|
|
28
28
|
* Original work. No code from any other barcode implementation.
|
|
29
29
|
*/
|
|
30
|
-
|
|
31
30
|
/**
|
|
32
31
|
* Data Matrix ECC 200 detection in a binarized image.
|
|
33
32
|
*
|
|
@@ -44,104 +43,111 @@
|
|
|
44
43
|
*
|
|
45
44
|
* @module datamatrix/detector
|
|
46
45
|
*/
|
|
47
|
-
|
|
48
46
|
import { NotFoundError } from '../core/errors.js';
|
|
49
47
|
import { sampleGrid, sampleQuad } from '../image/grid-sampler.js';
|
|
50
48
|
import { PerspectiveTransform } from '../image/perspective.js';
|
|
51
49
|
import { decodeDataMatrix } from './decoder.js';
|
|
52
|
-
|
|
53
50
|
// ECC 200 dimensions. DMRE is deliberately not included: it uses a separate
|
|
54
51
|
// size table and is not part of the original ECC 200 family implemented here.
|
|
55
52
|
const SIZES = [
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
53
|
+
[10, 10], [12, 12], [14, 14], [16, 16], [18, 18], [20, 20], [22, 22], [24, 24], [26, 26],
|
|
54
|
+
[32, 32], [36, 36], [40, 40], [44, 44], [48, 48], [52, 52], [64, 64], [72, 72], [80, 80],
|
|
55
|
+
[88, 88], [96, 96], [104, 104], [120, 120], [132, 132], [144, 144],
|
|
56
|
+
[18, 8], [32, 8], [26, 12], [36, 12], [36, 16], [48, 16],
|
|
60
57
|
];
|
|
61
|
-
|
|
62
58
|
/** @typedef {{x:number, y:number}} Point */
|
|
63
59
|
/** @typedef {{corners: Point[], dimension: number, width: number, height: number, moduleSize: number, matrix: import('../core/bit-matrix.js').BitMatrix}} Detection */
|
|
64
|
-
|
|
65
60
|
function dark(image, x, y) {
|
|
66
|
-
|
|
67
|
-
Math.max(0, Math.min(image.height - 1, Math.round(y))));
|
|
61
|
+
return image.get(Math.max(0, Math.min(image.width - 1, Math.round(x))), Math.max(0, Math.min(image.height - 1, Math.round(y))));
|
|
68
62
|
}
|
|
69
|
-
|
|
70
63
|
/** Return components which are large enough to plausibly contain a symbol. */
|
|
71
64
|
function components(image) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
65
|
+
const seen = new Uint8Array(image.width * image.height);
|
|
66
|
+
const out = [];
|
|
67
|
+
const push = (x, y, xs, ys) => { xs.push(x); ys.push(y); };
|
|
68
|
+
for (let y = 0; y < image.height; y++)
|
|
69
|
+
for (let x = 0; x < image.width; x++) {
|
|
70
|
+
const start = y * image.width + x;
|
|
71
|
+
if (seen[start] || !image.get(x, y))
|
|
72
|
+
continue;
|
|
73
|
+
const xs = [x], ys = [y];
|
|
74
|
+
seen[start] = 1;
|
|
75
|
+
let head = 0, minX = x, maxX = x, minY = y, maxY = y;
|
|
76
|
+
while (head < xs.length) {
|
|
77
|
+
const px = xs[head], py = ys[head++];
|
|
78
|
+
if (px < minX)
|
|
79
|
+
minX = px;
|
|
80
|
+
if (px > maxX)
|
|
81
|
+
maxX = px;
|
|
82
|
+
if (py < minY)
|
|
83
|
+
minY = py;
|
|
84
|
+
if (py > maxY)
|
|
85
|
+
maxY = py;
|
|
86
|
+
for (const [nx, ny] of [[px - 1, py], [px + 1, py], [px, py - 1], [px, py + 1]]) {
|
|
87
|
+
if (nx < 0 || ny < 0 || nx >= image.width || ny >= image.height)
|
|
88
|
+
continue;
|
|
89
|
+
const at = ny * image.width + nx;
|
|
90
|
+
if (!seen[at] && image.get(nx, ny)) {
|
|
91
|
+
seen[at] = 1;
|
|
92
|
+
push(nx, ny, xs, ys);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (maxX - minX >= 7 && maxY - minY >= 7)
|
|
97
|
+
out.push({ minX, minY, maxX, maxY, pixels: xs.length });
|
|
98
|
+
}
|
|
99
|
+
return out.sort((a, b) => b.pixels - a.pixels).slice(0, 40);
|
|
93
100
|
}
|
|
94
|
-
|
|
95
101
|
/** Sample a physical edge at module centres. */
|
|
96
102
|
function edge(image, a, b, count) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
+
const values = [];
|
|
104
|
+
for (let i = 0; i < count; i++) {
|
|
105
|
+
const t = (i + 0.5) / count;
|
|
106
|
+
values.push(dark(image, a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t));
|
|
107
|
+
}
|
|
108
|
+
return values;
|
|
103
109
|
}
|
|
104
|
-
|
|
105
110
|
function solidScore(values) {
|
|
106
|
-
|
|
107
|
-
|
|
111
|
+
let n = 0;
|
|
112
|
+
for (const value of values)
|
|
113
|
+
if (value)
|
|
114
|
+
n++;
|
|
115
|
+
return n / values.length;
|
|
108
116
|
}
|
|
109
|
-
|
|
110
117
|
function clockScore(values, startsDark) {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
118
|
+
let n = 0;
|
|
119
|
+
for (let i = 0; i < values.length; i++)
|
|
120
|
+
if (values[i] === ((i & 1) === 0 ? startsDark : !startsDark))
|
|
121
|
+
n++;
|
|
122
|
+
return n / values.length;
|
|
114
123
|
}
|
|
115
|
-
|
|
116
124
|
/** Count light/dark changes along a physical edge at approximately one-pixel intervals. */
|
|
117
125
|
function edgeTransitions(image, a, b) {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
126
|
+
const steps = Math.max(1, Math.ceil(Math.hypot(b.x - a.x, b.y - a.y)));
|
|
127
|
+
let previous = dark(image, a.x, a.y);
|
|
128
|
+
let changes = 0;
|
|
129
|
+
for (let i = 1; i <= steps; i++) {
|
|
130
|
+
const t = i / steps;
|
|
131
|
+
const value = dark(image, a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t);
|
|
132
|
+
if (value !== previous)
|
|
133
|
+
changes++;
|
|
134
|
+
previous = value;
|
|
135
|
+
}
|
|
136
|
+
return changes;
|
|
128
137
|
}
|
|
129
|
-
|
|
130
138
|
/** Reject a smaller harmonic whose module-centre samples happen to alternate. */
|
|
131
139
|
function transitionCountFits(observed, modules) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
140
|
+
const expected = modules - 1;
|
|
141
|
+
const tolerance = Math.max(2, Math.floor(expected * 0.08));
|
|
142
|
+
return Math.abs(observed - expected) <= tolerance;
|
|
135
143
|
}
|
|
136
|
-
|
|
137
144
|
function sample(image, width, height, corners, voting) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
tl.x, tl.y, tr.x, tr.y, br.x, br.y, bl.x, bl.y);
|
|
142
|
-
|
|
145
|
+
if (width === height)
|
|
146
|
+
return sampleQuad(image, width, corners, voting);
|
|
147
|
+
const [tl, tr, br, bl] = corners;
|
|
148
|
+
const transform = PerspectiveTransform.quadToQuad(0, 0, width, 0, width, height, 0, height, tl.x, tl.y, tr.x, tr.y, br.x, br.y, bl.x, bl.y);
|
|
149
|
+
return sampleGrid(image, width, height, transform);
|
|
143
150
|
}
|
|
144
|
-
|
|
145
151
|
/**
|
|
146
152
|
* Find Data Matrix symbols in a binarized image.
|
|
147
153
|
*
|
|
@@ -149,61 +155,70 @@ function sample(image, width, height, corners, voting) {
|
|
|
149
155
|
* @returns {Detection | null} The strongest candidate, or null when absent.
|
|
150
156
|
*/
|
|
151
157
|
export function detectDataMatrix(binaryImage) {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
158
|
+
if (!binaryImage || !binaryImage.width || !binaryImage.height) {
|
|
159
|
+
throw new NotFoundError('detectDataMatrix: no image supplied');
|
|
160
|
+
}
|
|
161
|
+
const detections = [];
|
|
162
|
+
const used = new Set();
|
|
163
|
+
for (const box of components(binaryImage)) {
|
|
164
|
+
const base = [
|
|
165
|
+
{ x: box.minX, y: box.minY }, { x: box.maxX + 1, y: box.minY },
|
|
166
|
+
{ x: box.maxX + 1, y: box.maxY + 1 }, { x: box.minX, y: box.maxY + 1 },
|
|
167
|
+
];
|
|
168
|
+
// Profile the actual ink, rather than the outer sampling quadrilateral:
|
|
169
|
+
// its far x/y boundary lies one pixel beyond the last dark pixel.
|
|
170
|
+
const ink = [
|
|
171
|
+
{ x: box.minX, y: box.minY }, { x: box.maxX, y: box.minY },
|
|
172
|
+
{ x: box.maxX, y: box.maxY }, { x: box.minX, y: box.maxY },
|
|
173
|
+
];
|
|
174
|
+
for (const [w, h] of SIZES)
|
|
175
|
+
for (let turn = 0; turn < 4; turn++) {
|
|
176
|
+
// A 90 degree turn swaps physical width and height.
|
|
177
|
+
const physicalW = (turn & 1) ? h : w, physicalH = (turn & 1) ? w : h;
|
|
178
|
+
const pitchX = (box.maxX - box.minX + 1) / physicalW;
|
|
179
|
+
const pitchY = (box.maxY - box.minY + 1) / physicalH;
|
|
180
|
+
if (Math.min(pitchX, pitchY) < 1 || Math.abs(pitchX - pitchY) > Math.max(pitchX, pitchY) * 0.22)
|
|
181
|
+
continue;
|
|
182
|
+
const corners = base.slice(turn).concat(base.slice(0, turn));
|
|
183
|
+
const profile = ink.slice(turn).concat(ink.slice(0, turn));
|
|
184
|
+
// Canonical edge order: top clock, right clock, bottom solid, left solid.
|
|
185
|
+
const top = edge(binaryImage, profile[0], profile[1], w);
|
|
186
|
+
const right = edge(binaryImage, profile[1], profile[2], h);
|
|
187
|
+
const bottom = edge(binaryImage, profile[2], profile[3], w);
|
|
188
|
+
const left = edge(binaryImage, profile[3], profile[0], h);
|
|
189
|
+
// Sampling only the proposed module centres aliases exact harmonics: an
|
|
190
|
+
// 80-module clock border, for example, can look like a perfect 16-module
|
|
191
|
+
// border. Count transitions at image-pixel resolution as an independent
|
|
192
|
+
// dimension measurement before accepting the candidate.
|
|
193
|
+
if (!transitionCountFits(edgeTransitions(binaryImage, profile[0], profile[1]), w) ||
|
|
194
|
+
!transitionCountFits(edgeTransitions(binaryImage, profile[1], profile[2]), h))
|
|
195
|
+
continue;
|
|
196
|
+
// The top clock starts dark at the solid left border. The right clock is
|
|
197
|
+
// anchored dark at the solid bottom border instead, so its top phase
|
|
198
|
+
// depends on the symbol height (all ECC 200 heights are even and
|
|
199
|
+
// therefore start light).
|
|
200
|
+
const score = (clockScore(top, true) + clockScore(right, (h & 1) === 1) +
|
|
201
|
+
solidScore(bottom) + solidScore(left)) / 4;
|
|
202
|
+
if (score < 0.88)
|
|
203
|
+
continue;
|
|
204
|
+
const key = `${box.minX},${box.minY},${box.maxX},${box.maxY}`;
|
|
205
|
+
if (used.has(key))
|
|
206
|
+
continue;
|
|
207
|
+
let matrix;
|
|
208
|
+
try {
|
|
209
|
+
matrix = sample(binaryImage, w, h, corners, false);
|
|
210
|
+
}
|
|
211
|
+
catch (e) {
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
used.add(key);
|
|
215
|
+
detections.push({ corners, dimension: w === h ? w : 0, width: w, height: h,
|
|
216
|
+
moduleSize: (pitchX + pitchY) / 2, matrix, score });
|
|
217
|
+
}
|
|
201
218
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
return detections[0] ?? null;
|
|
219
|
+
detections.sort((a, b) => b.score - a.score || b.moduleSize - a.moduleSize);
|
|
220
|
+
return detections[0] ?? null;
|
|
205
221
|
}
|
|
206
|
-
|
|
207
222
|
/**
|
|
208
223
|
* Detect and decode Data Matrix symbols. Detection failure is normal for an
|
|
209
224
|
* image without a symbol, so candidates that cannot decode are skipped.
|
|
@@ -212,14 +227,28 @@ export function detectDataMatrix(binaryImage) {
|
|
|
212
227
|
* @returns {(import('./decoder.js').DecodeResult & {corners: Point[]}) | null}
|
|
213
228
|
*/
|
|
214
229
|
export function detectAndDecodeDataMatrix(binaryImage) {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
230
|
+
let detection;
|
|
231
|
+
try {
|
|
232
|
+
detection = detectDataMatrix(binaryImage);
|
|
233
|
+
}
|
|
234
|
+
catch (e) {
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
if (!detection)
|
|
238
|
+
return null;
|
|
239
|
+
for (const voting of [false, true]) {
|
|
240
|
+
let matrix = detection.matrix;
|
|
241
|
+
try {
|
|
242
|
+
if (voting)
|
|
243
|
+
matrix = sample(binaryImage, detection.width, detection.height, detection.corners, true);
|
|
244
|
+
}
|
|
245
|
+
catch (e) {
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
try {
|
|
249
|
+
return Object.assign({ corners: detection.corners }, decodeDataMatrix(matrix));
|
|
250
|
+
}
|
|
251
|
+
catch (e) { /* A geometric candidate is not necessarily a symbol. */ }
|
|
252
|
+
}
|
|
253
|
+
return null;
|
|
225
254
|
}
|