quadqr-js 1.0.0 → 1.0.2

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/docs/API.md CHANGED
@@ -1,309 +1,329 @@
1
- # API Reference
2
-
3
- ## Core encoding
4
-
5
- ### `encodeText(text, options?)`
6
-
7
- Synchronously encodes UTF-8 text.
8
-
9
- ```js
10
- import { encodeText } from "quadqr-js";
11
-
12
- const code = encodeText("Hello", {
13
- ecc: "M",
14
- version: 5
15
- });
16
- ```
17
-
18
- If `version` is omitted, QuadQR selects the smallest version that fits.
19
-
20
- ### `encodeBytes(bytes, options?)`
21
-
22
- Encodes arbitrary bytes.
23
-
24
- ```js
25
- const code = encodeBytes(
26
- new Uint8Array([1, 2, 3, 4]),
27
- { ecc: "Q" }
28
- );
29
- ```
30
-
31
- ## Secure encoding
32
-
33
- ### `encodeSecureText(text, options)`
34
-
35
- Encrypts text using Secure Payload v1 and encodes the encrypted envelope.
36
-
37
- Password mode:
38
-
39
- ```js
40
- const code = await encodeSecureText("secret", {
41
- security: {
42
- mode: "password",
43
- password: "example-password"
44
- }
45
- });
46
- ```
47
-
48
- Raw-key mode:
49
-
50
- ```js
51
- const code = await encodeSecureText("secret", {
52
- security: {
53
- mode: "raw-key",
54
- key
55
- }
56
- });
57
- ```
58
-
59
- ### `encodeSecureBytes(bytes, options)`
60
-
61
- Byte-oriented equivalent of `encodeSecureText()`.
62
-
63
- ### `decryptDecoded(result, credentials)`
64
-
65
- Unlocks a secure result returned by matrix, image, camera, or Node.js scanning.
66
-
67
- ```js
68
- const unlocked = await decryptDecoded(locked, {
69
- password: "example-password"
70
- });
71
- ```
72
-
73
- For raw-key mode:
74
-
75
- ```js
76
- const unlocked = await decryptDecoded(locked, { key });
77
- ```
78
-
79
- ## Matrix decoding
80
-
81
- ### `decodeMatrix(matrix, options?)`
82
-
83
- Decodes an already sampled QuadQR matrix.
84
-
85
- ```js
86
- const result = decodeMatrix(code.matrix);
87
- console.log(result.text);
88
- ```
89
-
90
- A secure result remains locked until it is decrypted:
91
-
92
- ```js
93
- {
94
- secure: true,
95
- requiresDecryption: true,
96
- text: null,
97
- payload: Uint8Array(...),
98
- security: {
99
- mode: "password",
100
- algorithm: "AES-256-GCM"
101
- }
102
- }
103
- ```
104
-
105
- ## Rendering
106
-
107
- ### `renderToCanvas(codeOrMatrix, canvas, options?)`
108
-
109
- Renders to an HTML canvas.
110
-
111
- ```js
112
- renderToCanvas(code, canvas, {
113
- moduleSize: 12,
114
- quietZone: 4,
115
- style: "classic"
116
- });
117
- ```
118
-
119
- Supported styles:
120
-
121
- - `classic`
122
- - `depth`
123
- - `soft`
124
- - `inset`
125
-
126
- ### `renderToImageData(codeOrMatrix, options?)`
127
-
128
- Returns runtime-neutral RGBA pixels:
129
-
130
- ```js
131
- {
132
- width,
133
- height,
134
- data: Uint8ClampedArray
135
- }
136
- ```
137
-
138
- ## Image and camera scanning
139
-
140
- ### `scanImageData(imageData, options?)`
141
-
142
- Scans an ImageData-like RGBA object:
143
-
144
- ```js
145
- const result = scanImageData({
146
- width,
147
- height,
148
- data
149
- });
150
- ```
151
-
152
- ### Browser `scanFile(file, options?)`
153
-
154
- Available from `quadqr-js/browser` and the browser-compatible main entry. Accepts a browser `File` or `Blob`.
155
-
156
- ```js
157
- const result = await scanFile(file);
158
- ```
159
-
160
- ### `scanVideoFrame(video, options?)`
161
-
162
- Scans the current frame from an HTML video element.
163
-
164
- ```js
165
- const result = scanVideoFrame(video);
166
- ```
167
-
168
- ### `startCameraScanner(video, options?)`
169
-
170
- Starts live camera scanning.
171
-
172
- ```js
173
- const scanner = await startCameraScanner(video, {
174
- scanInterval: 120,
175
- onDecode(result) {
176
- console.log(result);
177
- },
178
- onError(error) {
179
- console.error(error);
180
- }
181
- });
182
-
183
- scanner.stop();
184
- ```
185
-
186
- ### `rectifyDetectedCode(imageData, options?)`
187
-
188
- Returns the perspective-corrected detected symbol image when detection succeeds.
189
-
190
- ### `rotateMatrix(matrix, quarterTurns?)`
191
-
192
- Rotates a matrix by 90-degree quarter turns.
193
-
194
- ## Node.js helpers
195
-
196
- Import from `quadqr-js/node`.
197
-
198
- ### `toPNG(codeOrMatrix, options?)`
199
-
200
- Returns a Node.js `Buffer` containing a PNG.
201
-
202
- ### `savePNG(codeOrMatrix, filename, options?)`
203
-
204
- Writes a PNG file.
205
-
206
- ```js
207
- await savePNG(code, "quadqr.png", {
208
- moduleSize: 12,
209
- quietZone: 4
210
- });
211
- ```
212
-
213
- ### `scanBuffer(buffer, options?)`
214
-
215
- Scans an image buffer. PNG is supported without dependencies. Other formats can use the host application's optional `sharp` installation.
216
-
217
- ### Node `scanFile(filename, options?)`
218
-
219
- Reads and scans an image file.
220
-
221
- ### `decodePNG(buffer)`
222
-
223
- Decodes supported PNG data into RGBA pixels.
224
-
225
- ### `encodePNG(imageData, options?)`
226
-
227
- Encodes RGBA pixels into a PNG `Buffer`.
228
-
229
- ## Security utilities
230
-
231
- ### `generateRaw256Key()`
232
-
233
- Generates a cryptographically random 32-byte key.
234
-
235
- ### `normalizeRaw256Key(key)`
236
-
237
- Normalizes a 32-byte key supplied as bytes, ArrayBuffer, or 64-character hexadecimal text.
238
-
239
- ### `bytesToHex(bytes)`
240
-
241
- Converts bytes to lowercase hexadecimal text.
242
-
243
- Security constants include:
244
-
245
- - `SECURITY_MODES`
246
- - `SECURITY_ALGORITHMS`
247
- - `SECURE_PAYLOAD_VERSION`
248
- - `DEFAULT_PBKDF2_ITERATIONS`
249
-
250
- ## WASM
251
-
252
- ### `initWasm(options?)`
253
-
254
- Loads and activates the optional bundled WASM accelerator.
255
-
256
- ```js
257
- import { initWasm } from "quadqr-js";
258
- await initWasm();
259
- ```
260
-
261
- An explicit URL or byte buffer can also be supplied.
262
-
263
- ### `getWasmState()`
264
-
265
- Returns the active WASM state or `null`.
266
-
267
- ### `disableWasm()`
268
-
269
- Returns the codec to the JavaScript implementation.
270
-
271
- ## Version and capacity
272
-
273
- ### `getVersionInfo(version, options?)`
274
-
275
- Returns geometry and payload information for a specific QuadQR version/ECC profile.
276
-
277
- ## CRC utilities
278
-
279
- ### `crc32(bytes)`
280
-
281
- Calculates QuadQR's CRC-32 value for a byte array.
282
-
283
- ### `installCrc32Accelerator(accelerator?)`
284
-
285
- Installs or removes a synchronous CRC-32 accelerator. Most applications should use `initWasm()` rather than calling this directly.
286
-
287
- ## Constants
288
-
289
- Common exported constants include:
290
-
291
- - `FORMAT_VERSION`
292
- - `MIN_VERSION`
293
- - `MAX_VERSION`
294
- - `DEFAULT_ECC_LEVEL`
295
- - `CELL`
296
- - `DEFAULT_PALETTE`
297
- - `RENDER_STYLES`
298
- - `ECC_LEVELS`
299
-
300
- ## Benchmark entry
301
-
302
- Benchmark helpers are available from `quadqr-js/benchmark`:
303
-
304
- ```js
305
- import {
306
- buildCapacityComparison,
307
- benchmarkCodec
308
- } from "quadqr-js/benchmark";
309
- ```
1
+ # API Reference
2
+
3
+ ## Core encoding
4
+
5
+ ### `encodeText(text, options?)`
6
+
7
+ Synchronously encodes UTF-8 text.
8
+
9
+ ```js
10
+ import { encodeText } from "quadqr-js";
11
+
12
+ const code = encodeText("Hello", {
13
+ ecc: "M",
14
+ version: 5
15
+ });
16
+ ```
17
+
18
+ If `version` is omitted, QuadQR selects the smallest version that fits.
19
+
20
+ ### `encodeBytes(bytes, options?)`
21
+
22
+ Encodes arbitrary bytes.
23
+
24
+ ```js
25
+ const code = encodeBytes(
26
+ new Uint8Array([1, 2, 3, 4]),
27
+ { ecc: "Q" }
28
+ );
29
+ ```
30
+
31
+ ## Secure encoding
32
+
33
+ ### `encodeSecureText(text, options)`
34
+
35
+ Encrypts text using Secure Payload v1 and encodes the encrypted envelope.
36
+
37
+ Password mode:
38
+
39
+ ```js
40
+ const code = await encodeSecureText("secret", {
41
+ security: {
42
+ mode: "password",
43
+ password: "example-password"
44
+ }
45
+ });
46
+ ```
47
+
48
+ Raw-key mode:
49
+
50
+ ```js
51
+ const code = await encodeSecureText("secret", {
52
+ security: {
53
+ mode: "raw-key",
54
+ key
55
+ }
56
+ });
57
+ ```
58
+
59
+ ### `encodeSecureBytes(bytes, options)`
60
+
61
+ Byte-oriented equivalent of `encodeSecureText()`.
62
+
63
+ ### `decryptDecoded(result, credentials)`
64
+
65
+ Unlocks a secure result returned by matrix, image, camera, or Node.js scanning.
66
+
67
+ ```js
68
+ const unlocked = await decryptDecoded(locked, {
69
+ password: "example-password"
70
+ });
71
+ ```
72
+
73
+ For raw-key mode:
74
+
75
+ ```js
76
+ const unlocked = await decryptDecoded(locked, { key });
77
+ ```
78
+
79
+ ## Matrix decoding
80
+
81
+ ### `decodeMatrix(matrix, options?)`
82
+
83
+ Decodes an already sampled QuadQR matrix.
84
+
85
+ ```js
86
+ const result = decodeMatrix(code.matrix);
87
+ console.log(result.text);
88
+ ```
89
+
90
+ A secure result remains locked until it is decrypted:
91
+
92
+ ```js
93
+ {
94
+ secure: true,
95
+ requiresDecryption: true,
96
+ text: null,
97
+ payload: Uint8Array(...),
98
+ security: {
99
+ mode: "password",
100
+ algorithm: "AES-256-GCM"
101
+ }
102
+ }
103
+ ```
104
+
105
+ ## Rendering
106
+
107
+ ### `renderToCanvas(codeOrMatrix, canvas, options?)`
108
+
109
+ Renders to an HTML canvas.
110
+
111
+ ```js
112
+ renderToCanvas(code, canvas, {
113
+ moduleSize: 12,
114
+ quietZone: 4,
115
+ style: "classic"
116
+ });
117
+ ```
118
+
119
+ Supported styles:
120
+
121
+ - `classic`
122
+ - `depth`
123
+ - `soft`
124
+ - `inset`
125
+
126
+ ### `renderToImageData(codeOrMatrix, options?)`
127
+
128
+ Returns runtime-neutral RGBA pixels:
129
+
130
+ ```js
131
+ {
132
+ width,
133
+ height,
134
+ data: Uint8ClampedArray
135
+ }
136
+ ```
137
+
138
+ ## Image and camera scanning
139
+
140
+ ### `scanImageData(imageData, options?)`
141
+
142
+ Scans an ImageData-like RGBA object. Clean frames use the normal observed-RGB path first. Difficult frames then get bounded fallback attempts using per-channel white balancing, spatial black/white normalization, tighter centre sampling, module-grid Auto Tone / Auto Contrast / Auto Color recovery, a rectified QR-region pixel enhancement pass, and sub-module geometry micro-refinement before the scan is rejected.
143
+
144
+ ```js
145
+ const result = scanImageData({
146
+ width,
147
+ height,
148
+ data
149
+ });
150
+ ```
151
+
152
+ Useful scanner options include `sampleRadius`, `robustSampleRadius`, `adaptiveSampling`, `spatialColorNormalization`, `autoEnhanceRecovery`, `rectifiedAutoEnhanceRecovery`, `rectifiedRecoveryModuleSize`, `autoEnhanceBlackClip`, `autoEnhanceWhiteClip`, `autoEnhanceSaturation`, `geometryRefinement`, `refinementOffset`, `structureTolerance`, and `maxErasureConfidence`. Auto enhancement and geometry refinement are enabled by default, but only run after the normal scan path fails.
153
+
154
+ ### Browser `scanFile(file, options?)`
155
+
156
+ Available from `quadqr-js/browser` and the browser-compatible main entry. Accepts a browser `File` or `Blob`.
157
+
158
+ ```js
159
+ const result = await scanFile(file);
160
+ ```
161
+
162
+ ### `scanVideoFrame(video, options?)`
163
+
164
+ Scans the current frame from an HTML video element. With the default `videoCropMode: "visible"`, an `object-fit: cover` preview is mapped back to the matching source crop before scanning, so hidden sensor pixels do not shrink the code or skew enhancement statistics.
165
+
166
+ ```js
167
+ const result = scanVideoFrame(video);
168
+ ```
169
+
170
+ ### `startCameraScanner(video, options?)`
171
+
172
+ Starts live camera scanning. On browsers that expose camera controls, QuadQR requests continuous autofocus, exposure, and white balance. It scans the CSS-visible preview region by default. Finder detection uses a QuadQR-specific RGB value channel (`max(R,G,B)`) on the fast pass so saturated blue/red/green data cells are not mistaken for structural black. If that fast pass fails, the **same captured frame** is retried through code-centric Auto Color recovery before finder detection. The default recovery sequence crops 8%, 16%, and 22% from the camera-frame edges, then falls back to the full frame. This prevents dark room pixels, browser UI, or monitor bezels outside the guide from controlling Auto Color and Otsu thresholds. Each crop uses the Photoshop-style per-channel shadow/highlight correction with a neutral mid-high highlight target (190 by default). Finder-only recovery also tries multiple center-weighted Auto Color histogram windows before raw threshold bracketing. The normal fast path is untouched; these extra passes run only after a miss. If geometry is found but color decoding fails, the captured ROI is retried with the stronger color/geometry recovery, and consecutive failed frames can still be combined with confidence-weighted module voting.
173
+
174
+ ```js
175
+ const scanner = await startCameraScanner(video, {
176
+ scanInterval: 120,
177
+ multiFrame: true,
178
+ multiFrameWindow: 4,
179
+ cameraAutoColorEvery: 1,
180
+ cameraAutoColorCropInsets: [0.08, 0.16, 0.22, 0],
181
+ cameraAutoColorHighlightPercentile: 0.95,
182
+ cameraAutoColorOutputHighlight: 190,
183
+ cameraAutoColorAnalysisInset: 0.10,
184
+ cameraAutoEnhanceEvery: 2,
185
+ cameraFinderRecoveryEvery: 2,
186
+ onResult(result) {
187
+ console.log(result);
188
+ },
189
+ onScanMiss() {
190
+ // Keep searching.
191
+ },
192
+ onDiagnostic(event) {
193
+ // Finder positions are in the scanner frame coordinate space.
194
+ // Use scanWidth/scanHeight to map them onto a video overlay.
195
+ if (event.type === "frame") {
196
+ console.log(event.method, event.finderCount, event.geometry?.version, event.elapsedMs);
197
+ }
198
+ }
199
+ });
200
+
201
+ scanner.stop();
202
+ ```
203
+
204
+ `onDiagnostic` is opt-in. It reports finder candidates, the locator method (`rgb-value-otsu`, threshold-bracket recovery, or luminance fallback), geometry data, and recovery state. Diagnostics reuse the scanner's existing passes and do not add a separate finder scan.
205
+
206
+ ### `rectifyDetectedCode(imageData, options?)`
207
+
208
+ Returns the perspective-corrected detected symbol image when detection succeeds.
209
+
210
+ ### `rotateMatrix(matrix, quarterTurns?)`
211
+
212
+ Rotates a matrix by 90-degree quarter turns.
213
+
214
+ ## Node.js helpers
215
+
216
+ Import from `quadqr-js/node`.
217
+
218
+ ### `toPNG(codeOrMatrix, options?)`
219
+
220
+ Returns a Node.js `Buffer` containing a PNG.
221
+
222
+ ### `savePNG(codeOrMatrix, filename, options?)`
223
+
224
+ Writes a PNG file.
225
+
226
+ ```js
227
+ await savePNG(code, "quadqr.png", {
228
+ moduleSize: 12,
229
+ quietZone: 4
230
+ });
231
+ ```
232
+
233
+ ### `scanBuffer(buffer, options?)`
234
+
235
+ Scans an image buffer. PNG is supported without dependencies. Other formats can use the host application's optional `sharp` installation.
236
+
237
+ ### Node `scanFile(filename, options?)`
238
+
239
+ Reads and scans an image file.
240
+
241
+ ### `decodePNG(buffer)`
242
+
243
+ Decodes supported PNG data into RGBA pixels.
244
+
245
+ ### `encodePNG(imageData, options?)`
246
+
247
+ Encodes RGBA pixels into a PNG `Buffer`.
248
+
249
+ ## Security utilities
250
+
251
+ ### `generateRaw256Key()`
252
+
253
+ Generates a cryptographically random 32-byte key.
254
+
255
+ ### `normalizeRaw256Key(key)`
256
+
257
+ Normalizes a 32-byte key supplied as bytes, ArrayBuffer, or 64-character hexadecimal text.
258
+
259
+ ### `bytesToHex(bytes)`
260
+
261
+ Converts bytes to lowercase hexadecimal text.
262
+
263
+ Security constants include:
264
+
265
+ - `SECURITY_MODES`
266
+ - `SECURITY_ALGORITHMS`
267
+ - `SECURE_PAYLOAD_VERSION`
268
+ - `DEFAULT_PBKDF2_ITERATIONS`
269
+
270
+ ## WASM
271
+
272
+ ### `initWasm(options?)`
273
+
274
+ Loads and activates the optional bundled WASM accelerator.
275
+
276
+ ```js
277
+ import { initWasm } from "quadqr-js";
278
+ await initWasm();
279
+ ```
280
+
281
+ An explicit URL or byte buffer can also be supplied.
282
+
283
+ ### `getWasmState()`
284
+
285
+ Returns the active WASM state or `null`.
286
+
287
+ ### `disableWasm()`
288
+
289
+ Returns the codec to the JavaScript implementation.
290
+
291
+ ## Version and capacity
292
+
293
+ ### `getVersionInfo(version, options?)`
294
+
295
+ Returns geometry and payload information for a specific QuadQR version/ECC profile.
296
+
297
+ ## CRC utilities
298
+
299
+ ### `crc32(bytes)`
300
+
301
+ Calculates QuadQR's CRC-32 value for a byte array.
302
+
303
+ ### `installCrc32Accelerator(accelerator?)`
304
+
305
+ Installs or removes a synchronous CRC-32 accelerator. Most applications should use `initWasm()` rather than calling this directly.
306
+
307
+ ## Constants
308
+
309
+ Common exported constants include:
310
+
311
+ - `FORMAT_VERSION`
312
+ - `MIN_VERSION`
313
+ - `MAX_VERSION`
314
+ - `DEFAULT_ECC_LEVEL`
315
+ - `CELL`
316
+ - `DEFAULT_PALETTE`
317
+ - `RENDER_STYLES`
318
+ - `ECC_LEVELS`
319
+
320
+ ## Benchmark entry
321
+
322
+ Benchmark helpers are available from `quadqr-js/benchmark`:
323
+
324
+ ```js
325
+ import {
326
+ buildCapacityComparison,
327
+ benchmarkCodec
328
+ } from "quadqr-js/benchmark";
329
+ ```