qr 0.2.3 → 0.4.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 +203 -21
- package/LICENSE-MIT +21 -0
- package/README.md +249 -65
- package/decode.d.ts +62 -0
- package/decode.d.ts.map +1 -0
- package/decode.js +930 -0
- package/dom.d.ts +102 -0
- package/dom.d.ts.map +1 -0
- package/dom.js +326 -0
- package/esm/decode.d.ts +62 -0
- package/esm/decode.d.ts.map +1 -0
- package/esm/decode.js +926 -0
- package/esm/decode.js.map +1 -0
- package/esm/dom.d.ts +102 -0
- package/esm/dom.d.ts.map +1 -0
- package/esm/dom.js +318 -0
- package/esm/dom.js.map +1 -0
- package/esm/index.d.ts +211 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +1093 -0
- package/esm/index.js.map +1 -0
- package/esm/package.json +1 -0
- package/index.d.ts +211 -0
- package/index.d.ts.map +1 -0
- package/index.js +1099 -0
- package/package.json +74 -25
- package/.npmignore +0 -1
- package/.travis.yml +0 -9
- package/benchmark.js +0 -15
- package/lib/encoder.js +0 -142
- package/qr.js +0 -1
- package/test/encoder.js +0 -176
- package/test/qr.js +0 -16
package/LICENSE
CHANGED
|
@@ -1,21 +1,203 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
203
|
+
|
package/LICENSE-MIT
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Paul Miller (https://paulmillr.com)
|
|
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,93 +1,277 @@
|
|
|
1
|
-
#
|
|
1
|
+
# paulmillr-qr
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Minimal 0-dep QR code generator & reader.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
- 🔒 Auditable, 0-dependency
|
|
6
|
+
- 🏞️ Encoding (generating) supports ASCII, term, gif and svg codes
|
|
7
|
+
- 📷 Decoding (reading) supports camera feed input, files and non-browser environments
|
|
8
|
+
- 🔍 Extensive tests ensure correctness: 100MB+ of vectors
|
|
9
|
+
- 🪶 35KB for encoding + decoding, 18KB for encoding (1000 lines of code)
|
|
6
10
|
|
|
7
|
-
|
|
11
|
+
Check out:
|
|
8
12
|
|
|
9
|
-
|
|
13
|
+
- [paulmillr.com/apps/qr/](https://paulmillr.com/apps/qr/) - interactive demo
|
|
14
|
+
- [qrBTF.com](https://qrbtf.com/en) - uses the library to generate custom, styled codes
|
|
15
|
+
- [cuer](https://github.com/wevm/cuer) - React component based on the library
|
|
16
|
+
- [metamask-sdk](https://github.com/MetaMask/metamask-sdk/blob/3d0ba19610853ec9259bb1aad459b1eaea799375/packages/sdk/package.json#L56) - is using the library
|
|
10
17
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
For Ubuntu Linux
|
|
18
|
+
Why other libraries are less optimal:
|
|
14
19
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
Others (see instructions on site)
|
|
20
|
+
- [jsQR](https://github.com/cozmo/jsQR) is dead, [zxing-js](https://github.com/zxing-js/) is [dead](https://github.com/zxing-js/library/commit/b797504c25454db32aa2db410e6502b6db12a401), [qr-scanner](https://github.com/nimiq/qr-scanner/) uses jsQR and doesn't work outside of browser, [qcode-decoder](https://github.com/cirocosta/qcode-decoder) is broken version of jsQR and doesn't work outside of browser, [qrcode](https://github.com/nuintun/qrcode) is fork of jsQR without adoption
|
|
21
|
+
- [instascan](https://github.com/schmich/instascan) is too big: over 1MB+ (it's zxing compiled to js via emscripten)
|
|
18
22
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
Node-qr is available in the Node Package Manager (NPM)
|
|
23
|
+
## Usage
|
|
23
24
|
|
|
24
|
-
|
|
25
|
+
A standalone file [qr.js](https://github.com/paulmillr/qr/releases) is also available.
|
|
25
26
|
|
|
26
|
-
|
|
27
|
+
> `npm install qr`
|
|
27
28
|
|
|
28
|
-
|
|
29
|
+
> `jsr add jsr:@paulmillr/qr`
|
|
29
30
|
|
|
30
|
-
|
|
31
|
+
- [Encoding](#encoding)
|
|
32
|
+
- [Decoding](#decoding)
|
|
33
|
+
- [Decoding options](#decoding-options)
|
|
34
|
+
- [Decoding algorithm](#decoding-algorithm)
|
|
35
|
+
- [Decoding test vectors](#decoding-test-vectors)
|
|
36
|
+
- [DOM helpers for web apps](#dom-helpers-for-web-apps)
|
|
37
|
+
- [Using with Kotlin](#using-with-kotlin)
|
|
38
|
+
- [Security](#security)
|
|
39
|
+
- [Speed](#speed)
|
|
31
40
|
|
|
32
|
-
|
|
41
|
+
## Encoding
|
|
33
42
|
|
|
34
|
-
|
|
35
|
-
|
|
43
|
+
```ts
|
|
44
|
+
import encodeQR from '@paulmillr/qr';
|
|
36
45
|
|
|
37
|
-
|
|
46
|
+
// import decodeQR from '@paulmillr/qr/decode';
|
|
47
|
+
// See separate README section for decoding.
|
|
38
48
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
encoder.encode('some value');
|
|
46
|
-
|
|
47
|
-
Alternatively, you can pass a file path for the PNG data to be saved to
|
|
49
|
+
const txt = 'Hello world';
|
|
50
|
+
const ascii = encodeQR(txt, 'ascii'); // Not all fonts are supported
|
|
51
|
+
const terminalFriendly = encodeQR(txt, 'term'); // 2x larger, all fonts are OK
|
|
52
|
+
const gifBytes = encodeQR(txt, 'gif'); // Uncompressed GIF
|
|
53
|
+
const svgElement = encodeQR(txt, 'svg'); // SVG vector image element
|
|
54
|
+
const array = encodeQR(txt, 'raw'); // 2d array for canvas or other libs
|
|
48
55
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
+
// Options
|
|
57
|
+
// Custom error correction level
|
|
58
|
+
// low: 7%, medium: 15% (default), quartile: 25%, high: 30%
|
|
59
|
+
const highErrorCorrection = encodeQR(txt, 'gif', { ecc: 'high' });
|
|
60
|
+
// Custom encoding: 'numeric', 'alphanumeric' or 'byte'
|
|
61
|
+
const customEncoding = encodeQR(txt, 'gif', { encoding: 'byte' });
|
|
62
|
+
// Default scale is 2: each block is 2x2 pixels.
|
|
63
|
+
const larger = encodeQR(txt, 'gif', { scale: 4 });
|
|
64
|
+
// All options
|
|
65
|
+
// type QrOpts = {
|
|
66
|
+
// ecc?: 'low' | 'medium' | 'quartile' | 'high';
|
|
67
|
+
// encoding?: 'numeric' | 'alphanumeric' | 'byte' | 'kanji' | 'eci';
|
|
68
|
+
// version?: number; // 1..40, QR code version
|
|
69
|
+
// mask?: number; // 0..7, mask number
|
|
70
|
+
// border?: number; // Border size, default 2.
|
|
71
|
+
// scale?: number; // Scale to this number. Scale=2 -> each block will be 2x2 pixels
|
|
72
|
+
// };
|
|
56
73
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
74
|
+
console.log(ascii);
|
|
75
|
+
> █████████████████████████████████████
|
|
76
|
+
> ██ ▄▄▄▄▄ █ ▀▄▄█ ██▀▄▄▄▄█ ▀█ ▄▄▄▄▄ ██
|
|
77
|
+
> ██ █ █ █▀▄▀▄ ▄▄█▄█ ██▀█▀▀█ █ █ ██
|
|
78
|
+
> ██ █▄▄▄█ ██ ▄▄█▄▀▀ ▀ ██ ▄ ▄█ █▄▄▄█ ██
|
|
79
|
+
> ██▄▄▄▄▄▄▄█ ▀ ▀ █▄▀ ▀ ▀▄█ █ █▄▄▄▄▄▄▄██
|
|
80
|
+
> ██ █ ▀ ▄▄▀▀▀ █▀ ▄ ▀▀▄▀ ▄█ ▀█ ▀▄▄██
|
|
81
|
+
> ██▀▀▀ ▀▄▄██▄▀▀▄█▀ ▀▄█ ▀▀▀ ▄ █▄▄██
|
|
82
|
+
> █████▄▀▀▄▄██ ▀ ▀ ▄▄██▄ ▄▄ ▄ █▀█ █ ███
|
|
83
|
+
> ███ ▄▀▄█▄▄▄█ ▀██▄▄▄▀▀█▄▀ ▄█▀ ████
|
|
84
|
+
> ██▀▀ ▄ ▀▄ ▄▄██▀▄▀▀████▄▄▄ █▄ █ █▀▀██
|
|
85
|
+
> ██▀▀▄ ▄▀▄ ▀▀█▄▀▀▄▄▀▀ █▄▄▀█▀ ▀▄ █▄ ▀██
|
|
86
|
+
> ██▀▄▀██ ▄▄ ▀█▄█▀ ▀ ▀█▄▀▀ █▄▀▀ █ █ ██
|
|
87
|
+
> ███▀█▄▀▄▄ █ █ ██ ██ ▄ █ ▄▄▄ ▄▀▀▄▄ ██
|
|
88
|
+
> ██▄█▄▄▄█▄█ ▄ ▄▀█▀▀ ▄▀ █▀ ▄ ▄▄▄ ▀▄▀▄██
|
|
89
|
+
> ██ ▄▄▄▄▄ █ ▄█▄▀▀ ▀█ █▄█ █▄█ ▀▀▄▀██
|
|
90
|
+
> ██ █ █ █▀ ▄▀█ ██ ▄▄▀██ ▄▄ ▄█ ██
|
|
91
|
+
> ██ █▄▄▄█ █▄ ██▀ ▄▄ ▀█ ▄ ▀▄▄█▀██
|
|
92
|
+
> ██▄▄▄▄▄▄▄█▄███▄█▄█▄▄▄▄█▄█▄████▄▄█████
|
|
93
|
+
> ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
|
|
94
|
+
```
|
|
63
95
|
|
|
64
|
-
|
|
96
|
+
## Decoding
|
|
65
97
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
## Encoder Options ##
|
|
98
|
+
GIF reader is not included in the package (it would take a lot of space).
|
|
99
|
+
Decoding raw bitmap is still possible.
|
|
69
100
|
|
|
70
|
-
|
|
101
|
+
```js
|
|
102
|
+
import encodeQR from '@paulmillr/qr';
|
|
103
|
+
import decodeQR from '@paulmillr/qr/decode';
|
|
104
|
+
import { Bitmap } from '@paulmillr/qr';
|
|
71
105
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
* dot_size: specify the size of dot (pixel). (default=3)
|
|
75
|
-
* margin: specify the width of margin. (default=4)
|
|
76
|
-
* level: specify error correction level from L (lowest) to H (highest). (default=L)
|
|
77
|
-
* case_sensitive: ignore case distinctions and use only upper-case characters. (default=true)
|
|
78
|
-
* version: specify the version of the symbol. (default=1)
|
|
79
|
-
* type: type of image to export ('PNG','EPS','SVG','ANSI','ANSI256','ASCII','ASCIIi','UTF8','ANSIUTF8')
|
|
106
|
+
// Scale so it would be 100x100 instead of 25x25
|
|
107
|
+
const opts = { scale: 4 };
|
|
80
108
|
|
|
81
|
-
|
|
109
|
+
// a) Decode using raw bitmap, dependency-free
|
|
110
|
+
function decodeRawBitmap() {
|
|
111
|
+
const bmBits = encodeQR('Hello world', 'raw', opts);
|
|
112
|
+
const bm = new Bitmap({ width: bmBits[0].length, height: bmBits.length });
|
|
113
|
+
bm.data = bmBits;
|
|
114
|
+
const decoded = decodeQR(bm.toImage());
|
|
115
|
+
console.log('decoded(pixels)', decoded);
|
|
116
|
+
}
|
|
117
|
+
/*
|
|
118
|
+
Output:
|
|
119
|
+
decoded(pixels) Hello world
|
|
120
|
+
decoded(gif) Hello world
|
|
121
|
+
*/
|
|
82
122
|
|
|
83
|
-
|
|
123
|
+
// b) Decode using external GIF decoder
|
|
124
|
+
import gif from 'omggif'; // npm install omggif@1.0.10
|
|
125
|
+
function parseGIF(image) {
|
|
126
|
+
const r = new gif.GifReader(image);
|
|
127
|
+
const data = [];
|
|
128
|
+
r.decodeAndBlitFrameRGBA(0, data);
|
|
129
|
+
const { width, height } = r.frameInfo(0);
|
|
130
|
+
return { width, height, data };
|
|
131
|
+
}
|
|
132
|
+
function decodeWithExternal() {
|
|
133
|
+
const gifBytes = encodeQR('Hello world', 'gif', opts);
|
|
134
|
+
const decoded = decodeQR(parseGIF(gifBytes));
|
|
135
|
+
console.log('decoded(gif)', decoded);
|
|
136
|
+
}
|
|
84
137
|
|
|
85
|
-
|
|
138
|
+
// c) draw gif/svg to browser DOM canvas
|
|
139
|
+
import { svgToPng } from '@paulmillr/qr/dom';
|
|
140
|
+
const png = svgToPng(encodeQR('Hello world', 'svg'), 512, 512);
|
|
141
|
+
```
|
|
86
142
|
|
|
87
|
-
|
|
143
|
+
### Decoding options
|
|
88
144
|
|
|
89
|
-
|
|
145
|
+
```ts
|
|
146
|
+
export type Point4 = { x: number; y: number }[];
|
|
147
|
+
export type Image = {
|
|
148
|
+
height: number;
|
|
149
|
+
width: number;
|
|
150
|
+
data: Uint8Array | Uint8ClampedArray | number[];
|
|
151
|
+
};
|
|
152
|
+
export type DecodeOpts = {
|
|
153
|
+
// By default we assume that image has 4 channel per pixel (RGBA). isRGB: true will force to use only one
|
|
154
|
+
isRGB?: boolean;
|
|
155
|
+
// Returns 4 center (3 finder pattern + 1 alignment pattern) points if detected
|
|
156
|
+
detectFn?: (points: Point4) => void;
|
|
157
|
+
// Returns RGBA image of detected QR code
|
|
158
|
+
qrFn?: (img: Image) => void;
|
|
159
|
+
};
|
|
160
|
+
export default function decodeQR(img: Image, opts: DecodeOpts = {});
|
|
161
|
+
```
|
|
90
162
|
|
|
91
|
-
|
|
163
|
+
### Decoding algorithm
|
|
92
164
|
|
|
93
|
-
|
|
165
|
+
QR code decoding is challenging; it is essentially a computer vision problem. There are two main scenarios:
|
|
166
|
+
|
|
167
|
+
- Decoding from files: This can be slow because it needs to handle complicated cases such as blur or rotation.
|
|
168
|
+
- Decoding from a camera feed: This must be fast; even if one frame fails, the next frame can succeed.
|
|
169
|
+
|
|
170
|
+
The state-of-the-art approach for this, as with other computer vision problems, is using neural networks. However, using them would make the library hard to audit. Additionally, since JavaScript can't access hardware accelerators, it would likely be very slow. We also avoid using WebGL because it is complex and exposes users to fingerprinting.
|
|
171
|
+
|
|
172
|
+
The implemented reader algorithm is inspired by [ZXing](https://github.com/zxing/zxing):
|
|
173
|
+
|
|
174
|
+
1. `toBitmap`: Convert the image to a bitmap of black and white segments. This is the slowest part and the most important.
|
|
175
|
+
2. `detect`: Find three finder patterns and one alignment pattern (for versions > 1). This is tricky—they can be rotated and distorted by perspective. A square might appear as a quadrilateral with unknown size. The best we can do is count runs of the same color and select patterns with almost the same ratio of runs.
|
|
176
|
+
3. `transform`: Once patterns have been found, correct the perspective and transform the quadrilateral into a square.
|
|
177
|
+
4. `decodeBitmap`: Execute the encoding in reverse: read information via a zig-zag pattern, de-interleave bytes, correct errors, convert to bits, and finally, read segments from bits to create the string.
|
|
178
|
+
5. **Finished!**
|
|
179
|
+
|
|
180
|
+
### Decoding test vectors
|
|
181
|
+
|
|
182
|
+
To test our QR code decoding, we use an excellent dataset
|
|
183
|
+
from [BoofCV](http://boofcv.org/index.php?title=Performance:QrCode). BoofCV decodes 73% of the test cases,
|
|
184
|
+
while ZXing decodes 49%. Our implementation is nearly at parity with ZXing, primarily because ECI (Extended
|
|
185
|
+
Channel Interpretation) support is not yet included. The test vectors are preserved in a Git repository at
|
|
186
|
+
[github.com/paulmillr/qr-code-vectors](https://github.com/paulmillr/qr-code-vectors).
|
|
187
|
+
|
|
188
|
+
**Note for Testing on iOS Safari:** Accessing the camera on iOS Safari requires HTTPS. This means that the file: protocol or non-encrypted http cannot be used. Ensure your testing environment uses https:.
|
|
189
|
+
|
|
190
|
+
The QR code specification is available for purchase at [iso.org](https://www.iso.org/standard/62021.html) for 200 CHF.
|
|
191
|
+
|
|
192
|
+
### DOM helpers for web apps
|
|
193
|
+
|
|
194
|
+
Check out `dom.ts` for browser-related camera code that would make your apps simpler.
|
|
195
|
+
|
|
196
|
+
## Using with Kotlin
|
|
197
|
+
|
|
198
|
+
```kotlin
|
|
199
|
+
@JsModule("@paulmillr/qr")
|
|
200
|
+
@JsNonModule
|
|
201
|
+
external object Qr {
|
|
202
|
+
@JsName("default")
|
|
203
|
+
fun encodeQR(text: String, output: String = definedExternally, opts: dynamic = definedExternally): Uint8Array
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// then
|
|
207
|
+
val bytes = Qr.encodeQR("text", "gif", js("{ scale: 10 }"))
|
|
208
|
+
val blob = Blob(arrayOf(bytes), BlobPropertyBag("image/gif"))
|
|
209
|
+
val imgSrc = URL.createObjectURL(blob)
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## Security
|
|
213
|
+
|
|
214
|
+
There are multiple ways a single text can be encoded in a QR code, which can lead to potential security implications:
|
|
215
|
+
|
|
216
|
+
- **Segmentation Differences:** For example, `abc123` can be encoded as:
|
|
217
|
+
`[{type: 'alphanum', data: 'abc'}, {type: 'num', data: '123'}]` or `[{type: 'alphanum', data: 'abc123'}]`
|
|
218
|
+
- **Mask Selection Algorithms:** Different libraries may use different algorithms for mask selection.
|
|
219
|
+
- **Default Settings:** Variations in error correction levels and how many bits are stored before upgrading versions.
|
|
220
|
+
|
|
221
|
+
If an adversary can access multiple QR codes generated from a specific library, they may be able to fingerprint the user. This fingerprinting could be used to exfiltrate data from air-gapped systems. In such cases, the adversary would need to create a library-specific exploit.
|
|
222
|
+
|
|
223
|
+
We mitigate these risks by:
|
|
224
|
+
|
|
225
|
+
- **Cross-Testing:** We currently cross-test against python-qrcode, which is closer to the specification
|
|
226
|
+
than some JavaScript implementations.
|
|
227
|
+
- **Single Segment Encoding:** We always use single-segment encoding.
|
|
228
|
+
While this may not be the most optimal for performance, it reduces the amount of fingerprinting data.
|
|
229
|
+
|
|
230
|
+
Future plans:
|
|
231
|
+
|
|
232
|
+
- **Testing Against Multiple Libraries:** To further improve security and reduce fingerprinting, we can
|
|
233
|
+
cross-test against three to four popular libraries.
|
|
234
|
+
|
|
235
|
+
## Speed
|
|
236
|
+
|
|
237
|
+
Benchmarks measured with Apple M2 on MacOS 13 with node.js 19.
|
|
238
|
+
|
|
239
|
+
```
|
|
240
|
+
======== encode/ascii ========
|
|
241
|
+
encode/paulmillr-qr x 1,794 ops/sec @ 557μs/op
|
|
242
|
+
encode/qrcode-generator x 3,128 ops/sec @ 319μs/op ± 1.12% (min: 293μs, max: 3ms)
|
|
243
|
+
encode/nuintun x 1,872 ops/sec @ 533μs/op
|
|
244
|
+
======== encode/gif ========
|
|
245
|
+
encode/paulmillr-qr x 1,771 ops/sec @ 564μs/op
|
|
246
|
+
encode/qrcode-generator x 1,773 ops/sec @ 563μs/op
|
|
247
|
+
encode/nuintun x 1,883 ops/sec @ 530μs/op
|
|
248
|
+
======== encode: big ========
|
|
249
|
+
encode/paulmillr-qr x 87 ops/sec @ 11ms/op
|
|
250
|
+
encode/qrcode-generator x 124 ops/sec @ 8ms/op
|
|
251
|
+
encode/nuintun x 143 ops/sec @ 6ms/op
|
|
252
|
+
======== decode ========
|
|
253
|
+
decode/paulmillr-qr x 96 ops/sec @ 10ms/op ± 1.39% (min: 9ms, max: 32ms)
|
|
254
|
+
decode/jsqr x 34 ops/sec @ 28ms/op
|
|
255
|
+
decode/nuintun x 35 ops/sec @ 28ms/op
|
|
256
|
+
decode/instascan x 79 ops/sec @ 12ms/op ± 6.73% (min: 9ms, max: 223ms)
|
|
257
|
+
======== Decoding quality ========
|
|
258
|
+
blurred(45): paulmillr-qr=12 (26.66%) jsqr=13 (28.88%) nuintun=13 (28.88%) instascan=11 (24.44%)
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
## License
|
|
262
|
+
|
|
263
|
+
Copyright (c) 2023 Paul Miller (paulmillr.com)
|
|
264
|
+
|
|
265
|
+
Copyright (c) 2019 ZXing authors
|
|
266
|
+
|
|
267
|
+
The library @paulmillr/qr is dual-licensed under the Apache 2.0 OR MIT license.
|
|
268
|
+
You can select a license of your choice.
|
|
269
|
+
|
|
270
|
+
The library contains code inspired by [ZXing](https://github.com/zxing/zxing), which is licensed under Apache 2.0.
|
|
271
|
+
|
|
272
|
+
The license to the use of the QR Code stipulated by JIS (Japanese Industrial Standards) and the ISO are not necessary.
|
|
273
|
+
The specification for QR Code has been made available for use by any person or organization. (Obtaining QR Code Specification)
|
|
274
|
+
The word “QR Code” is registered trademark of DENSO WAVE INCORPORATED in Japan and other countries.
|
|
275
|
+
To use the word “QR Code” in your publications or web site, etc, please indicate a sentence QR Code is registered trademark of DENSO WAVE INCORPORATED.
|
|
276
|
+
This registered trademark applies only for the word “QR Code”, and not for the QR Code pattern (image).
|
|
277
|
+
(https://www.qrcode.com/en/faq.html)
|
package/decode.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
Copyright (c) 2023 Paul Miller (paulmillr.com)
|
|
3
|
+
The library paulmillr-qr is dual-licensed under the Apache 2.0 OR MIT license.
|
|
4
|
+
You can select a license of your choice.
|
|
5
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
you may not use this file except in compliance with the License.
|
|
7
|
+
You may obtain a copy of the License at
|
|
8
|
+
|
|
9
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
|
|
11
|
+
Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
See the License for the specific language governing permissions and
|
|
15
|
+
limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* Methods for decoding (reading) QR code patterns.
|
|
19
|
+
* @module
|
|
20
|
+
* @example
|
|
21
|
+
```js
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
*/
|
|
25
|
+
import type { Image, Point } from './index.js';
|
|
26
|
+
import { Bitmap } from './index.js';
|
|
27
|
+
export type FinderPoints = [Pattern, Pattern, Point, Pattern];
|
|
28
|
+
/**
|
|
29
|
+
* Convert to grayscale. The function is the most expensive part of decoding:
|
|
30
|
+
* it takes up to 90% of time. TODO: check gamma correction / sqr.
|
|
31
|
+
*/
|
|
32
|
+
declare function toBitmap(img: Image): Bitmap;
|
|
33
|
+
type Pattern = Point & {
|
|
34
|
+
moduleSize: number;
|
|
35
|
+
count: number;
|
|
36
|
+
};
|
|
37
|
+
declare function findFinder(b: Bitmap): {
|
|
38
|
+
bl: Pattern;
|
|
39
|
+
tl: Pattern;
|
|
40
|
+
tr: Pattern;
|
|
41
|
+
};
|
|
42
|
+
declare function detect(b: Bitmap): {
|
|
43
|
+
bits: Bitmap;
|
|
44
|
+
points: FinderPoints;
|
|
45
|
+
};
|
|
46
|
+
declare function decodeBitmap(b: Bitmap): string;
|
|
47
|
+
export type DecodeOpts = {
|
|
48
|
+
cropToSquare?: boolean;
|
|
49
|
+
pointsOnDetect?: (points: FinderPoints) => void;
|
|
50
|
+
imageOnBitmap?: (img: Image) => void;
|
|
51
|
+
imageOnDetect?: (img: Image) => void;
|
|
52
|
+
imageOnResult?: (img: Image) => void;
|
|
53
|
+
};
|
|
54
|
+
export declare function decodeQR(img: Image, opts?: DecodeOpts): string;
|
|
55
|
+
export default decodeQR;
|
|
56
|
+
export declare const _tests: {
|
|
57
|
+
toBitmap: typeof toBitmap;
|
|
58
|
+
decodeBitmap: typeof decodeBitmap;
|
|
59
|
+
findFinder: typeof findFinder;
|
|
60
|
+
detect: typeof detect;
|
|
61
|
+
};
|
|
62
|
+
//# sourceMappingURL=decode.d.ts.map
|