qr-decoder-js 1.0.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/README.md ADDED
@@ -0,0 +1,113 @@
1
+ # qr-decoder-js
2
+
3
+ A small, browser-focused QR code decoder with TypeScript declarations. It detects
4
+ payload encoding and falls back across common encodings to reduce garbled text,
5
+ especially for Chinese and other non-UTF-8 content.
6
+
7
+ [中文](README_ZH.md)
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ npm install qr-decoder-js
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ Decode an image file selected by the user:
18
+
19
+ ```html
20
+ <input id="file" type="file" accept="image/*" />
21
+ ```
22
+
23
+ ```js
24
+ import { decodeQRFile } from 'qr-decoder-js';
25
+
26
+ const input = document.querySelector('#file');
27
+
28
+ input.addEventListener('change', async () => {
29
+ const file = input.files?.[0];
30
+ if (!file) return;
31
+
32
+ const result = await decodeQRFile(file);
33
+ console.log(result.success ? result.text : result.error);
34
+ });
35
+ ```
36
+
37
+ For an existing canvas, use `decodeQRImageData`:
38
+
39
+ ```js
40
+ import { decodeQRImageData } from 'qr-decoder-js';
41
+
42
+ const imageData = context.getImageData(0, 0, canvas.width, canvas.height);
43
+ const result = await decodeQRImageData(imageData);
44
+ ```
45
+
46
+ `decodeQRFile` accepts `File`, `Blob`, `ImageData`, `HTMLImageElement`,
47
+ `HTMLCanvasElement`, `OffscreenCanvas`, and `{ width, height, data }`.
48
+ `decodeQRImageData` accepts `ImageData` or the same `{ width, height, data }`
49
+ shape. The `data` value must contain RGBA pixels.
50
+
51
+ ## API
52
+
53
+ ### `decodeQRFile(input, options?)`
54
+
55
+ Decode a QR code from a file or browser image object.
56
+
57
+ ### `decodeQRImageData(imageData, options?)`
58
+
59
+ Decode a QR code from RGBA image data.
60
+
61
+ Both methods return `Promise<DecodeResult>`.
62
+
63
+ Common reader options include `tryRotate`, `tryInvert`, `tryHarder`,
64
+ `tryDownscale`, `tryDenoise`, `maxNumberOfSymbols`, `formats`, and
65
+ `characterSet`:
66
+
67
+ ```js
68
+ const result = await decodeQRFile(file, {
69
+ tryHarder: true,
70
+ tryRotate: true,
71
+ maxNumberOfSymbols: 1
72
+ });
73
+ ```
74
+
75
+ ### Result
76
+
77
+ ```ts
78
+ interface DecodeResult {
79
+ success: boolean;
80
+ text?: string;
81
+ encoding?: string;
82
+ detectedEncoding?: string | null;
83
+ version?: string;
84
+ error?: string;
85
+ }
86
+ ```
87
+
88
+ On failure, check `success` and read `error`:
89
+
90
+ ```js
91
+ {
92
+ success: false,
93
+ error: 'No QR code found in the image'
94
+ }
95
+ ```
96
+
97
+ ## Browser support
98
+
99
+ The target browser must support WebAssembly, `fetch`, `TextDecoder`, and
100
+ `ImageData`. The package is intended for browser applications and does not
101
+ provide a Node.js image adapter.
102
+
103
+ ## Development
104
+
105
+ ```bash
106
+ pnpm install
107
+ pnpm dev # local demo
108
+ pnpm build # build the package
109
+ ```
110
+
111
+ ## License
112
+
113
+ MIT
package/README_ZH.md ADDED
@@ -0,0 +1,109 @@
1
+ # qr-decoder-js
2
+
3
+ 一个面向浏览器的轻量二维码解码库,提供 TypeScript 类型声明,并支持文本编码检测与常见编码回退,减少中文及其他非 UTF-8 内容的乱码问题。
4
+
5
+ [English](README.md)
6
+
7
+ ## 安装
8
+
9
+ ```bash
10
+ npm install qr-decoder-js
11
+ ```
12
+
13
+ ## 使用
14
+
15
+ 从用户选择的图片文件中解码二维码:
16
+
17
+ ```html
18
+ <input id="file" type="file" accept="image/*" />
19
+ ```
20
+
21
+ ```js
22
+ import { decodeQRFile } from 'qr-decoder-js';
23
+
24
+ const input = document.querySelector('#file');
25
+
26
+ input.addEventListener('change', async () => {
27
+ const file = input.files?.[0];
28
+ if (!file) return;
29
+
30
+ const result = await decodeQRFile(file);
31
+ console.log(result.success ? result.text : result.error);
32
+ });
33
+ ```
34
+
35
+ 如果已有 Canvas 图像数据,可以使用 `decodeQRImageData`:
36
+
37
+ ```js
38
+ import { decodeQRImageData } from 'qr-decoder-js';
39
+
40
+ const imageData = context.getImageData(0, 0, canvas.width, canvas.height);
41
+ const result = await decodeQRImageData(imageData);
42
+ ```
43
+
44
+ `decodeQRFile` 支持 `File`、`Blob`、`ImageData`、`HTMLImageElement`、
45
+ `HTMLCanvasElement`、`OffscreenCanvas` 和 `{ width, height, data }`。
46
+ `decodeQRImageData` 支持 `ImageData` 或同样的 `{ width, height, data }`
47
+ 结构,其中 `data` 必须是 RGBA 像素数据。
48
+
49
+ ## API
50
+
51
+ ### `decodeQRFile(input, options?)`
52
+
53
+ 从文件或浏览器图像对象中解码二维码。
54
+
55
+ ### `decodeQRImageData(imageData, options?)`
56
+
57
+ 从 RGBA 图像数据中解码二维码。
58
+
59
+ 两个方法都返回 `Promise<DecodeResult>`。
60
+
61
+ 常用选项包括 `tryRotate`、`tryInvert`、`tryHarder`、`tryDownscale`、
62
+ `tryDenoise`、`maxNumberOfSymbols`、`formats` 和 `characterSet`:
63
+
64
+ ```js
65
+ const result = await decodeQRFile(file, {
66
+ tryHarder: true,
67
+ tryRotate: true,
68
+ maxNumberOfSymbols: 1
69
+ });
70
+ ```
71
+
72
+ ### 返回值
73
+
74
+ ```ts
75
+ interface DecodeResult {
76
+ success: boolean;
77
+ text?: string;
78
+ encoding?: string;
79
+ detectedEncoding?: string | null;
80
+ version?: string;
81
+ error?: string;
82
+ }
83
+ ```
84
+
85
+ 解码失败时检查 `success`,并读取 `error`:
86
+
87
+ ```js
88
+ {
89
+ success: false,
90
+ error: 'No QR code found in the image'
91
+ }
92
+ ```
93
+
94
+ ## 浏览器要求
95
+
96
+ 目标浏览器需要支持 WebAssembly、`fetch`、`TextDecoder` 和 `ImageData`。
97
+ 本包面向浏览器应用,不提供 Node.js 图片解码适配层。
98
+
99
+ ## 本地开发
100
+
101
+ ```bash
102
+ pnpm install
103
+ pnpm dev # 启动本地示例
104
+ pnpm build # 构建 npm 包
105
+ ```
106
+
107
+ ## 许可证
108
+
109
+ MIT
@@ -0,0 +1,47 @@
1
+ export interface DecodeResult {
2
+ success: boolean;
3
+ /** The decoded text. */
4
+ text?: string;
5
+ /** The TextDecoder label that was ultimately used. */
6
+ encoding?: string;
7
+ /** Raw detection result from ced-wasm (e.g. "GBK"); may be empty. */
8
+ detectedEncoding?: string | null;
9
+ /** Preprocess version that matched: original / blue / green / red / gray / inverted. */
10
+ version?: string;
11
+ error?: string;
12
+ }
13
+
14
+ export interface ReaderOptions {
15
+ formats?: string[];
16
+ tryRotate?: boolean;
17
+ tryInvert?: boolean;
18
+ tryHarder?: boolean;
19
+ tryDownscale?: boolean;
20
+ tryDenoise?: boolean;
21
+ maxNumberOfSymbols?: number;
22
+ binarizer?: string;
23
+ isPure?: boolean;
24
+ returnErrors?: boolean;
25
+ textMode?: string;
26
+ characterSet?: string;
27
+ [key: string]: unknown;
28
+ }
29
+
30
+ export function decodeQRImageData(
31
+ imageData:
32
+ | ImageData
33
+ | { width: number; height: number; data: Uint8ClampedArray | Uint8Array | number[] },
34
+ options?: ReaderOptions
35
+ ): Promise<DecodeResult>;
36
+
37
+ export function decodeQRFile(
38
+ input:
39
+ | File
40
+ | Blob
41
+ | ImageData
42
+ | HTMLImageElement
43
+ | HTMLCanvasElement
44
+ | OffscreenCanvas
45
+ | { width: number; height: number; data: Uint8ClampedArray | Uint8Array | number[] },
46
+ options?: ReaderOptions
47
+ ): Promise<DecodeResult>;