@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 +21 -0
- package/README.md +230 -151
- package/index.bundler.js +59 -34
- package/index.d.ts +29 -22
- package/index.web.js +61 -42
- package/package.json +66 -64
- package/pkg/bundler/LICENSE +21 -0
- package/pkg/bundler/README.md +230 -151
- package/pkg/bundler/hqr_generate.d.ts +2 -0
- package/pkg/bundler/hqr_generate.js +1 -1
- package/pkg/bundler/hqr_generate_bg.js +32 -0
- package/pkg/bundler/hqr_generate_bg.wasm +0 -0
- package/pkg/bundler/hqr_generate_bg.wasm.d.ts +3 -2
- package/pkg/bundler/package.json +1 -1
- package/pkg/web/LICENSE +21 -0
- package/pkg/web/README.md +230 -151
- package/pkg/web/hqr_generate.d.ts +5 -2
- package/pkg/web/hqr_generate.js +32 -0
- package/pkg/web/hqr_generate_bg.wasm +0 -0
- package/pkg/web/hqr_generate_bg.wasm.d.ts +3 -2
- package/pkg/web/package.json +1 -1
- package/react/index.d.ts +12 -17
- package/react/index.js +4 -91
- package/react/useQrDecodeFromImageData.d.ts +5 -0
- package/react/useQrDecodeFromImageData.js +47 -0
- package/react/useQrDecodeFromImageSrc.d.ts +6 -0
- package/react/useQrDecodeFromImageSrc.js +59 -0
- package/react/useQrPngBlobUrl.d.ts +6 -0
- package/react/useQrPngBlobUrl.js +52 -0
- package/react/useQrPngDataUrl.d.ts +6 -0
- package/react/useQrPngDataUrl.js +41 -0
package/pkg/web/README.md
CHANGED
|
@@ -1,151 +1,230 @@
|
|
|
1
|
-
# @wirunrom/hqr-generate
|
|
2
|
-
|
|
3
|
-
A
|
|
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
|
-
|
|
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
|
+
```
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
/* tslint:disable */
|
|
2
2
|
/* eslint-disable */
|
|
3
3
|
|
|
4
|
+
export function qr_decode_from_rgba(width: number, height: number, rgba: Uint8Array): string;
|
|
5
|
+
|
|
4
6
|
export function qr_png_bytes(text: string, size: number, margin: number, ecc: number): Uint8Array;
|
|
5
7
|
|
|
6
8
|
export function qr_png_data_url(text: string, size: number, margin: number, ecc: number): string;
|
|
@@ -9,13 +11,14 @@ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembl
|
|
|
9
11
|
|
|
10
12
|
export interface InitOutput {
|
|
11
13
|
readonly memory: WebAssembly.Memory;
|
|
14
|
+
readonly qr_decode_from_rgba: (a: number, b: number, c: any) => [number, number, number, number];
|
|
12
15
|
readonly qr_png_bytes: (a: number, b: number, c: number, d: number, e: number) => [number, number, number];
|
|
13
16
|
readonly qr_png_data_url: (a: number, b: number, c: number, d: number, e: number) => [number, number, number, number];
|
|
14
17
|
readonly __wbindgen_externrefs: WebAssembly.Table;
|
|
15
|
-
readonly __wbindgen_malloc: (a: number, b: number) => number;
|
|
16
|
-
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
17
18
|
readonly __externref_table_dealloc: (a: number) => void;
|
|
18
19
|
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
20
|
+
readonly __wbindgen_malloc: (a: number, b: number) => number;
|
|
21
|
+
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
19
22
|
readonly __wbindgen_start: () => void;
|
|
20
23
|
}
|
|
21
24
|
|
package/pkg/web/hqr_generate.js
CHANGED
|
@@ -1,5 +1,30 @@
|
|
|
1
1
|
/* @ts-self-types="./hqr_generate.d.ts" */
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* @param {number} width
|
|
5
|
+
* @param {number} height
|
|
6
|
+
* @param {Uint8Array} rgba
|
|
7
|
+
* @returns {string}
|
|
8
|
+
*/
|
|
9
|
+
export function qr_decode_from_rgba(width, height, rgba) {
|
|
10
|
+
let deferred2_0;
|
|
11
|
+
let deferred2_1;
|
|
12
|
+
try {
|
|
13
|
+
const ret = wasm.qr_decode_from_rgba(width, height, rgba);
|
|
14
|
+
var ptr1 = ret[0];
|
|
15
|
+
var len1 = ret[1];
|
|
16
|
+
if (ret[3]) {
|
|
17
|
+
ptr1 = 0; len1 = 0;
|
|
18
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
19
|
+
}
|
|
20
|
+
deferred2_0 = ptr1;
|
|
21
|
+
deferred2_1 = len1;
|
|
22
|
+
return getStringFromWasm0(ptr1, len1);
|
|
23
|
+
} finally {
|
|
24
|
+
wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
3
28
|
/**
|
|
4
29
|
* @param {string} text
|
|
5
30
|
* @param {number} size
|
|
@@ -51,10 +76,17 @@ function __wbg_get_imports() {
|
|
|
51
76
|
__wbg___wbindgen_throw_be289d5034ed271b: function(arg0, arg1) {
|
|
52
77
|
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
53
78
|
},
|
|
79
|
+
__wbg_length_32ed9a279acd054c: function(arg0) {
|
|
80
|
+
const ret = arg0.length;
|
|
81
|
+
return ret;
|
|
82
|
+
},
|
|
54
83
|
__wbg_new_from_slice_a3d2629dc1826784: function(arg0, arg1) {
|
|
55
84
|
const ret = new Uint8Array(getArrayU8FromWasm0(arg0, arg1));
|
|
56
85
|
return ret;
|
|
57
86
|
},
|
|
87
|
+
__wbg_prototypesetcall_bdcdcc5842e4d77d: function(arg0, arg1, arg2) {
|
|
88
|
+
Uint8Array.prototype.set.call(getArrayU8FromWasm0(arg0, arg1), arg2);
|
|
89
|
+
},
|
|
58
90
|
__wbindgen_cast_0000000000000001: function(arg0, arg1) {
|
|
59
91
|
// Cast intrinsic for `Ref(String) -> Externref`.
|
|
60
92
|
const ret = getStringFromWasm0(arg0, arg1);
|
|
Binary file
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/* tslint:disable */
|
|
2
2
|
/* eslint-disable */
|
|
3
3
|
export const memory: WebAssembly.Memory;
|
|
4
|
+
export const qr_decode_from_rgba: (a: number, b: number, c: any) => [number, number, number, number];
|
|
4
5
|
export const qr_png_bytes: (a: number, b: number, c: number, d: number, e: number) => [number, number, number];
|
|
5
6
|
export const qr_png_data_url: (a: number, b: number, c: number, d: number, e: number) => [number, number, number, number];
|
|
6
7
|
export const __wbindgen_externrefs: WebAssembly.Table;
|
|
7
|
-
export const __wbindgen_malloc: (a: number, b: number) => number;
|
|
8
|
-
export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
9
8
|
export const __externref_table_dealloc: (a: number) => void;
|
|
10
9
|
export const __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
10
|
+
export const __wbindgen_malloc: (a: number, b: number) => number;
|
|
11
|
+
export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
11
12
|
export const __wbindgen_start: () => void;
|
package/pkg/web/package.json
CHANGED
package/react/index.d.ts
CHANGED
|
@@ -1,17 +1,12 @@
|
|
|
1
|
-
import type { QrEcc } from "../index";
|
|
2
|
-
|
|
3
|
-
export interface UseQrPngOptions {
|
|
4
|
-
size?: number;
|
|
5
|
-
margin?: number;
|
|
6
|
-
ecc?: QrEcc;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export declare function useQrPngBlobUrl(
|
|
15
|
-
text: string,
|
|
16
|
-
opts?: UseQrPngOptions
|
|
17
|
-
): string;
|
|
1
|
+
import type { QrEcc } from "../index";
|
|
2
|
+
|
|
3
|
+
export interface UseQrPngOptions {
|
|
4
|
+
size?: number;
|
|
5
|
+
margin?: number;
|
|
6
|
+
ecc?: QrEcc;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export * from "./useQrPngDataUrl";
|
|
10
|
+
export * from "./useQrPngBlobUrl";
|
|
11
|
+
export * from "./useQrDecodeFromImageData";
|
|
12
|
+
export * from "./useQrDecodeFromImageSrc";
|
package/react/index.js
CHANGED
|
@@ -1,91 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
* React hook for QR Code Data URL (browser-only).
|
|
6
|
-
* Backward-compatible, but heavier than Blob URL due to base64.
|
|
7
|
-
* @param {string} text
|
|
8
|
-
* @param {{size?:number, margin?:number, ecc?:"L"|"M"|"Q"|"H"}} [opts]
|
|
9
|
-
*/
|
|
10
|
-
export function useQrPngDataUrl(text, opts) {
|
|
11
|
-
const size = opts?.size ?? 320;
|
|
12
|
-
const margin = opts?.margin ?? 4;
|
|
13
|
-
const ecc = opts?.ecc ?? "Q";
|
|
14
|
-
|
|
15
|
-
const [src, setSrc] = useState("");
|
|
16
|
-
|
|
17
|
-
useEffect(() => {
|
|
18
|
-
let alive = true;
|
|
19
|
-
|
|
20
|
-
if (!text) {
|
|
21
|
-
setSrc("");
|
|
22
|
-
return () => {
|
|
23
|
-
alive = false;
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
qr_png_data_url(text, size, margin, ecc)
|
|
28
|
-
.then((res) => {
|
|
29
|
-
if (alive) setSrc(res);
|
|
30
|
-
})
|
|
31
|
-
.catch(() => {
|
|
32
|
-
if (alive) setSrc("");
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
return () => {
|
|
36
|
-
alive = false;
|
|
37
|
-
};
|
|
38
|
-
}, [text, size, margin, ecc]);
|
|
39
|
-
|
|
40
|
-
return src;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* React hook for QR Code as Blob URL (browser-only).
|
|
45
|
-
* Faster + lower memory than base64 data URL for frequent updates / large images.
|
|
46
|
-
* @param {string} text
|
|
47
|
-
* @param {{size?:number, margin?:number, ecc?:"L"|"M"|"Q"|"H"}} [opts]
|
|
48
|
-
*/
|
|
49
|
-
export function useQrPngBlobUrl(text, opts) {
|
|
50
|
-
const size = opts?.size ?? 320;
|
|
51
|
-
const margin = opts?.margin ?? 4;
|
|
52
|
-
const ecc = opts?.ecc ?? "Q";
|
|
53
|
-
|
|
54
|
-
const [src, setSrc] = useState("");
|
|
55
|
-
|
|
56
|
-
useEffect(() => {
|
|
57
|
-
let alive = true;
|
|
58
|
-
let objectUrl = "";
|
|
59
|
-
|
|
60
|
-
if (!text) {
|
|
61
|
-
setSrc("");
|
|
62
|
-
return () => {
|
|
63
|
-
alive = false;
|
|
64
|
-
if (objectUrl) URL.revokeObjectURL(objectUrl);
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
qr_png_bytes(text, size, margin, ecc)
|
|
69
|
-
.then((bytes) => {
|
|
70
|
-
if (!alive) return;
|
|
71
|
-
|
|
72
|
-
// revoke old url (if any)
|
|
73
|
-
if (objectUrl) URL.revokeObjectURL(objectUrl);
|
|
74
|
-
|
|
75
|
-
objectUrl = URL.createObjectURL(
|
|
76
|
-
new Blob([bytes], { type: "image/png" }),
|
|
77
|
-
);
|
|
78
|
-
setSrc(objectUrl);
|
|
79
|
-
})
|
|
80
|
-
.catch(() => {
|
|
81
|
-
if (alive) setSrc("");
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
return () => {
|
|
85
|
-
alive = false;
|
|
86
|
-
if (objectUrl) URL.revokeObjectURL(objectUrl);
|
|
87
|
-
};
|
|
88
|
-
}, [text, size, margin, ecc]);
|
|
89
|
-
|
|
90
|
-
return src;
|
|
91
|
-
}
|
|
1
|
+
export { useQrPngDataUrl } from "./useQrPngDataUrl.js";
|
|
2
|
+
export { useQrPngBlobUrl } from "./useQrPngBlobUrl.js";
|
|
3
|
+
export { useQrDecodeFromImageData } from "./useQrDecodeFromImageData.js";
|
|
4
|
+
export { useQrDecodeFromImageSrc } from "./useQrDecodeFromImageSrc.js";
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
import { qr_decode_from_image_data } from "../index.web.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* React hook for decoding QR code from ImageData.
|
|
6
|
+
*
|
|
7
|
+
* @param {ImageData | null} image
|
|
8
|
+
* @returns {{ text: string | null, error: unknown | null, loading: boolean }}
|
|
9
|
+
*/
|
|
10
|
+
export function useQrDecodeFromImageData(image) {
|
|
11
|
+
const [text, setText] = useState(null);
|
|
12
|
+
const [error, setError] = useState(null);
|
|
13
|
+
const [loading, setLoading] = useState(false);
|
|
14
|
+
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
let alive = true;
|
|
17
|
+
|
|
18
|
+
if (!image) {
|
|
19
|
+
setText(null);
|
|
20
|
+
setError(null);
|
|
21
|
+
setLoading(false);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
setLoading(true);
|
|
26
|
+
setError(null);
|
|
27
|
+
|
|
28
|
+
qr_decode_from_image_data(image)
|
|
29
|
+
.then((res) => {
|
|
30
|
+
if (!alive) return;
|
|
31
|
+
setText(res);
|
|
32
|
+
setLoading(false);
|
|
33
|
+
})
|
|
34
|
+
.catch((err) => {
|
|
35
|
+
if (!alive) return;
|
|
36
|
+
setText(null);
|
|
37
|
+
setError(err);
|
|
38
|
+
setLoading(false);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
return () => {
|
|
42
|
+
alive = false;
|
|
43
|
+
};
|
|
44
|
+
}, [image]);
|
|
45
|
+
|
|
46
|
+
return { text, error, loading };
|
|
47
|
+
}
|