qr-decoder-js 1.0.5 → 1.0.8
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.jsQR +201 -0
- package/LICENSE.qcore +21 -0
- package/README.md +19 -121
- package/README_ZH.md +19 -117
- package/dist/cjs/core/decoder.js +246 -0
- package/dist/cjs/core/encoding.js +217 -0
- package/dist/cjs/core/preprocess.js +687 -0
- package/dist/cjs/index.js +12 -0
- package/dist/cjs/native/binarize.js +76 -0
- package/dist/cjs/native/bitstream.js +138 -0
- package/dist/cjs/native/format.js +33 -0
- package/dist/cjs/native/gf256.js +50 -0
- package/dist/cjs/native/locator.js +159 -0
- package/dist/cjs/native/matrix-decoder.js +166 -0
- package/dist/cjs/native/matrix.js +31 -0
- package/dist/cjs/native/reed-solomon.js +106 -0
- package/dist/cjs/native/sampling.js +71 -0
- package/dist/cjs/native/scanner.js +60 -0
- package/dist/cjs/native/versions.js +45 -0
- package/dist/cjs/package.json +1 -0
- package/dist/esm/core/decoder.d.ts +59 -0
- package/dist/esm/core/decoder.js +242 -0
- package/dist/esm/core/encoding.d.ts +33 -0
- package/dist/esm/core/encoding.js +211 -0
- package/dist/esm/core/preprocess.d.ts +191 -0
- package/dist/esm/core/preprocess.js +670 -0
- package/dist/esm/index.d.ts +7 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/native/binarize.d.ts +6 -0
- package/dist/esm/native/binarize.js +73 -0
- package/dist/esm/native/bitstream.d.ts +14 -0
- package/dist/esm/native/bitstream.js +135 -0
- package/dist/esm/native/format.d.ts +9 -0
- package/dist/esm/native/format.js +30 -0
- package/dist/esm/native/gf256.d.ts +5 -0
- package/dist/esm/native/gf256.js +44 -0
- package/dist/esm/native/locator.d.ts +14 -0
- package/dist/esm/native/locator.js +156 -0
- package/dist/esm/native/matrix-decoder.d.ts +5 -0
- package/dist/esm/native/matrix-decoder.js +162 -0
- package/dist/esm/native/matrix.d.ts +12 -0
- package/dist/esm/native/matrix.js +27 -0
- package/dist/esm/native/reed-solomon.d.ts +2 -0
- package/dist/esm/native/reed-solomon.js +103 -0
- package/dist/esm/native/sampling.d.ts +7 -0
- package/dist/esm/native/sampling.js +68 -0
- package/dist/esm/native/scanner.d.ts +2 -0
- package/dist/esm/native/scanner.js +57 -0
- package/dist/esm/native/versions.d.ts +9 -0
- package/dist/esm/native/versions.js +42 -0
- package/dist/{index.d.ts → esm/types.d.ts} +18 -3
- package/package.json +17 -19
- package/dist/qr-decoder.js +0 -2611
- package/dist/qr-decoder.umd.cjs +0 -1
|
@@ -0,0 +1,670 @@
|
|
|
1
|
+
// Image preprocessing: multi-channel color rescue, auto-contrast stretching,
|
|
2
|
+
// sharpening, CLAHE, center-zoom, Otsu binarization, and perspective tilt shear compensation.
|
|
3
|
+
// Rescues colored, blue-ink, low-contrast, blurred, distant, inverted, and tilted QR codes.
|
|
4
|
+
// ImageData creation and geometric transforms
|
|
5
|
+
/**
|
|
6
|
+
* Safe factory for ImageData or ImageData-compatible pixel container.
|
|
7
|
+
* Compatible with Browser DOM, Web Workers, OffscreenCanvas, and headless environments.
|
|
8
|
+
*
|
|
9
|
+
* @param {number} width
|
|
10
|
+
* @param {number} height
|
|
11
|
+
* @returns {ImageData}
|
|
12
|
+
*/
|
|
13
|
+
export function createImageData(width, height) {
|
|
14
|
+
if (typeof ImageData !== 'undefined') {
|
|
15
|
+
try {
|
|
16
|
+
return new ImageData(width, height);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
// Fallback below
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
if (typeof document !== 'undefined' && typeof document.createElement === 'function') {
|
|
23
|
+
try {
|
|
24
|
+
const c = document.createElement('canvas');
|
|
25
|
+
const ctx = c.getContext('2d');
|
|
26
|
+
if (ctx && typeof ctx.createImageData === 'function') {
|
|
27
|
+
return ctx.createImageData(width, height);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
// Fallback below
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
width,
|
|
36
|
+
height,
|
|
37
|
+
data: new Uint8ClampedArray(width * height * 4),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
// Separable cubic interpolation keeps small modules distinct before thresholding.
|
|
41
|
+
function resizePlane(source, width, height, outWidth, outHeight, a, scale) {
|
|
42
|
+
const weight = (distance) => {
|
|
43
|
+
const x = Math.abs(distance);
|
|
44
|
+
if (x <= 1)
|
|
45
|
+
return (a + 2) * x ** 3 - (a + 3) * x ** 2 + 1;
|
|
46
|
+
if (x < 2)
|
|
47
|
+
return a * x ** 3 - 5 * a * x ** 2 + 8 * a * x - 4 * a;
|
|
48
|
+
return 0;
|
|
49
|
+
};
|
|
50
|
+
const horizontal = new Float32Array(outWidth * height);
|
|
51
|
+
for (let x = 0; x < outWidth; x++) {
|
|
52
|
+
const sourceX = (x + 0.5) * (scale ? 1 / scale : width / outWidth) - 0.5, base = Math.floor(sourceX);
|
|
53
|
+
const indices = Array.from({ length: 4 }, (_, i) => Math.max(0, Math.min(width - 1, base + i - 1)));
|
|
54
|
+
const weights = indices.map((_, i) => weight(sourceX - (base + i - 1)));
|
|
55
|
+
for (let y = 0; y < height; y++) {
|
|
56
|
+
let sum = 0;
|
|
57
|
+
for (let i = 0; i < 4; i++)
|
|
58
|
+
sum += source[y * width + indices[i]] * weights[i];
|
|
59
|
+
horizontal[y * outWidth + x] = sum;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const output = new Uint8Array(outWidth * outHeight);
|
|
63
|
+
for (let y = 0; y < outHeight; y++) {
|
|
64
|
+
const sourceY = (y + 0.5) * (scale ? 1 / scale : height / outHeight) - 0.5, base = Math.floor(sourceY);
|
|
65
|
+
const indices = Array.from({ length: 4 }, (_, i) => Math.max(0, Math.min(height - 1, base + i - 1)));
|
|
66
|
+
const weights = indices.map((_, i) => weight(sourceY - (base + i - 1)));
|
|
67
|
+
for (let x = 0; x < outWidth; x++) {
|
|
68
|
+
let sum = 0;
|
|
69
|
+
for (let i = 0; i < 4; i++)
|
|
70
|
+
sum += horizontal[indices[i] * outWidth + x] * weights[i];
|
|
71
|
+
output[y * outWidth + x] = Math.max(0, Math.min(255, Math.round(sum)));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return output;
|
|
75
|
+
}
|
|
76
|
+
/** Enlarge small colored codes before and after adaptive thresholding. */
|
|
77
|
+
export function enlargeSmallColorCode(imageData) {
|
|
78
|
+
const { data, width, height } = imageData;
|
|
79
|
+
const evenRound = (value) => value % 1 === 0.5 ? Math.round(value / 2) * 2 : Math.round(value);
|
|
80
|
+
const w = evenRound(width * 1.5), h = evenRound(height * 1.5);
|
|
81
|
+
const red = new Uint8Array(width * height), blue = new Uint8Array(width * height);
|
|
82
|
+
for (let i = 0; i < red.length; i++) {
|
|
83
|
+
red[i] = data[i * 4];
|
|
84
|
+
blue[i] = data[i * 4 + 2];
|
|
85
|
+
}
|
|
86
|
+
const scaledRed = resizePlane(red, width, height, w, h, -0.75, 1.5);
|
|
87
|
+
const scaledBlue = resizePlane(blue, width, height, w, h, -0.75, 1.5);
|
|
88
|
+
const gray = new Uint8Array(w * h);
|
|
89
|
+
let min = 255, max = 0;
|
|
90
|
+
for (let i = 0; i < gray.length; i++) {
|
|
91
|
+
const value = Math.max(0, Math.min(255, 2 * scaledRed[i] - scaledBlue[i]));
|
|
92
|
+
gray[i] = value;
|
|
93
|
+
min = Math.min(min, value);
|
|
94
|
+
max = Math.max(max, value);
|
|
95
|
+
}
|
|
96
|
+
if (max - min <= 15)
|
|
97
|
+
return null;
|
|
98
|
+
for (let i = 0; i < gray.length; i++)
|
|
99
|
+
gray[i] = Math.floor((gray[i] - min) * 255 / (max - min));
|
|
100
|
+
const binary = toAdaptiveBinary(gray, w, h, 8, 0.15);
|
|
101
|
+
const enlarged = resizePlane(binary, w, h, w * 4, h * 4, -0.5);
|
|
102
|
+
return grayToImageData(enlarged, w * 4, h * 4);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Create a canvas when a browser or worker canvas API is available.
|
|
106
|
+
* Returns null so callers can use their CPU fallback in headless environments.
|
|
107
|
+
*
|
|
108
|
+
* @param {number} width
|
|
109
|
+
* @param {number} height
|
|
110
|
+
* @returns {HTMLCanvasElement|OffscreenCanvas|null}
|
|
111
|
+
*/
|
|
112
|
+
function createCanvas(width, height) {
|
|
113
|
+
if (typeof OffscreenCanvas !== 'undefined') {
|
|
114
|
+
return new OffscreenCanvas(width, height);
|
|
115
|
+
}
|
|
116
|
+
if (typeof document !== 'undefined' && typeof document.createElement === 'function') {
|
|
117
|
+
const canvas = document.createElement('canvas');
|
|
118
|
+
canvas.width = width;
|
|
119
|
+
canvas.height = height;
|
|
120
|
+
return canvas;
|
|
121
|
+
}
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Generate targeted multi-path rescue variants for colored and blue-ink QR codes.
|
|
126
|
+
* Keeps the raw channel formulas first because they preserve module boundaries;
|
|
127
|
+
* stretched variants follow for images whose color range is especially narrow.
|
|
128
|
+
*
|
|
129
|
+
* 1. blue-enhanced: raw (2*R - B), the primary blue-ink rescue path.
|
|
130
|
+
* 2. red-channel: raw red channel, useful when blue ink absorbs red strongly.
|
|
131
|
+
* 3. blue-enhanced-stretched and red-stretched: dynamic-range rescue variants.
|
|
132
|
+
*
|
|
133
|
+
* @param {ImageData} imageData
|
|
134
|
+
* @returns {{ data: ImageData, label: string }[]}
|
|
135
|
+
*/
|
|
136
|
+
export function generatePreprocessVariants(imageData) {
|
|
137
|
+
const { data, width, height } = imageData;
|
|
138
|
+
const n = data.length;
|
|
139
|
+
const rawRed = createImageData(width, height);
|
|
140
|
+
const dRawRed = rawRed.data;
|
|
141
|
+
const rawBlueEnhanced = createImageData(width, height);
|
|
142
|
+
const dRawBlueEnhanced = rawBlueEnhanced.data;
|
|
143
|
+
const stretchedRed = createImageData(width, height);
|
|
144
|
+
const dStretchedRed = stretchedRed.data;
|
|
145
|
+
const stretchedBlueEnhanced = createImageData(width, height);
|
|
146
|
+
const dStretchedBlueEnhanced = stretchedBlueEnhanced.data;
|
|
147
|
+
// Pass 1: find min and max for contrast stretching
|
|
148
|
+
let minR = 255, maxR = 0;
|
|
149
|
+
let minDiff = 500, maxDiff = -500;
|
|
150
|
+
const diffs = new Int16Array(width * height);
|
|
151
|
+
for (let i = 0, p = 0; i < n; i += 4, p++) {
|
|
152
|
+
const r = data[i];
|
|
153
|
+
const b = data[i + 2];
|
|
154
|
+
const d = 2 * r - b;
|
|
155
|
+
diffs[p] = d;
|
|
156
|
+
if (r < minR)
|
|
157
|
+
minR = r;
|
|
158
|
+
if (r > maxR)
|
|
159
|
+
maxR = r;
|
|
160
|
+
if (d < minDiff)
|
|
161
|
+
minDiff = d;
|
|
162
|
+
if (d > maxDiff)
|
|
163
|
+
maxDiff = d;
|
|
164
|
+
}
|
|
165
|
+
// Range normalization factor
|
|
166
|
+
const rangeR = maxR - minR > 15 ? maxR - minR : 255;
|
|
167
|
+
const baseR = maxR - minR > 15 ? minR : 0;
|
|
168
|
+
const rangeDiff = maxDiff - minDiff > 15 ? maxDiff - minDiff : 255;
|
|
169
|
+
const baseDiff = maxDiff - minDiff > 15 ? minDiff : 0;
|
|
170
|
+
for (let p = 0, i = 0; i < n; i += 4, p++) {
|
|
171
|
+
const a = data[i + 3] || 255;
|
|
172
|
+
const rawRedValue = data[i];
|
|
173
|
+
const rawBlueValue = Math.max(0, Math.min(255, 2 * rawRedValue - data[i + 2]));
|
|
174
|
+
dRawRed[i] = rawRedValue;
|
|
175
|
+
dRawRed[i + 1] = rawRedValue;
|
|
176
|
+
dRawRed[i + 2] = rawRedValue;
|
|
177
|
+
dRawRed[i + 3] = a;
|
|
178
|
+
dRawBlueEnhanced[i] = rawBlueValue;
|
|
179
|
+
dRawBlueEnhanced[i + 1] = rawBlueValue;
|
|
180
|
+
dRawBlueEnhanced[i + 2] = rawBlueValue;
|
|
181
|
+
dRawBlueEnhanced[i + 3] = a;
|
|
182
|
+
const valR = Math.max(0, Math.min(255, Math.round(((rawRedValue - baseR) * 255) / rangeR)));
|
|
183
|
+
dStretchedRed[i] = valR;
|
|
184
|
+
dStretchedRed[i + 1] = valR;
|
|
185
|
+
dStretchedRed[i + 2] = valR;
|
|
186
|
+
dStretchedRed[i + 3] = a;
|
|
187
|
+
// Dynamic-range blue suppression (2*R - B).
|
|
188
|
+
const valDiff = Math.max(0, Math.min(255, Math.round(((diffs[p] - baseDiff) * 255) / rangeDiff)));
|
|
189
|
+
dStretchedBlueEnhanced[i] = valDiff;
|
|
190
|
+
dStretchedBlueEnhanced[i + 1] = valDiff;
|
|
191
|
+
dStretchedBlueEnhanced[i + 2] = valDiff;
|
|
192
|
+
dStretchedBlueEnhanced[i + 3] = a;
|
|
193
|
+
}
|
|
194
|
+
return [
|
|
195
|
+
{ data: rawBlueEnhanced, label: 'blue-enhanced' },
|
|
196
|
+
{ data: rawRed, label: 'red-channel' },
|
|
197
|
+
{ data: stretchedBlueEnhanced, label: 'blue-enhanced-stretched' },
|
|
198
|
+
{ data: stretchedRed, label: 'red-stretched' },
|
|
199
|
+
];
|
|
200
|
+
}
|
|
201
|
+
// Channel enhancement and local contrast transforms
|
|
202
|
+
/**
|
|
203
|
+
* 3x3 Laplacian edge-sharpening (Unsharp Mask) filter.
|
|
204
|
+
* Directly restores blurry, out-of-focus, and low-contrast module transitions.
|
|
205
|
+
*
|
|
206
|
+
* @param {ImageData} imageData
|
|
207
|
+
* @param {number} [amount=1.2]
|
|
208
|
+
* @returns {ImageData}
|
|
209
|
+
*/
|
|
210
|
+
export function sharpenImageData(imageData, amount = 1.2) {
|
|
211
|
+
const { width, height, data } = imageData;
|
|
212
|
+
const out = createImageData(width, height);
|
|
213
|
+
const oData = out.data;
|
|
214
|
+
for (let y = 0; y < height; y++) {
|
|
215
|
+
const row = y * width;
|
|
216
|
+
const upRow = Math.max(0, y - 1) * width;
|
|
217
|
+
const downRow = Math.min(height - 1, y + 1) * width;
|
|
218
|
+
for (let x = 0; x < width; x++) {
|
|
219
|
+
const idx = (row + x) * 4;
|
|
220
|
+
const c = data[idx];
|
|
221
|
+
const u = data[(upRow + x) * 4];
|
|
222
|
+
const d = data[(downRow + x) * 4];
|
|
223
|
+
const l = data[(row + Math.max(0, x - 1)) * 4];
|
|
224
|
+
const r = data[(row + Math.min(width - 1, x + 1)) * 4];
|
|
225
|
+
const laplacian = 5 * c - u - d - l - r;
|
|
226
|
+
const val = Math.max(0, Math.min(255, Math.round(c * (1 - amount) + laplacian * amount)));
|
|
227
|
+
oData[idx] = val;
|
|
228
|
+
oData[idx + 1] = val;
|
|
229
|
+
oData[idx + 2] = val;
|
|
230
|
+
oData[idx + 3] = data[idx + 3] || 255;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return out;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Generate CLAHE (Contrast Limited Adaptive Histogram Equalization) variants.
|
|
237
|
+
* Handles blur, weak focus, shadows, and locally uneven lighting.
|
|
238
|
+
*
|
|
239
|
+
* @param {ImageData} imageData
|
|
240
|
+
* @returns {{ data: ImageData, label: string }[]}
|
|
241
|
+
*/
|
|
242
|
+
export function generateCLAHEVariants(imageData) {
|
|
243
|
+
const { width, height, data } = imageData;
|
|
244
|
+
// Red plane extraction (ultra effective for faint/blurry blue QR codes)
|
|
245
|
+
const redPlane = new Uint8ClampedArray(width * height);
|
|
246
|
+
for (let i = 0, p = 0; p < redPlane.length; i += 4, p++) {
|
|
247
|
+
redPlane[p] = data[i];
|
|
248
|
+
}
|
|
249
|
+
const claheRed = toCLAHE(redPlane, width, height, { clipLimit: 2.5, tileSize: 8 });
|
|
250
|
+
// Standard luma plane extraction
|
|
251
|
+
const gray = toGray(imageData);
|
|
252
|
+
const claheGray = toCLAHE(gray, width, height, { clipLimit: 2.5, tileSize: 8 });
|
|
253
|
+
return [
|
|
254
|
+
{ data: grayToImageData(claheRed, width, height), label: 'clahe-red' },
|
|
255
|
+
{ data: grayToImageData(claheGray, width, height), label: 'clahe-gray' },
|
|
256
|
+
];
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Extract center crop (e.g. 60% of frame).
|
|
260
|
+
* Fixes distant / small QR codes in large high-resolution frames (e.g. 960px or 1080p).
|
|
261
|
+
* Prevents full-image downscaling from destroying small modules.
|
|
262
|
+
*
|
|
263
|
+
* @param {ImageData} imageData
|
|
264
|
+
* @param {number} [ratio=0.6]
|
|
265
|
+
* @returns {ImageData}
|
|
266
|
+
*/
|
|
267
|
+
export function centerCrop(imageData, ratio = 0.6) {
|
|
268
|
+
const { width, height, data } = imageData;
|
|
269
|
+
const cropW = Math.round(width * ratio);
|
|
270
|
+
const cropH = Math.round(height * ratio);
|
|
271
|
+
const startX = Math.round((width - cropW) / 2);
|
|
272
|
+
const startY = Math.round((height - cropH) / 2);
|
|
273
|
+
const out = createImageData(cropW, cropH);
|
|
274
|
+
const oData = out.data;
|
|
275
|
+
for (let y = 0; y < cropH; y++) {
|
|
276
|
+
const srcRow = (startY + y) * width;
|
|
277
|
+
const dstRow = y * cropW;
|
|
278
|
+
for (let x = 0; x < cropW; x++) {
|
|
279
|
+
const s = (srcRow + startX + x) * 4;
|
|
280
|
+
const d = (dstRow + x) * 4;
|
|
281
|
+
oData[d] = data[s];
|
|
282
|
+
oData[d + 1] = data[s + 1];
|
|
283
|
+
oData[d + 2] = data[s + 2];
|
|
284
|
+
oData[d + 3] = data[s + 3] || 255;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return out;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Perspective shear compensation for tilted/skewed QR codes.
|
|
291
|
+
* Uses fast Canvas 2D GPU transform where available, with fast affine CPU fallback.
|
|
292
|
+
*
|
|
293
|
+
* @param {ImageData} srcImageData
|
|
294
|
+
* @param {Array<{sx: number, sy: number, label: string}>} [shearAngles]
|
|
295
|
+
* @returns {{ data: ImageData, label: string }[]}
|
|
296
|
+
*/
|
|
297
|
+
export function generateShearVariants(srcImageData, shearAngles = [
|
|
298
|
+
{ sx: 0, sy: 0.35, label: 'tilt-pitch-down' },
|
|
299
|
+
{ sx: 0, sy: -0.35, label: 'tilt-pitch-up' },
|
|
300
|
+
{ sx: 0.35, sy: 0, label: 'tilt-yaw-right' },
|
|
301
|
+
{ sx: -0.35, sy: 0, label: 'tilt-yaw-left' },
|
|
302
|
+
{ sx: 0.25, sy: 0.25, label: 'tilt-diagonal' },
|
|
303
|
+
{ sx: -0.25, sy: -0.25, label: 'tilt-diagonal-rev' },
|
|
304
|
+
]) {
|
|
305
|
+
const { width, height, data } = srcImageData;
|
|
306
|
+
const source = createCanvas(width, height);
|
|
307
|
+
if (source) {
|
|
308
|
+
const sourceCtx = source.getContext('2d');
|
|
309
|
+
if (sourceCtx) {
|
|
310
|
+
sourceCtx.putImageData(srcImageData, 0, 0);
|
|
311
|
+
const pad = Math.round(Math.max(width, height) * 0.2);
|
|
312
|
+
const outW = width + pad * 2;
|
|
313
|
+
const outH = height + pad * 2;
|
|
314
|
+
const out = createCanvas(outW, outH);
|
|
315
|
+
if (out) {
|
|
316
|
+
const ctx = out.getContext('2d', { willReadFrequently: true });
|
|
317
|
+
if (ctx) {
|
|
318
|
+
const variants = [];
|
|
319
|
+
for (const { sx, sy, label } of shearAngles) {
|
|
320
|
+
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
321
|
+
ctx.fillStyle = '#FFFFFF';
|
|
322
|
+
ctx.fillRect(0, 0, outW, outH);
|
|
323
|
+
ctx.translate(outW / 2, outH / 2);
|
|
324
|
+
ctx.transform(1, sy, sx, 1, 0, 0);
|
|
325
|
+
ctx.drawImage(source, -width / 2, -height / 2);
|
|
326
|
+
variants.push({
|
|
327
|
+
data: ctx.getImageData(0, 0, outW, outH),
|
|
328
|
+
label: `shear-${label}`,
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
return variants;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
// CPU fallback when Canvas is unavailable
|
|
337
|
+
const variants = [];
|
|
338
|
+
for (const { sx, sy, label } of shearAngles) {
|
|
339
|
+
const padX = Math.round(height * Math.abs(sx) * 0.6 + 10);
|
|
340
|
+
const padY = Math.round(width * Math.abs(sy) * 0.6 + 10);
|
|
341
|
+
const outW = width + padX * 2;
|
|
342
|
+
const outH = height + padY * 2;
|
|
343
|
+
const out = createImageData(outW, outH);
|
|
344
|
+
const oData = out.data;
|
|
345
|
+
oData.fill(255);
|
|
346
|
+
const cx = width / 2;
|
|
347
|
+
const cy = height / 2;
|
|
348
|
+
const ocx = outW / 2;
|
|
349
|
+
const ocy = outH / 2;
|
|
350
|
+
const det = 1 - sx * sy;
|
|
351
|
+
if (Math.abs(det) < 0.001)
|
|
352
|
+
continue;
|
|
353
|
+
for (let dy = 0; dy < outH; dy++) {
|
|
354
|
+
const yOff = dy - ocy;
|
|
355
|
+
for (let dx = 0; dx < outW; dx++) {
|
|
356
|
+
const xOff = dx - ocx;
|
|
357
|
+
const srcX = Math.round((xOff - sx * yOff) / det + cx);
|
|
358
|
+
const srcY = Math.round((-sy * xOff + yOff) / det + cy);
|
|
359
|
+
if (srcX >= 0 && srcX < width && srcY >= 0 && srcY < height) {
|
|
360
|
+
const s = (srcY * width + srcX) * 4;
|
|
361
|
+
const d = (dy * outW + dx) * 4;
|
|
362
|
+
oData[d] = data[s];
|
|
363
|
+
oData[d + 1] = data[s + 1];
|
|
364
|
+
oData[d + 2] = data[s + 2];
|
|
365
|
+
oData[d + 3] = 255;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
variants.push({ data: out, label: `shear-${label}` });
|
|
370
|
+
}
|
|
371
|
+
return variants;
|
|
372
|
+
}
|
|
373
|
+
// Quiet-zone, inversion, and binary repair transforms
|
|
374
|
+
/**
|
|
375
|
+
* Pad an image with a clean white quiet zone border.
|
|
376
|
+
* Fixes edge-cropped QR codes that lack surrounding quiet zone.
|
|
377
|
+
*
|
|
378
|
+
* @param {ImageData} imageData
|
|
379
|
+
* @param {number} [borderRatio=0.15]
|
|
380
|
+
* @returns {ImageData|null}
|
|
381
|
+
*/
|
|
382
|
+
export function addQuietZone(imageData, borderRatio = 0.15) {
|
|
383
|
+
const { width, height, data } = imageData;
|
|
384
|
+
const pad = Math.max(24, Math.round(Math.min(width, height) * borderRatio));
|
|
385
|
+
const outW = width + pad * 2;
|
|
386
|
+
const outH = height + pad * 2;
|
|
387
|
+
const out = createImageData(outW, outH);
|
|
388
|
+
const oData = out.data;
|
|
389
|
+
oData.fill(255); // White border
|
|
390
|
+
for (let y = 0; y < height; y++) {
|
|
391
|
+
const srcRow = y * width * 4;
|
|
392
|
+
const dstRow = ((y + pad) * outW + pad) * 4;
|
|
393
|
+
for (let i = 0; i < width * 4; i++) {
|
|
394
|
+
oData[dstRow + i] = data[srcRow + i];
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
return out;
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Generate inverted luma variant for light-on-dark (inverted) QR codes.
|
|
401
|
+
*
|
|
402
|
+
* @param {ImageData} imageData
|
|
403
|
+
* @returns {{ data: ImageData, label: string }}
|
|
404
|
+
*/
|
|
405
|
+
export function generateInvertedVariant(imageData) {
|
|
406
|
+
const { data, width, height } = imageData;
|
|
407
|
+
const inv = createImageData(width, height);
|
|
408
|
+
const dInv = inv.data;
|
|
409
|
+
const n = data.length;
|
|
410
|
+
for (let i = 0; i < n; i += 4) {
|
|
411
|
+
const r = data[i];
|
|
412
|
+
const g = data[i + 1];
|
|
413
|
+
const b = data[i + 2];
|
|
414
|
+
const a = data[i + 3];
|
|
415
|
+
const luma = Math.round(0.299 * r + 0.587 * g + 0.114 * b);
|
|
416
|
+
const invLuma = 255 - luma;
|
|
417
|
+
dInv[i] = invLuma;
|
|
418
|
+
dInv[i + 1] = invLuma;
|
|
419
|
+
dInv[i + 2] = invLuma;
|
|
420
|
+
dInv[i + 3] = a;
|
|
421
|
+
}
|
|
422
|
+
return { data: inv, label: 'inverted' };
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* Lightweight 3x3 morphological closing to heal broken dot-matrix / pin-printer dots.
|
|
426
|
+
*
|
|
427
|
+
* @param {ImageData} imageData
|
|
428
|
+
* @param {number} [threshold=135]
|
|
429
|
+
* @returns {{ data: ImageData, label: string }[]}
|
|
430
|
+
*/
|
|
431
|
+
export function generateDotMatrixFixVariants(imageData, threshold = 135) {
|
|
432
|
+
const { data, width, height } = imageData;
|
|
433
|
+
const size = width * height;
|
|
434
|
+
const dark = new Uint8Array(size);
|
|
435
|
+
for (let i = 0, p = 0; p < size; i += 4, p++) {
|
|
436
|
+
const lum = (data[i] * 77 + data[i + 1] * 150 + data[i + 2] * 29) >> 8;
|
|
437
|
+
dark[p] = lum < threshold ? 1 : 0;
|
|
438
|
+
}
|
|
439
|
+
const dilated = new Uint8Array(size);
|
|
440
|
+
for (let y = 1; y < height - 1; y++) {
|
|
441
|
+
const row = y * width;
|
|
442
|
+
for (let x = 1; x < width - 1; x++) {
|
|
443
|
+
const idx = row + x;
|
|
444
|
+
dilated[idx] =
|
|
445
|
+
dark[idx] |
|
|
446
|
+
dark[idx - 1] |
|
|
447
|
+
dark[idx + 1] |
|
|
448
|
+
dark[idx - width] |
|
|
449
|
+
dark[idx + width];
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
const closed = new Uint8Array(size);
|
|
453
|
+
for (let y = 1; y < height - 1; y++) {
|
|
454
|
+
const row = y * width;
|
|
455
|
+
for (let x = 1; x < width - 1; x++) {
|
|
456
|
+
const idx = row + x;
|
|
457
|
+
closed[idx] =
|
|
458
|
+
dilated[idx] &
|
|
459
|
+
dilated[idx - 1] &
|
|
460
|
+
dilated[idx + 1] &
|
|
461
|
+
dilated[idx - width] &
|
|
462
|
+
dilated[idx + width];
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
const out = createImageData(width, height);
|
|
466
|
+
const outData = out.data;
|
|
467
|
+
for (let p = 0, i = 0; p < size; p++, i += 4) {
|
|
468
|
+
const val = closed[p] ? 0 : 255;
|
|
469
|
+
outData[i] = val;
|
|
470
|
+
outData[i + 1] = val;
|
|
471
|
+
outData[i + 2] = val;
|
|
472
|
+
outData[i + 3] = 255;
|
|
473
|
+
}
|
|
474
|
+
return [{ data: out, label: 'dot-matrix-healed' }];
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Standard grayscale conversion.
|
|
478
|
+
*
|
|
479
|
+
* @param {ImageData} imgData
|
|
480
|
+
* @returns {Uint8ClampedArray}
|
|
481
|
+
*/
|
|
482
|
+
export function toGray(imgData) {
|
|
483
|
+
const { data, width, height } = imgData;
|
|
484
|
+
const gray = new Uint8ClampedArray(width * height);
|
|
485
|
+
for (let i = 0, j = 0; i < data.length; i += 4, j++) {
|
|
486
|
+
gray[j] = Math.round(0.299 * data[i] + 0.587 * data[i + 1] + 0.114 * data[i + 2]);
|
|
487
|
+
}
|
|
488
|
+
return gray;
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* Convert a 1-channel grayscale byte plane back to an RGBA ImageData.
|
|
492
|
+
*
|
|
493
|
+
* @param {Uint8ClampedArray} gray
|
|
494
|
+
* @param {number} width
|
|
495
|
+
* @param {number} height
|
|
496
|
+
* @returns {ImageData}
|
|
497
|
+
*/
|
|
498
|
+
export function grayToImageData(gray, width, height) {
|
|
499
|
+
const imgData = createImageData(width, height);
|
|
500
|
+
const rgba = imgData.data;
|
|
501
|
+
for (let i = 0, j = 0; i < gray.length; i++, j += 4) {
|
|
502
|
+
const val = gray[i];
|
|
503
|
+
rgba[j] = val;
|
|
504
|
+
rgba[j + 1] = val;
|
|
505
|
+
rgba[j + 2] = val;
|
|
506
|
+
rgba[j + 3] = 255;
|
|
507
|
+
}
|
|
508
|
+
return imgData;
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* Otsu global thresholding binarization.
|
|
512
|
+
*
|
|
513
|
+
* @param {Uint8ClampedArray} gray
|
|
514
|
+
* @param {number} width
|
|
515
|
+
* @param {number} height
|
|
516
|
+
* @returns {Uint8ClampedArray}
|
|
517
|
+
*/
|
|
518
|
+
export function toOtsuBinary(gray, width, height) {
|
|
519
|
+
const total = gray.length;
|
|
520
|
+
const hist = new Int32Array(256);
|
|
521
|
+
for (let i = 0; i < total; i++)
|
|
522
|
+
hist[gray[i]]++;
|
|
523
|
+
let sum = 0;
|
|
524
|
+
for (let t = 0; t < 256; t++)
|
|
525
|
+
sum += t * hist[t];
|
|
526
|
+
let sumB = 0;
|
|
527
|
+
let wB = 0;
|
|
528
|
+
let maxVar = -1;
|
|
529
|
+
let thr = 127;
|
|
530
|
+
for (let t = 0; t < 256; t++) {
|
|
531
|
+
wB += hist[t];
|
|
532
|
+
if (!wB)
|
|
533
|
+
continue;
|
|
534
|
+
const wF = total - wB;
|
|
535
|
+
if (!wF)
|
|
536
|
+
break;
|
|
537
|
+
sumB += t * hist[t];
|
|
538
|
+
const mB = sumB / wB;
|
|
539
|
+
const mF = (sum - sumB) / wF;
|
|
540
|
+
const between = wB * wF * (mB - mF) * (mB - mF);
|
|
541
|
+
if (between > maxVar) {
|
|
542
|
+
maxVar = between;
|
|
543
|
+
thr = t;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
const bin = new Uint8ClampedArray(total);
|
|
547
|
+
for (let i = 0; i < total; i++) {
|
|
548
|
+
bin[i] = gray[i] > thr ? 255 : 0;
|
|
549
|
+
}
|
|
550
|
+
return bin;
|
|
551
|
+
}
|
|
552
|
+
/**
|
|
553
|
+
* Local adaptive thresholding for unevenly lit or low-contrast QR images.
|
|
554
|
+
* Uses an integral image so the per-pixel neighborhood mean stays linear-time.
|
|
555
|
+
*
|
|
556
|
+
* @param {Uint8ClampedArray} gray
|
|
557
|
+
* @param {number} width
|
|
558
|
+
* @param {number} height
|
|
559
|
+
* @param {number} [radius=8]
|
|
560
|
+
* @param {number} [bias=0.15]
|
|
561
|
+
* @returns {Uint8ClampedArray}
|
|
562
|
+
*/
|
|
563
|
+
export function toAdaptiveBinary(gray, width, height, radius = 8, bias = 0.15) {
|
|
564
|
+
const stride = width + 1;
|
|
565
|
+
const integral = new Int32Array((width + 1) * (height + 1));
|
|
566
|
+
const out = new Uint8ClampedArray(gray.length);
|
|
567
|
+
for (let y = 1; y <= height; y++) {
|
|
568
|
+
let rowSum = 0;
|
|
569
|
+
const sourceRow = (y - 1) * width;
|
|
570
|
+
const integralRow = y * stride;
|
|
571
|
+
const previousRow = (y - 1) * stride;
|
|
572
|
+
for (let x = 1; x <= width; x++) {
|
|
573
|
+
rowSum += gray[sourceRow + x - 1];
|
|
574
|
+
integral[integralRow + x] = integral[previousRow + x] + rowSum;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
for (let y = 0; y < height; y++) {
|
|
578
|
+
const y0 = Math.max(0, y - radius);
|
|
579
|
+
const y1 = Math.min(height - 1, y + radius);
|
|
580
|
+
const top = y0 * stride;
|
|
581
|
+
const bottom = (y1 + 1) * stride;
|
|
582
|
+
for (let x = 0; x < width; x++) {
|
|
583
|
+
const x0 = Math.max(0, x - radius);
|
|
584
|
+
const x1 = Math.min(width - 1, x + radius);
|
|
585
|
+
const area = (x1 - x0 + 1) * (y1 - y0 + 1);
|
|
586
|
+
const sum = integral[bottom + x1 + 1] -
|
|
587
|
+
integral[top + x1 + 1] -
|
|
588
|
+
integral[bottom + x0] +
|
|
589
|
+
integral[top + x0];
|
|
590
|
+
const threshold = (sum / area) * (1 - bias);
|
|
591
|
+
out[y * width + x] = gray[y * width + x] <= threshold ? 0 : 255;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
return out;
|
|
595
|
+
}
|
|
596
|
+
/**
|
|
597
|
+
* CLAHE (Contrast Limited Adaptive Histogram Equalization) on a grayscale plane.
|
|
598
|
+
* Directly fixes blurred, poorly focused, or unevenly lit QR codes.
|
|
599
|
+
*
|
|
600
|
+
* @param {Uint8ClampedArray} gray
|
|
601
|
+
* @param {number} width
|
|
602
|
+
* @param {number} height
|
|
603
|
+
* @param {object} [opts]
|
|
604
|
+
* @returns {Uint8ClampedArray}
|
|
605
|
+
*/
|
|
606
|
+
export function toCLAHE(gray, width, height, opts = {}) {
|
|
607
|
+
const { clipLimit = 2.5, tileSize = 8 } = opts;
|
|
608
|
+
const out = new Uint8ClampedArray(gray.length);
|
|
609
|
+
const maps = [];
|
|
610
|
+
for (let ty = 0; ty < tileSize; ty++) {
|
|
611
|
+
for (let tx = 0; tx < tileSize; tx++) {
|
|
612
|
+
const hist = new Int32Array(256);
|
|
613
|
+
const x0 = Math.floor((tx * width) / tileSize);
|
|
614
|
+
const x1 = Math.floor(((tx + 1) * width) / tileSize);
|
|
615
|
+
const y0 = Math.floor((ty * height) / tileSize);
|
|
616
|
+
const y1 = Math.floor(((ty + 1) * height) / tileSize);
|
|
617
|
+
// clipLimit is normalized against the 256-bin histogram of this tile.
|
|
618
|
+
const tileArea = (x1 - x0) * (y1 - y0);
|
|
619
|
+
const clip = Math.max(1, Math.round((clipLimit * tileArea) / 256));
|
|
620
|
+
for (let y = y0; y < y1; y++) {
|
|
621
|
+
const row = y * width;
|
|
622
|
+
for (let x = x0; x < x1; x++) {
|
|
623
|
+
hist[gray[row + x]]++;
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
let excess = 0;
|
|
627
|
+
for (let i = 0; i < 256; i++) {
|
|
628
|
+
if (hist[i] > clip) {
|
|
629
|
+
excess += hist[i] - clip;
|
|
630
|
+
hist[i] = clip;
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
const bonus = Math.floor(excess / 256);
|
|
634
|
+
let rem = excess - bonus * 256;
|
|
635
|
+
for (let i = 0; i < 256; i++)
|
|
636
|
+
hist[i] += bonus;
|
|
637
|
+
for (let i = 0; i < 256 && rem > 0; i++, rem--)
|
|
638
|
+
hist[i] += 1;
|
|
639
|
+
const total = (x1 - x0) * (y1 - y0) || 1;
|
|
640
|
+
let sum = 0;
|
|
641
|
+
const map = new Uint8ClampedArray(256);
|
|
642
|
+
for (let i = 0; i < 256; i++) {
|
|
643
|
+
sum += hist[i];
|
|
644
|
+
map[i] = Math.round((sum * 255) / total);
|
|
645
|
+
}
|
|
646
|
+
maps.push(map);
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
for (let y = 0; y < height; y++) {
|
|
650
|
+
const fy = ((y + 0.5) / height) * tileSize - 0.5;
|
|
651
|
+
const ty0 = Math.max(0, Math.min(tileSize - 1, Math.floor(fy)));
|
|
652
|
+
const ty1 = Math.max(0, Math.min(tileSize - 1, ty0 + 1));
|
|
653
|
+
const wy = Math.max(0, Math.min(1, fy - ty0));
|
|
654
|
+
for (let x = 0; x < width; x++) {
|
|
655
|
+
const fx = ((x + 0.5) / width) * tileSize - 0.5;
|
|
656
|
+
const tx0 = Math.max(0, Math.min(tileSize - 1, Math.floor(fx)));
|
|
657
|
+
const tx1 = Math.max(0, Math.min(tileSize - 1, tx0 + 1));
|
|
658
|
+
const wx = Math.max(0, Math.min(1, fx - tx0));
|
|
659
|
+
const v = gray[y * width + x];
|
|
660
|
+
const m00 = maps[ty0 * tileSize + tx0][v];
|
|
661
|
+
const m01 = maps[ty0 * tileSize + tx1][v];
|
|
662
|
+
const m10 = maps[ty1 * tileSize + tx0][v];
|
|
663
|
+
const m11 = maps[ty1 * tileSize + tx1][v];
|
|
664
|
+
const top = m00 + (m01 - m00) * wx;
|
|
665
|
+
const bottom = m10 + (m11 - m10) * wx;
|
|
666
|
+
out[y * width + x] = top + (bottom - top) * wy;
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
return out;
|
|
670
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public entry point for the QR decoder package.
|
|
3
|
+
*
|
|
4
|
+
* Keep implementation details in `core/` private so they can evolve without
|
|
5
|
+
* expanding the package's public compatibility surface.
|
|
6
|
+
*/
|
|
7
|
+
export { decodeQRImageData, decodeQRFile } from './core/decoder.js';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public entry point for the QR decoder package.
|
|
3
|
+
*
|
|
4
|
+
* Keep implementation details in `core/` private so they can evolve without
|
|
5
|
+
* expanding the package's public compatibility surface.
|
|
6
|
+
*/
|
|
7
|
+
export { decodeQRImageData, decodeQRFile } from './core/decoder.js';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { BitMatrix } from './matrix.js';
|
|
2
|
+
/** Convert RGBA pixels to a locally thresholded QR matrix and, optionally, its inverse. */
|
|
3
|
+
export declare function binarize(data: Uint8ClampedArray, width: number, height: number, returnInverted: boolean): {
|
|
4
|
+
binarized: BitMatrix;
|
|
5
|
+
inverted: BitMatrix;
|
|
6
|
+
};
|