@wirunrom/hqr-generate 0.4.2 → 0.5.0
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 +21 -21
- package/README.md +165 -458
- package/index.d.ts +66 -68
- package/index.node.js +41 -33
- package/index.web.d.ts +2 -2
- package/index.web.js +60 -52
- package/package.json +90 -88
- package/pkg/nodejs/hqr_generate.js +10 -11
- package/pkg/nodejs/hqr_generate_bg.wasm +0 -0
- package/pkg/web/hqr_generate.js +9 -10
- package/pkg/web/hqr_generate_bg.wasm +0 -0
- package/react/useGenerate.js +11 -10
- package/react/useGenerateSvg.js +6 -2
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
The MIT License (MIT)
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Wirunrom
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
|
13
|
-
all copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
-
THE SOFTWARE.
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Wirunrom
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,458 +1,165 @@
|
|
|
1
|
-
# @wirunrom/hqr-generate
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
###
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
const [src, setSrc] = useState<string>("");
|
|
167
|
-
|
|
168
|
-
useEffect(() => {
|
|
169
|
-
const blob = new Blob([bytes], { type: "image/png" });
|
|
170
|
-
const url = URL.createObjectURL(blob);
|
|
171
|
-
setSrc(url);
|
|
172
|
-
return () => URL.revokeObjectURL(url);
|
|
173
|
-
}, [bytes]);
|
|
174
|
-
|
|
175
|
-
return <img src={src} alt="QR Code" />;
|
|
176
|
-
}
|
|
177
|
-
```
|
|
178
|
-
|
|
179
|
-
---
|
|
180
|
-
|
|
181
|
-
## Generate QR Code (SVG)
|
|
182
|
-
|
|
183
|
-
If you want to generate a QR code as **SVG** in React,
|
|
184
|
-
use the `useGenerateSvg` hook from the `/react` entry.
|
|
185
|
-
|
|
186
|
-
This hook:
|
|
187
|
-
|
|
188
|
-
- Calls the core `generate_svg()` API
|
|
189
|
-
- Converts SVG markup into a `Blob URL`
|
|
190
|
-
- Returns a `src` that can be used directly in `<img />`
|
|
191
|
-
- Automatically handles cleanup (`URL.revokeObjectURL`)
|
|
192
|
-
|
|
193
|
-
### Example
|
|
194
|
-
|
|
195
|
-
```tsx
|
|
196
|
-
"use client";
|
|
197
|
-
|
|
198
|
-
import { useGenerateSvg } from "@wirunrom/hqr-generate/react";
|
|
199
|
-
|
|
200
|
-
export default function QrSvg() {
|
|
201
|
-
const { src, loading } = useGenerateSvg("hello svg", {
|
|
202
|
-
size: 320,
|
|
203
|
-
ecc: "Q",
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
if (loading) return <p>Loading…</p>;
|
|
207
|
-
|
|
208
|
-
return src && <img src={src} alt="QR Code (SVG)" />;
|
|
209
|
-
}
|
|
210
|
-
```
|
|
211
|
-
|
|
212
|
-
---
|
|
213
|
-
|
|
214
|
-
### Returned Values
|
|
215
|
-
|
|
216
|
-
```ts
|
|
217
|
-
const {
|
|
218
|
-
src, // string | null (Blob URL for <img>)
|
|
219
|
-
svg, // string | null (raw SVG markup)
|
|
220
|
-
loading,
|
|
221
|
-
error,
|
|
222
|
-
} = useGenerateSvg(text, options);
|
|
223
|
-
```
|
|
224
|
-
|
|
225
|
-
### Design Notes
|
|
226
|
-
|
|
227
|
-
- SVG generation is **data-first** at the core level
|
|
228
|
-
- Rendering decisions are handled in the React hook
|
|
229
|
-
- `src` is provided for convenience and safety
|
|
230
|
-
- Advanced users can use `svg` to render inline SVG manually if needed
|
|
231
|
-
|
|
232
|
-
---
|
|
233
|
-
|
|
234
|
-
## Decode QR Code (Client-side)
|
|
235
|
-
|
|
236
|
-
To decode a QR code in the browser,
|
|
237
|
-
use the `useDecode` hook from the `/react` entry.
|
|
238
|
-
|
|
239
|
-
> ⚠️ `useDecode` accepts **ImageData only**
|
|
240
|
-
> If you have PNG bytes or an image URL, convert them to `ImageData` first using Canvas APIs.
|
|
241
|
-
|
|
242
|
-
### Example (decode from Canvas ImageData)
|
|
243
|
-
|
|
244
|
-
```tsx
|
|
245
|
-
"use client";
|
|
246
|
-
|
|
247
|
-
import { useDecode } from "@wirunrom/hqr-generate/react";
|
|
248
|
-
|
|
249
|
-
export default function DecodeQr({ imageData }: { imageData: ImageData }) {
|
|
250
|
-
const { text, loading, error } = useDecode(imageData);
|
|
251
|
-
|
|
252
|
-
if (loading) return <p>Decoding…</p>;
|
|
253
|
-
if (error) return <p>Failed to decode</p>;
|
|
254
|
-
|
|
255
|
-
return <p>Decoded text: {text}</p>;
|
|
256
|
-
}
|
|
257
|
-
```
|
|
258
|
-
|
|
259
|
-
---
|
|
260
|
-
|
|
261
|
-
## `useDecode` API Reference
|
|
262
|
-
|
|
263
|
-
### Signature
|
|
264
|
-
|
|
265
|
-
```ts
|
|
266
|
-
function useDecode(input?: ImageData): {
|
|
267
|
-
text: string | null;
|
|
268
|
-
loading: boolean;
|
|
269
|
-
error: unknown | null;
|
|
270
|
-
};
|
|
271
|
-
```
|
|
272
|
-
|
|
273
|
-
### Parameters
|
|
274
|
-
|
|
275
|
-
| Name | Type | Description |
|
|
276
|
-
| ------- | ---------------------- | -------------------------------------------------------------------- |
|
|
277
|
-
| `input` | `ImageData` (optional) | Image data containing a QR code. Decoding is skipped if `undefined`. |
|
|
278
|
-
|
|
279
|
-
### Returns
|
|
280
|
-
|
|
281
|
-
| Field | Type | Description |
|
|
282
|
-
| --------- | ----------------- | --------------------------------------------- |
|
|
283
|
-
| `text` | `string \| null` | Decoded QR text, or `null` if not decoded yet |
|
|
284
|
-
| `loading` | `boolean` | `true` while decoding is in progress |
|
|
285
|
-
| `error` | `unknown \| null` | Error object if decoding fails |
|
|
286
|
-
|
|
287
|
-
---
|
|
288
|
-
|
|
289
|
-
### Design Notes
|
|
290
|
-
|
|
291
|
-
- `useDecode` is **browser-only**
|
|
292
|
-
- It does **not** perform:
|
|
293
|
-
- image loading
|
|
294
|
-
- network requests
|
|
295
|
-
- Base64 conversion
|
|
296
|
-
|
|
297
|
-
- This keeps decoding:
|
|
298
|
-
- predictable
|
|
299
|
-
- side-effect free
|
|
300
|
-
- consistent with the binary-first core API
|
|
301
|
-
|
|
302
|
-
If you have an image URL or PNG bytes,
|
|
303
|
-
convert them to `ImageData` explicitly before calling `useDecode`.
|
|
304
|
-
|
|
305
|
-
---
|
|
306
|
-
|
|
307
|
-
## Plain HTML / Vanilla JavaScript Usage
|
|
308
|
-
|
|
309
|
-
The library can be used directly in **plain HTML** using ES Modules.
|
|
310
|
-
No framework or build tool is required.
|
|
311
|
-
|
|
312
|
-
> Note: The example assumes you are serving files over HTTP
|
|
313
|
-
> (do not open the file via `file://`), because WASM requires HTTP.
|
|
314
|
-
|
|
315
|
-
---
|
|
316
|
-
|
|
317
|
-
### Generate QR Code (PNG)
|
|
318
|
-
|
|
319
|
-
```html
|
|
320
|
-
<!doctype html>
|
|
321
|
-
<html lang="en">
|
|
322
|
-
<body>
|
|
323
|
-
<h3>Generate PNG QR</h3>
|
|
324
|
-
|
|
325
|
-
<input id="text" value="hello world" />
|
|
326
|
-
<button id="btn">Generate</button>
|
|
327
|
-
|
|
328
|
-
<br /><br />
|
|
329
|
-
<img id="img" />
|
|
330
|
-
|
|
331
|
-
<script type="module">
|
|
332
|
-
import { generate } from "https://esm.sh/@wirunrom/hqr-generate";
|
|
333
|
-
|
|
334
|
-
const btn = document.getElementById("btn");
|
|
335
|
-
const img = document.getElementById("img");
|
|
336
|
-
const input = document.getElementById("text");
|
|
337
|
-
|
|
338
|
-
btn.onclick = async () => {
|
|
339
|
-
const bytes = await generate(input.value, {
|
|
340
|
-
size: 256,
|
|
341
|
-
ecc: "Q",
|
|
342
|
-
});
|
|
343
|
-
|
|
344
|
-
const blob = new Blob([bytes], { type: "image/png" });
|
|
345
|
-
const url = URL.createObjectURL(blob);
|
|
346
|
-
img.src = url;
|
|
347
|
-
};
|
|
348
|
-
</script>
|
|
349
|
-
</body>
|
|
350
|
-
</html>
|
|
351
|
-
```
|
|
352
|
-
|
|
353
|
-
---
|
|
354
|
-
|
|
355
|
-
### Generate QR Code (SVG)
|
|
356
|
-
|
|
357
|
-
```html
|
|
358
|
-
<!doctype html>
|
|
359
|
-
<html lang="en">
|
|
360
|
-
<body>
|
|
361
|
-
<h3>Generate SVG QR</h3>
|
|
362
|
-
|
|
363
|
-
<input id="text" value="hello world" />
|
|
364
|
-
<button id="btn">Generate</button>
|
|
365
|
-
|
|
366
|
-
<br /><br />
|
|
367
|
-
<div id="svg"></div>
|
|
368
|
-
|
|
369
|
-
<script type="module">
|
|
370
|
-
import { generate_svg } from "https://esm.sh/@wirunrom/hqr-generate";
|
|
371
|
-
|
|
372
|
-
const btn = document.getElementById("btn");
|
|
373
|
-
const input = document.getElementById("text");
|
|
374
|
-
const container = document.getElementById("svg");
|
|
375
|
-
|
|
376
|
-
btn.onclick = async () => {
|
|
377
|
-
const svg = await generate_svg(input.value, {
|
|
378
|
-
size: 256,
|
|
379
|
-
ecc: "Q",
|
|
380
|
-
});
|
|
381
|
-
|
|
382
|
-
container.innerHTML = svg;
|
|
383
|
-
};
|
|
384
|
-
</script>
|
|
385
|
-
</body>
|
|
386
|
-
</html>
|
|
387
|
-
```
|
|
388
|
-
|
|
389
|
-
---
|
|
390
|
-
|
|
391
|
-
### Decode QR Code
|
|
392
|
-
|
|
393
|
-
```html
|
|
394
|
-
<!doctype html>
|
|
395
|
-
<html lang="en">
|
|
396
|
-
<body>
|
|
397
|
-
<h3>Decode QR</h3>
|
|
398
|
-
|
|
399
|
-
<input type="file" id="file" accept="image/*" />
|
|
400
|
-
<p id="result"></p>
|
|
401
|
-
|
|
402
|
-
<script type="module">
|
|
403
|
-
import { decode } from "https://esm.sh/@wirunrom/hqr-generate";
|
|
404
|
-
|
|
405
|
-
const fileInput = document.getElementById("file");
|
|
406
|
-
const result = document.getElementById("result");
|
|
407
|
-
|
|
408
|
-
fileInput.onchange = async () => {
|
|
409
|
-
const file = fileInput.files?.[0];
|
|
410
|
-
if (!file) return;
|
|
411
|
-
|
|
412
|
-
const img = new Image();
|
|
413
|
-
img.src = URL.createObjectURL(file);
|
|
414
|
-
|
|
415
|
-
img.onload = async () => {
|
|
416
|
-
const canvas = document.createElement("canvas");
|
|
417
|
-
canvas.width = img.width;
|
|
418
|
-
canvas.height = img.height;
|
|
419
|
-
|
|
420
|
-
const ctx = canvas.getContext("2d");
|
|
421
|
-
ctx.drawImage(img, 0, 0);
|
|
422
|
-
|
|
423
|
-
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
|
424
|
-
const text = await decode(imageData);
|
|
425
|
-
|
|
426
|
-
result.textContent = `Decoded text: ${text}`;
|
|
427
|
-
};
|
|
428
|
-
};
|
|
429
|
-
</script>
|
|
430
|
-
</body>
|
|
431
|
-
</html>
|
|
432
|
-
```
|
|
433
|
-
|
|
434
|
-
---
|
|
435
|
-
|
|
436
|
-
## What This Library Does NOT Do
|
|
437
|
-
|
|
438
|
-
- ❌ No Base64 or Data URL generation in the core
|
|
439
|
-
- ❌ No JPG / WebP / GIF rendering
|
|
440
|
-
- ❌ No DOM or framework-specific logic outside `/react`
|
|
441
|
-
|
|
442
|
-
---
|
|
443
|
-
|
|
444
|
-
## Summary
|
|
445
|
-
|
|
446
|
-
- Core → returns `Uint8Array`
|
|
447
|
-
- React hooks → handle `Blob` conversion
|
|
448
|
-
- SSR → returns raw bytes or `Response`
|
|
449
|
-
- Client → renders via `blob:` URL
|
|
450
|
-
|
|
451
|
-
> Keep data binary.
|
|
452
|
-
> Convert only at the UI boundary.
|
|
453
|
-
|
|
454
|
-
---
|
|
455
|
-
|
|
456
|
-
## Changelog
|
|
457
|
-
|
|
458
|
-
See [CHANGELOG.md](./CHANGELOG.md)
|
|
1
|
+
# @wirunrom/hqr-generate
|
|
2
|
+
|
|
3
|
+
Fast, scan-reliable **QR code generator and decoder** powered by **Rust + WebAssembly**.
|
|
4
|
+
|
|
5
|
+
Binary-first: core APIs return raw `Uint8Array` (PNG) or `string` (SVG). Rendering and Blob conversion are handled at the UI layer, so the library works identically in the browser, React, Next.js (App Router / SSR / Route Handlers), and Node.js.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- High-contrast **black & white only** — scan reliability first
|
|
12
|
+
- **1-bit grayscale PNG** output — tiny payloads
|
|
13
|
+
- **SVG** emitted as a single compact `<path>` — resolution-independent
|
|
14
|
+
- QR **decoding** from `Uint8Array` or browser `ImageData`
|
|
15
|
+
- Optional React hooks (`react` is an optional peer dep)
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## Performance
|
|
20
|
+
|
|
21
|
+
Sub-millisecond end-to-end generation on modern hardware. Benchmarked on Apple Silicon (release + LTO) for a typical URL payload at `size: 320`:
|
|
22
|
+
|
|
23
|
+
| Pipeline | Time | Output size |
|
|
24
|
+
| ------------------------- | ------ | ----------- |
|
|
25
|
+
| `generate` → 1-bit PNG | ~96 µs | ~5.5 KB |
|
|
26
|
+
| `generate_svg` → `<path>` | ~90 µs | ~5.2 KB |
|
|
27
|
+
|
|
28
|
+
Behind the numbers: QR encoding via [`fast_qr`](https://crates.io/crates/fast_qr), direct rasterization into a 1-bit PNG buffer (no 8-bit intermediate), run-length-merged SVG subpaths with relative commands, and React hooks that track primitive option fields so inline `opts` don't re-trigger WASM on every render.
|
|
29
|
+
|
|
30
|
+
Run the suite locally with `cargo bench --bench generate`.
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
## Installation
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
npm i @wirunrom/hqr-generate
|
|
38
|
+
# or: yarn add / pnpm i
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
## API
|
|
44
|
+
|
|
45
|
+
| Function | Parameters | Returns |
|
|
46
|
+
| ------------------------------ | --------------------------------------------- | --------------------------- |
|
|
47
|
+
| `generate(text, options?)` | `text: string`, `options?: GenerateOptions` | `Promise<Uint8Array>` (PNG) |
|
|
48
|
+
| `generate_svg(text, options?)` | `text: string`, `options?: GenerateOptions` | `Promise<string>` (SVG) |
|
|
49
|
+
| `decode(input)` | `input: Uint8Array \| ImageData` | `Promise<string>` |
|
|
50
|
+
|
|
51
|
+
### `GenerateOptions`
|
|
52
|
+
|
|
53
|
+
| Option | Type | Default | Description |
|
|
54
|
+
| -------- | -------------------------- | ------- | ----------------------------- |
|
|
55
|
+
| `size` | `number` | `320` | Output image size (px) |
|
|
56
|
+
| `margin` | `number` | `4` | Quiet zone / margin (modules) |
|
|
57
|
+
| `ecc` | `'L' \| 'M' \| 'Q' \| 'H'` | `'Q'` | Error correction level |
|
|
58
|
+
|
|
59
|
+
PNG output is raw bytes (`Uint8Array`), SVG output is plain markup (`string`). No Base64 or Data URL wrapping.
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
## Usage
|
|
64
|
+
|
|
65
|
+
### React (client-side)
|
|
66
|
+
|
|
67
|
+
The `/react` entry provides hooks that call the core API, wrap the result in a `Blob URL`, and revoke it on cleanup.
|
|
68
|
+
|
|
69
|
+
```tsx
|
|
70
|
+
"use client";
|
|
71
|
+
import { useGenerate, useGenerateSvg } from "@wirunrom/hqr-generate/react";
|
|
72
|
+
|
|
73
|
+
export function PngQr() {
|
|
74
|
+
const { src, loading } = useGenerate("hello world", { size: 320, ecc: "Q" });
|
|
75
|
+
if (loading) return <p>Loading…</p>;
|
|
76
|
+
return <img src={src ?? ""} alt="QR" />;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function SvgQr() {
|
|
80
|
+
const { src, svg, loading } = useGenerateSvg("hello svg", { size: 320 });
|
|
81
|
+
if (loading) return <p>Loading…</p>;
|
|
82
|
+
return <img src={src ?? ""} alt="QR" />; // or render `svg` inline
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Both hooks return `{ src, bytes|svg, loading, error }`.
|
|
87
|
+
|
|
88
|
+
### Next.js SSR / Route Handlers
|
|
89
|
+
|
|
90
|
+
Return the raw PNG bytes directly — no Base64.
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
import { generate } from "@wirunrom/hqr-generate";
|
|
94
|
+
|
|
95
|
+
export async function GET() {
|
|
96
|
+
const bytes = await generate("hello ssr");
|
|
97
|
+
return new Response(bytes, {
|
|
98
|
+
headers: {
|
|
99
|
+
"Content-Type": "image/png",
|
|
100
|
+
"Cache-Control": "public, max-age=60",
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
To generate on the server and render on the client, pass bytes to a client component and convert to a `Blob URL` there:
|
|
107
|
+
|
|
108
|
+
```tsx
|
|
109
|
+
// ClientQr.tsx
|
|
110
|
+
"use client";
|
|
111
|
+
import { useEffect, useState } from "react";
|
|
112
|
+
|
|
113
|
+
export default function ClientQr({ bytes }: { bytes: Uint8Array }) {
|
|
114
|
+
const [src, setSrc] = useState("");
|
|
115
|
+
useEffect(() => {
|
|
116
|
+
const url = URL.createObjectURL(new Blob([bytes], { type: "image/png" }));
|
|
117
|
+
setSrc(url);
|
|
118
|
+
return () => URL.revokeObjectURL(url);
|
|
119
|
+
}, [bytes]);
|
|
120
|
+
return <img src={src} alt="QR" />;
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Decoding
|
|
125
|
+
|
|
126
|
+
```tsx
|
|
127
|
+
"use client";
|
|
128
|
+
import { useDecode } from "@wirunrom/hqr-generate/react";
|
|
129
|
+
|
|
130
|
+
export default function DecodeQr({ imageData }: { imageData: ImageData }) {
|
|
131
|
+
const { text, loading, error } = useDecode(imageData);
|
|
132
|
+
if (loading) return <p>Decoding…</p>;
|
|
133
|
+
if (error) return <p>Failed to decode</p>;
|
|
134
|
+
return <p>{text}</p>;
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
`useDecode` accepts **`ImageData` only**. If you have PNG bytes or a URL, draw them to a canvas first and call `ctx.getImageData(...)`. The core `decode()` also accepts raw `Uint8Array` (PNG/JPG/WebP) — use that for server-side decoding.
|
|
139
|
+
|
|
140
|
+
### Vanilla JS
|
|
141
|
+
|
|
142
|
+
ES modules over HTTP (WASM won't load from `file://`):
|
|
143
|
+
|
|
144
|
+
```html
|
|
145
|
+
<script type="module">
|
|
146
|
+
import { generate, generate_svg, decode } from "https://esm.sh/@wirunrom/hqr-generate";
|
|
147
|
+
|
|
148
|
+
// PNG
|
|
149
|
+
const bytes = await generate("hello", { size: 256 });
|
|
150
|
+
const url = URL.createObjectURL(new Blob([bytes], { type: "image/png" }));
|
|
151
|
+
document.querySelector("img").src = url;
|
|
152
|
+
|
|
153
|
+
// SVG
|
|
154
|
+
document.querySelector("#svg").innerHTML = await generate_svg("hello");
|
|
155
|
+
|
|
156
|
+
// Decode (from a canvas ImageData or raw PNG/JPG Uint8Array)
|
|
157
|
+
// const text = await decode(imageData);
|
|
158
|
+
</script>
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
---
|
|
162
|
+
|
|
163
|
+
## Changelog
|
|
164
|
+
|
|
165
|
+
See [CHANGELOG.md](./CHANGELOG.md).
|