@wirunrom/hqr-generate 0.2.23 → 0.3.11

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 ADDED
@@ -0,0 +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.
package/README.md CHANGED
@@ -1,151 +1,230 @@
1
- # @wirunrom/hqr-generate
2
-
3
- A **stable black-and-white QR Code generator** that returns **PNG Data URLs or PNG bytes**, powered by **Rust + WebAssembly (WASM)**.
4
-
5
- This library is designed with a **scan-reliability-first** mindset and a **frontend-first API**, making it easy to use in modern web applications without additional configuration.
6
-
7
- ---
8
-
9
- ## Features
10
-
11
- - High-contrast **black & white only** output (maximum scan compatibility)
12
- - Optimized for both **old and new mobile devices**
13
- - Deterministic and consistent QR output
14
- - Lightweight and fast (**Rust + WASM**)
15
- - Supports:
16
- - **PNG Data URL** (simple usage)
17
- - **PNG raw bytes** (best performance, no Base64 overhead)
18
- - Works out of the box with:
19
- - Plain HTML / JavaScript
20
- - React
21
- - Next.js (Pages Router & App Router)
22
- - Modern bundlers (Vite, Webpack, etc.)
23
-
24
- ---
25
-
26
- ## Installation
27
-
28
- ```bash
29
- npm install @wirunrom/hqr-generate
30
- ```
31
-
32
- ## Basic Usage (Bundler / React / Next.js)
33
-
34
- **Generate PNG Data URL**
35
-
36
- Simple and widely compatible. Recommended for most use cases.
37
-
38
- ```ts
39
- import { qrPngDataUrl } from "@wirunrom/hqr-generate";
40
-
41
- const src = await qrPngDataUrl("hello world", {
42
- size: 320,
43
- margin: 4,
44
- ecc: "Q",
45
- });
46
-
47
- <img src={src} alt="QR Code" />
48
- ```
49
-
50
- **Generate PNG Bytes (Best Performance)**
51
-
52
- Using raw bytes avoids Base64 overhead and is more memory-efficient.
53
-
54
- ```ts
55
- import { qrPngBytes } from "@wirunrom/hqr-generate";
56
-
57
- const bytes = await qrPngBytes("hello world", {
58
- size: 320,
59
- margin: 4,
60
- ecc: "Q",
61
- });
62
-
63
- const url = URL.createObjectURL(
64
- new Blob([bytes], { type: "image/png" })
65
- );
66
-
67
- <img src={url} alt="QR Code" />
68
-
69
- ```
70
-
71
- ## React Hook Helper (/react)
72
-
73
- For React or Next.js applications, the library provides idiomatic React hooks that automatically update when inputs change.
74
-
75
- **useQrPngDataUrl**
76
-
77
- Generates a PNG Data URL and updates automatically when dependencies change.
78
-
79
- ```ts
80
- import { useQrPngDataUrl } from "@wirunrom/hqr-generate/react";
81
-
82
- function QR() {
83
- const src = useQrPngDataUrl("hello world", {
84
- size: 320,
85
- margin: 4,
86
- ecc: "Q",
87
- });
88
-
89
- if (!src) return null;
90
- return <img src={src} alt="QR Code" />;
91
- }
92
- ```
93
-
94
- **useQrPngBlobUrl**
95
-
96
- Generates a Blob URL instead of a Base64 Data URL.
97
- Recommended for larger QR codes or frequent updates.
98
-
99
- ```ts
100
- import { useQrPngBlobUrl } from "@wirunrom/hqr-generate/react";
101
-
102
- function QR() {
103
- const src = useQrPngBlobUrl("hello world", {
104
- size: 320,
105
- margin: 4,
106
- ecc: "Q",
107
- });
108
-
109
- if (!src) return null;
110
- return <img src={src} alt="QR Code" />;
111
- }
112
- ```
113
-
114
- ## Plain HTML / No Bundler (/web)
115
-
116
- Use this entry when working with static HTML, CDN, or environments without a bundler.
117
-
118
- ```html
119
- <script type="module">
120
- import { qr_png_bytes, qr_png_data_url } from "@wirunrom/hqr-generate/web";
121
-
122
- const src = await qr_png_bytes("hello world", {
123
- size: 320,
124
- margin: 4,
125
- ecc: "Q",
126
- });
127
-
128
- // or
129
-
130
- const src = await qr_png_data_url("hello world", {
131
- size: 320,
132
- margin: 4,
133
- ecc: "Q",
134
- });
135
-
136
- document.getElementById("qr").src = src;
137
- </script>
138
-
139
- <img id="qr" />
140
- ```
141
-
142
- ## API Reference
143
-
144
- Generate a QR code and return a PNG Data URL.
145
-
146
- | Name | Type | Default | Description |
147
- | ------ | -------------------------- | ------- | ---------------------------- |
148
- | text | `string` | — | Text to encode |
149
- | size | `number` | `320` | Image size in pixels |
150
- | margin | `number` | `4` | Quiet zone (recommended ≥ 4) |
151
- | ecc | `"L" \| "M" \| "Q" \| "H"` | `"Q"` | Error correction level |
1
+ # @wirunrom/hqr-generate
2
+
3
+ A high-performance **QR Code generator and decoder** with
4
+ **black-and-white only output**, powered by **Rust + WebAssembly (WASM)**.
5
+
6
+ Supports both **QR generation** and **QR decoding (scan)** in modern web applications.
7
+
8
+ This library is designed with a **scan-reliability-first** mindset and a **frontend-first API**, making it easy to use in modern web applications without additional configuration.
9
+
10
+ ---
11
+
12
+ ## Features
13
+
14
+ - High-contrast **black & white only** output (maximum scan compatibility)
15
+ - Optimized for both **old and new mobile devices**
16
+ - Deterministic and consistent QR output
17
+ - Lightweight and fast (**Rust + WASM**)
18
+ - QR decoding (scan) from browser ImageData
19
+ - Supports:
20
+ - **PNG Data URL** (simple usage)
21
+ - **PNG raw bytes** (best performance, no Base64 overhead)
22
+ - Works out of the box with:
23
+ - Plain HTML / JavaScript
24
+ - React
25
+ - Next.js Client Component (Pages Router & App Router)
26
+ - Modern bundlers (Vite, Webpack, etc.)
27
+
28
+ ---
29
+
30
+ ## Installation
31
+
32
+ ```bash
33
+ npm install @wirunrom/hqr-generate
34
+ ```
35
+
36
+ ## API Reference (GenerateQR)
37
+
38
+ Generate a QR code and return a PNG Data URL.
39
+
40
+ Parameters
41
+
42
+ | Name | Type | Default | Description |
43
+ | ------ | -------------------------- | ------- | ---------------------------- |
44
+ | text | `string` | — | Text to encode |
45
+ | size | `number` | `320` | Image size in pixels |
46
+ | margin | `number` | `4` | Quiet zone (recommended ≥ 4) |
47
+ | ecc | `"L" \| "M" \| "Q" \| "H"` | `"Q"` | Error correction level |
48
+
49
+ ## Basic Usage [Bundler / React / Next.js (Client)]
50
+
51
+ **Generate PNG Data URL**
52
+
53
+ Simple and widely compatible. Recommended for most use cases.
54
+
55
+ ```ts
56
+ import { qr_png_data_url } from "@wirunrom/hqr-generate";
57
+
58
+ const src = await qr_png_data_url("hello world", 320, 4, "Q");
59
+
60
+ <img src={src} alt="QR Code" />
61
+ ```
62
+
63
+ **Generate PNG Bytes (Best Performance)**
64
+
65
+ Using raw bytes avoids Base64 overhead and is more memory-efficient.
66
+
67
+ ```ts
68
+ import { qr_png_bytes } from "@wirunrom/hqr-generate";
69
+
70
+ const bytes = await qr_png_bytes("hello world", 320, 4, "Q");
71
+
72
+ const url = URL.createObjectURL(
73
+ new Blob([bytes], { type: "image/png" })
74
+ );
75
+
76
+ <img src={url} alt="QR Code" />
77
+ ```
78
+
79
+ **Decode QR Code**
80
+
81
+ Decode a QR code from browser ImageData (Canvas / camera / image upload friendly).
82
+
83
+ ```ts
84
+ import { qr_decode_from_image } from "@wirunrom/hqr-generate";
85
+
86
+ const imageData = ctx.getImageData(0, 0, width, height);
87
+ const text = await qr_decode_from_image(imageData);
88
+
89
+ console.log(text);
90
+ ```
91
+
92
+ Decode runs entirely in WASM and does not require server-side processing.
93
+
94
+ ## React Hook Helper (/react)
95
+
96
+ For React or Next.js applications, the library provides idiomatic React hooks that manage async state automatically.
97
+
98
+ **useQrPngDataUrl**
99
+
100
+ Generates a PNG Data URL and updates automatically when inputs change.
101
+
102
+ ```ts
103
+ import { useQrPngDataUrl } from "@wirunrom/hqr-generate/react";
104
+
105
+ function QR() {
106
+ const src = useQrPngDataUrl("hello world", {
107
+ size: 320,
108
+ margin: 4,
109
+ ecc: "Q",
110
+ });
111
+
112
+ if (!src) return null;
113
+ return <img src={src} alt="QR Code" />;
114
+ }
115
+ ```
116
+
117
+ **useQrPngBlobUrl**
118
+
119
+ Generates a Blob URL instead of a Base64 Data URL.
120
+ Recommended for larger QR codes or frequent updates.
121
+
122
+ ```ts
123
+ import { useQrPngBlobUrl } from "@wirunrom/hqr-generate/react";
124
+
125
+ function QR() {
126
+ const src = useQrPngBlobUrl("hello world", {
127
+ size: 320,
128
+ margin: 4,
129
+ ecc: "Q",
130
+ });
131
+
132
+ if (!src) return null;
133
+ return <img src={src} alt="QR Code" />;
134
+ }
135
+ ```
136
+
137
+ **useQrDecodeFromImageData**
138
+
139
+ Decode a QR code from browser ImageData.
140
+
141
+ ```ts
142
+ import { useQrDecodeFromImageData } from "@wirunrom/hqr-generate/react";
143
+
144
+ function Scanner({ image }: { image: ImageData | null }) {
145
+ const { text, loading, error } = useQrDecodeFromImageData(image);
146
+
147
+ if (loading) return <div>Scanning…</div>;
148
+ if (error) return <div>Error</div>;
149
+ if (!text) return null;
150
+
151
+ return <div>QR: {text}</div>;
152
+ }
153
+ ```
154
+
155
+ Parameters
156
+
157
+ | Name | Type | Description |
158
+ | ----- | ----------- | ------------------------------------------- |
159
+ | image | `ImageData` | RGBA image data from Canvas or Camera frame |
160
+
161
+ Returns
162
+
163
+ - `Promise<string>`
164
+ - Resolves with decoded QR text
165
+ - Rejects if no QR code is detected or image is invalid
166
+
167
+ Notes
168
+
169
+ - Input must be RGBA ImageData
170
+ - Decode runs entirely in WASM
171
+ - No server or backend required
172
+ - Best results with:
173
+ - High-contrast QR codes
174
+ - Minimal blur
175
+ - Proper quiet zone
176
+
177
+ **useQrDecodeFromImageSrc**
178
+
179
+ Decode a QR code from your image path.
180
+
181
+ ```ts
182
+ import { useQrDecodeFromImageSrc } from "@wirunrom/hqr-generate/react";
183
+
184
+ function DecodeQR({ imagePath }: { imagePath: string | null }) {
185
+ const { text } = useQrDecodeFromImageSrc(imagePath);
186
+
187
+ if (!text) return null;
188
+
189
+ return <div>Decode QR: {text}</div>;
190
+ }
191
+ ```
192
+
193
+ Parameters
194
+
195
+ | Name | Type | Description |
196
+ | ---- | -------- | --------------- |
197
+ | src | `string` | Your image path |
198
+
199
+ Returns
200
+
201
+ - `Promise<string>`
202
+ - Resolves with decoded QR text
203
+
204
+ ## Plain HTML / No Bundler (/web)
205
+
206
+ Use this entry when working with static HTML, CDN, or environments without a bundler.
207
+
208
+ ```html
209
+ <script type="module">
210
+ import {
211
+ qr_png_bytes,
212
+ qr_png_data_url,
213
+ qr_decode_from_image_data,
214
+ } from "@wirunrom/hqr-generate/web";
215
+
216
+ const src = await qr_png_data_url("hello world", 320, 4, "Q");
217
+ document.getElementById("qr").src = src;
218
+
219
+ // decode example (canvas)
220
+ const canvas = document.getElementById("c");
221
+ const ctx = canvas.getContext("2d");
222
+ const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
223
+
224
+ const text = await qr_decode_from_image_data(imageData);
225
+ console.log(text);
226
+ </script>
227
+
228
+ <img id="qr" />
229
+ <canvas id="c" hidden></canvas>
230
+ ```
package/index.bundler.js CHANGED
@@ -1,34 +1,59 @@
1
- import init, {
2
- qr_png_data_url as _qr_png_data_url,
3
- qr_png_bytes as _qr_png_bytes,
4
- } from "./pkg/bundler/hqr_generate.js";
5
-
6
- let _initPromise;
7
-
8
- function eccToU8(ecc) {
9
- switch (ecc) {
10
- case "L":
11
- return 0;
12
- case "M":
13
- return 1;
14
- case "H":
15
- return 3;
16
- default:
17
- return 2;
18
- }
19
- }
20
-
21
- async function ensureInit() {
22
- if (!_initPromise) _initPromise = init();
23
- await _initPromise;
24
- }
25
-
26
- export async function qr_png_data_url(text, size = 320, margin = 4, ecc = "Q") {
27
- await ensureInit();
28
- return _qr_png_data_url(text, size, margin, eccToU8(ecc));
29
- }
30
-
31
- export async function qr_png_bytes(text, size = 320, margin = 4, ecc = "Q") {
32
- await ensureInit();
33
- return _qr_png_bytes(text, size, margin, eccToU8(ecc));
34
- }
1
+ import init, {
2
+ qr_png_data_url as _qr_png_data_url,
3
+ qr_png_bytes as _qr_png_bytes,
4
+ qr_decode_from_rgba as _qr_decode_from_rgba,
5
+ } from "./pkg/bundler/hqr_generate.js";
6
+
7
+ let _initPromise;
8
+
9
+ function eccToU8(ecc) {
10
+ switch (ecc) {
11
+ case "L":
12
+ return 0;
13
+ case "M":
14
+ return 1;
15
+ case "H":
16
+ return 3;
17
+ default:
18
+ return 2;
19
+ }
20
+ }
21
+
22
+ /** @returns {Promise<void>} */
23
+ async function ensureInit() {
24
+ _initPromise ??= init();
25
+ return _initPromise;
26
+ }
27
+
28
+ /**
29
+ * @param {string} text
30
+ * @param {number} [size=320]
31
+ * @param {number} [margin=4]
32
+ * @param {"L"|"M"|"Q"|"H"} [ecc="Q"]
33
+ * @returns {Promise<string>}
34
+ */
35
+ export async function qr_png_data_url(text, size = 320, margin = 4, ecc = "Q") {
36
+ await ensureInit();
37
+ return _qr_png_data_url(text, size, margin, eccToU8(ecc));
38
+ }
39
+
40
+ /**
41
+ * @param {string} text
42
+ * @param {number} [size=320]
43
+ * @param {number} [margin=4]
44
+ * @param {"L"|"M"|"Q"|"H"} [ecc="Q"]
45
+ * @returns {Promise<Uint8Array>}
46
+ */
47
+ export async function qr_png_bytes(text, size = 320, margin = 4, ecc = "Q") {
48
+ await ensureInit();
49
+ return _qr_png_bytes(text, size, margin, eccToU8(ecc));
50
+ }
51
+
52
+ /**
53
+ * @param {ImageData} image
54
+ * @returns {Promise<string>}
55
+ */
56
+ export async function qr_decode_from_image_data(image) {
57
+ await ensureInit();
58
+ return _qr_decode_from_rgba(image.width, image.height, image.data);
59
+ }
package/index.d.ts CHANGED
@@ -1,22 +1,29 @@
1
- export type QrEcc = "L" | "M" | "Q" | "H";
2
-
3
- /**
4
- * Generate QR code PNG as Data URL (base64).
5
- */
6
- export declare function qr_png_data_url(
7
- text: string,
8
- size?: number,
9
- margin?: number,
10
- ecc?: QrEcc
11
- ): Promise<string>;
12
-
13
- /**
14
- * Generate QR code PNG as raw bytes (Uint8Array).
15
- * Recommended for best performance (Blob URL, upload, caching).
16
- */
17
- export declare function qr_png_bytes(
18
- text: string,
19
- size?: number,
20
- margin?: number,
21
- ecc?: QrEcc
22
- ): Promise<Uint8Array>;
1
+ export type QrEcc = "L" | "M" | "Q" | "H";
2
+
3
+ /**
4
+ * Generate QR code PNG as Data URL (base64).
5
+ */
6
+ export declare function qr_png_data_url(
7
+ text: string,
8
+ size?: number,
9
+ margin?: number,
10
+ ecc?: QrEcc
11
+ ): Promise<string>;
12
+
13
+ /**
14
+ * Generate QR code PNG as raw bytes (Uint8Array).
15
+ * Recommended for best performance (Blob URL, upload, caching).
16
+ */
17
+ export declare function qr_png_bytes(
18
+ text: string,
19
+ size?: number,
20
+ margin?: number,
21
+ ecc?: QrEcc
22
+ ): Promise<Uint8Array>;
23
+
24
+ /**
25
+ * Decode QR from browser ImageData
26
+ */
27
+ export function qr_decode_from_image_data(
28
+ image: ImageData
29
+ ): Promise<string>;
package/index.web.js CHANGED
@@ -1,42 +1,61 @@
1
- import init, {
2
- qr_png_data_url as _qr_png_data_url,
3
- qr_png_bytes as _qr_png_bytes,
4
- } from "./pkg/web/hqr_generate.js";
5
-
6
- let _initPromise;
7
-
8
- function eccToU8(ecc) {
9
- switch (ecc) {
10
- case "L":
11
- return 0;
12
- case "M":
13
- return 1;
14
- case "H":
15
- return 3;
16
- default:
17
- return 2;
18
- }
19
- }
20
-
21
- /** @returns {Promise<void>} */
22
- async function ensureInit() {
23
- if (!_initPromise) _initPromise = init();
24
- await _initPromise;
25
- }
26
-
27
- /**
28
- * @param {string} text
29
- * @param {number} [size=320]
30
- * @param {number} [margin=4]
31
- * @param {"L"|"M"|"Q"|"H"} [ecc="Q"]
32
- * @returns {Promise<string>}
33
- */
34
- export async function qr_png_data_url(text, size = 320, margin = 4, ecc = "Q") {
35
- await ensureInit();
36
- return _qr_png_data_url(text, size, margin, eccToU8(ecc));
37
- }
38
-
39
- export async function qr_png_bytes(text, size = 320, margin = 4, ecc = "Q") {
40
- await ensureInit();
41
- return _qr_png_bytes(text, size, margin, eccToU8(ecc));
42
- }
1
+ import init, {
2
+ qr_png_data_url as _qr_png_data_url,
3
+ qr_png_bytes as _qr_png_bytes,
4
+ qr_decode_from_rgba as _qr_decode_from_rgba,
5
+ } from "./pkg/web/hqr_generate.js";
6
+
7
+ let _initPromise;
8
+
9
+ function eccToU8(ecc) {
10
+ switch (ecc) {
11
+ case "L":
12
+ return 0;
13
+ case "M":
14
+ return 1;
15
+ case "H":
16
+ return 3;
17
+ default:
18
+ return 2;
19
+ }
20
+ }
21
+
22
+ /** @returns {Promise<void>} */
23
+ async function ensureInit() {
24
+ _initPromise ??= init();
25
+ return _initPromise;
26
+ }
27
+
28
+ /**
29
+ * @param {string} text
30
+ * @param {number} [size=320]
31
+ * @param {number} [margin=4]
32
+ * @param {"L"|"M"|"Q"|"H"} [ecc="Q"]
33
+ * @returns {Promise<string>}
34
+ */
35
+ export async function qr_png_data_url(text, size = 320, margin = 4, ecc = "Q") {
36
+ await ensureInit();
37
+ return _qr_png_data_url(text, size, margin, eccToU8(ecc));
38
+ }
39
+
40
+ /**
41
+ * @param {string} text
42
+ * @param {number} [size=320]
43
+ * @param {number} [margin=4]
44
+ * @param {"L"|"M"|"Q"|"H"} [ecc="Q"]
45
+ * @returns {Promise<Uint8Array>}
46
+ */
47
+ export async function qr_png_bytes(text, size = 320, margin = 4, ecc = "Q") {
48
+ await ensureInit();
49
+ return _qr_png_bytes(text, size, margin, eccToU8(ecc));
50
+ }
51
+
52
+ /**
53
+ * Decode QR from Image (browser/canvas friendly)
54
+ *
55
+ * @param {ImageData} image
56
+ * @returns {Promise<string>}
57
+ */
58
+ export async function qr_decode_from_image_data(image) {
59
+ await ensureInit();
60
+ return _qr_decode_from_rgba(image.width, image.height, image.data);
61
+ }