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