qr 0.2.4 → 0.4.2
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 +328 -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 +320 -0
- package/esm/dom.js.map +1 -0
- package/esm/index.d.ts +232 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +1123 -0
- package/esm/index.js.map +1 -0
- package/esm/package.json +1 -0
- package/index.d.ts +232 -0
- package/index.d.ts.map +1 -0
- package/index.js +1129 -0
- package/package.json +72 -25
- package/.npmignore +0 -1
- package/.travis.yml +0 -9
- package/benchmark.js +0 -15
- package/lib/encoder.js +0 -140
- package/qr.js +0 -1
- package/test/encoder.js +0 -176
- package/test/qr.js +0 -16
package/dom.d.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
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
|
+
* Optional DOM related utilities. Some utilities, useful to decode QR from camera:
|
|
19
|
+
* - draw overlay: helps user to position QR code on camera
|
|
20
|
+
* - draw bitmap: useful for debugging (what decoder sees)
|
|
21
|
+
* - draw result: show scanned QR code
|
|
22
|
+
* The code is fragile: it is easy to make subtle errors, which will break decoding.
|
|
23
|
+
* @module
|
|
24
|
+
*/
|
|
25
|
+
export declare const getSize: (elm: HTMLElement) => {
|
|
26
|
+
width: number;
|
|
27
|
+
height: number;
|
|
28
|
+
};
|
|
29
|
+
export type QRCanvasOpts = {
|
|
30
|
+
resultBlockSize: number;
|
|
31
|
+
overlayMainColor: string;
|
|
32
|
+
overlayFinderColor: string;
|
|
33
|
+
overlaySideColor: string;
|
|
34
|
+
overlayTimeout: number;
|
|
35
|
+
cropToSquare: boolean;
|
|
36
|
+
};
|
|
37
|
+
export type QRCanvasElements = {
|
|
38
|
+
overlay?: HTMLCanvasElement;
|
|
39
|
+
bitmap?: HTMLCanvasElement;
|
|
40
|
+
resultQR?: HTMLCanvasElement;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Handles canvases for QR code decoding
|
|
44
|
+
*/
|
|
45
|
+
export declare class QRCanvas {
|
|
46
|
+
private opts;
|
|
47
|
+
private lastDetect;
|
|
48
|
+
private main;
|
|
49
|
+
private overlay?;
|
|
50
|
+
private bitmap?;
|
|
51
|
+
private resultQR?;
|
|
52
|
+
constructor({ overlay, bitmap, resultQR }?: QRCanvasElements, opts?: Partial<QRCanvasOpts>);
|
|
53
|
+
private setSize;
|
|
54
|
+
private drawBitmap;
|
|
55
|
+
private drawResultQr;
|
|
56
|
+
private drawOverlay;
|
|
57
|
+
drawImage(image: CanvasImageSource, height: number, width: number): string | undefined;
|
|
58
|
+
clear(): void;
|
|
59
|
+
}
|
|
60
|
+
declare class QRCamera {
|
|
61
|
+
private stream;
|
|
62
|
+
private player;
|
|
63
|
+
constructor(stream: MediaStream, player: HTMLVideoElement);
|
|
64
|
+
private setStream;
|
|
65
|
+
/**
|
|
66
|
+
* Returns list of cameras
|
|
67
|
+
* NOTE: available only after first getUserMedia request, so cannot be additional method
|
|
68
|
+
*/
|
|
69
|
+
listDevices(): Promise<{
|
|
70
|
+
deviceId: string;
|
|
71
|
+
label: string;
|
|
72
|
+
}[]>;
|
|
73
|
+
/**
|
|
74
|
+
* Change stream to different camera
|
|
75
|
+
* @param deviceId - devideId from '.listDevices'
|
|
76
|
+
*/
|
|
77
|
+
setDevice(deviceId: string): Promise<void>;
|
|
78
|
+
readFrame(canvas: QRCanvas, fullSize?: boolean): string | undefined;
|
|
79
|
+
stop(): void;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Creates new QRCamera from frontal camera
|
|
83
|
+
* @param player - HTML Video element
|
|
84
|
+
* @example
|
|
85
|
+
* const canvas = new QRCanvas();
|
|
86
|
+
* const camera = frontalCamera();
|
|
87
|
+
* const devices = await camera.listDevices();
|
|
88
|
+
* await camera.setDevice(devices[0].deviceId); // Change camera
|
|
89
|
+
* const res = camera.readFrame(canvas);
|
|
90
|
+
*/
|
|
91
|
+
export declare function frontalCamera(player: HTMLVideoElement): Promise<QRCamera>;
|
|
92
|
+
/**
|
|
93
|
+
* Run callback in a loop with requestAnimationFrame
|
|
94
|
+
* @param cb - callback
|
|
95
|
+
* @example
|
|
96
|
+
* const cancel = frameLoop((ns) => console.log(ns));
|
|
97
|
+
* cancel();
|
|
98
|
+
*/
|
|
99
|
+
export declare function frameLoop(cb: FrameRequestCallback): () => void;
|
|
100
|
+
export declare function svgToPng(svgData: string, width: number, height: number): Promise<string>;
|
|
101
|
+
export {};
|
|
102
|
+
//# sourceMappingURL=dom.d.ts.map
|
package/dom.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dom.d.ts","sourceRoot":"","sources":["src/dom.ts"],"names":[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AAM,CAAmB;gBACrB,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,gBAAgB;IAKzD,OAAO,CAAC,SAAS;IAQjB;;;OAGG;IACG,WAAW,IAAI,OAAO,CAC1B;QACE,QAAQ,EAAE,MAAM,CAAC;QACjB,KAAK,EAAE,MAAM,CAAC;KACf,EAAE,CACJ;IAWD;;;OAGG;IACG,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAOhD,SAAS,CAAC,MAAM,EAAE,QAAQ,EAAE,QAAQ,UAAQ,GAAG,MAAM,GAAG,SAAS;IAMjE,IAAI,IAAI,IAAI;CAGb;AACD;;;;;;;;;GASG;AACH,wBAAsB,aAAa,CAAC,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAAC,QAAQ,CAAC,CAY/E;AAED;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,EAAE,EAAE,oBAAoB,GAAG,MAAM,IAAI,CAY9D;AAED,wBAAgB,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CA2CxF"}
|
package/dom.js
ADDED
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*!
|
|
3
|
+
Copyright (c) 2023 Paul Miller (paulmillr.com)
|
|
4
|
+
The library paulmillr-qr is dual-licensed under the Apache 2.0 OR MIT license.
|
|
5
|
+
You can select a license of your choice.
|
|
6
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
you may not use this file except in compliance with the License.
|
|
8
|
+
You may obtain a copy of the License at
|
|
9
|
+
|
|
10
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
|
|
12
|
+
Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
See the License for the specific language governing permissions and
|
|
16
|
+
limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* Optional DOM related utilities. Some utilities, useful to decode QR from camera:
|
|
20
|
+
* - draw overlay: helps user to position QR code on camera
|
|
21
|
+
* - draw bitmap: useful for debugging (what decoder sees)
|
|
22
|
+
* - draw result: show scanned QR code
|
|
23
|
+
* The code is fragile: it is easy to make subtle errors, which will break decoding.
|
|
24
|
+
* @module
|
|
25
|
+
*/
|
|
26
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
|
+
exports.QRCanvas = exports.getSize = void 0;
|
|
28
|
+
exports.frontalCamera = frontalCamera;
|
|
29
|
+
exports.frameLoop = frameLoop;
|
|
30
|
+
exports.svgToPng = svgToPng;
|
|
31
|
+
const decode_ts_1 = require("./decode.js");
|
|
32
|
+
const getSize = (elm) => {
|
|
33
|
+
const css = getComputedStyle(elm);
|
|
34
|
+
const width = Math.floor(+css.width.split('px')[0]);
|
|
35
|
+
const height = Math.floor(+css.height.split('px')[0]);
|
|
36
|
+
return { width, height };
|
|
37
|
+
};
|
|
38
|
+
exports.getSize = getSize;
|
|
39
|
+
const setCanvasSize = (canvas, height, width) => {
|
|
40
|
+
// NOTE: setting canvas.width even to same size will clear & redraw it (flickering)
|
|
41
|
+
if (canvas.height !== height)
|
|
42
|
+
canvas.height = height;
|
|
43
|
+
if (canvas.width !== width)
|
|
44
|
+
canvas.width = width;
|
|
45
|
+
};
|
|
46
|
+
const getCanvasContext = (canvas) => {
|
|
47
|
+
const context = canvas.getContext('2d');
|
|
48
|
+
if (context === null)
|
|
49
|
+
throw new Error('Cannot get canvas context');
|
|
50
|
+
return { canvas, context };
|
|
51
|
+
};
|
|
52
|
+
const clearCanvas = ({ canvas, context }) => {
|
|
53
|
+
context.clearRect(0, 0, canvas.width, canvas.height);
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Handles canvases for QR code decoding
|
|
57
|
+
*/
|
|
58
|
+
class QRCanvas {
|
|
59
|
+
constructor({ overlay, bitmap, resultQR } = {}, opts = {}) {
|
|
60
|
+
this.lastDetect = 0;
|
|
61
|
+
this.opts = {
|
|
62
|
+
resultBlockSize: 8,
|
|
63
|
+
overlayMainColor: 'green',
|
|
64
|
+
overlayFinderColor: 'blue',
|
|
65
|
+
overlaySideColor: 'black',
|
|
66
|
+
overlayTimeout: 500,
|
|
67
|
+
cropToSquare: true,
|
|
68
|
+
...opts,
|
|
69
|
+
};
|
|
70
|
+
// TODO: check https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas
|
|
71
|
+
this.main = getCanvasContext(document.createElement('canvas'));
|
|
72
|
+
if (overlay)
|
|
73
|
+
this.overlay = getCanvasContext(overlay);
|
|
74
|
+
if (bitmap)
|
|
75
|
+
this.bitmap = getCanvasContext(bitmap);
|
|
76
|
+
if (resultQR) {
|
|
77
|
+
this.resultQR = getCanvasContext(resultQR);
|
|
78
|
+
this.resultQR.context.imageSmoothingEnabled = false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
setSize(height, width) {
|
|
82
|
+
setCanvasSize(this.main.canvas, height, width);
|
|
83
|
+
if (this.overlay)
|
|
84
|
+
setCanvasSize(this.overlay.canvas, height, width);
|
|
85
|
+
if (this.bitmap)
|
|
86
|
+
setCanvasSize(this.bitmap.canvas, height, width);
|
|
87
|
+
}
|
|
88
|
+
drawBitmap({ data, height, width }) {
|
|
89
|
+
if (!this.bitmap)
|
|
90
|
+
return;
|
|
91
|
+
const imgData = new ImageData(Uint8ClampedArray.from(data), width, height);
|
|
92
|
+
let offset = { x: 0, y: 0 };
|
|
93
|
+
if (this.opts.cropToSquare) {
|
|
94
|
+
offset = {
|
|
95
|
+
x: Math.ceil((this.bitmap.canvas.width - width) / 2),
|
|
96
|
+
y: Math.ceil((this.bitmap.canvas.height - height) / 2),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
this.bitmap.context.putImageData(imgData, offset.x, offset.y);
|
|
100
|
+
}
|
|
101
|
+
drawResultQr({ data, height, width }) {
|
|
102
|
+
if (!this.resultQR)
|
|
103
|
+
return;
|
|
104
|
+
const blockSize = this.opts.resultBlockSize;
|
|
105
|
+
setCanvasSize(this.resultQR.canvas, height, width);
|
|
106
|
+
const imgData = new ImageData(Uint8ClampedArray.from(data), width, height);
|
|
107
|
+
this.resultQR.context.putImageData(imgData, 0, 0);
|
|
108
|
+
this.resultQR.canvas.style = `image-rendering: pixelated; width: ${blockSize * width}px; height: ${blockSize * height}px`;
|
|
109
|
+
}
|
|
110
|
+
drawOverlay(points) {
|
|
111
|
+
if (!this.overlay)
|
|
112
|
+
return;
|
|
113
|
+
const ctx = this.overlay.context;
|
|
114
|
+
const height = this.overlay.canvas.height;
|
|
115
|
+
const width = this.overlay.canvas.width;
|
|
116
|
+
// Sides
|
|
117
|
+
if (this.opts.cropToSquare && height !== width) {
|
|
118
|
+
const squareSize = Math.min(height, width);
|
|
119
|
+
const offset = {
|
|
120
|
+
x: Math.floor((width - squareSize) / 2),
|
|
121
|
+
y: Math.floor((height - squareSize) / 2),
|
|
122
|
+
};
|
|
123
|
+
// Clear only central part (flickering)
|
|
124
|
+
ctx.clearRect(offset.x, offset.y, squareSize, squareSize);
|
|
125
|
+
ctx.fillStyle = this.opts.overlaySideColor;
|
|
126
|
+
if (width > height) {
|
|
127
|
+
ctx.fillRect(0, 0, offset.x, height); // left
|
|
128
|
+
ctx.fillRect(width - offset.x, 0, offset.x, height); // right
|
|
129
|
+
}
|
|
130
|
+
else if (height > width) {
|
|
131
|
+
ctx.fillRect(0, 0, width, offset.y); // top
|
|
132
|
+
ctx.fillRect(0, height - offset.y, width, offset.y); // bottom
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
ctx.clearRect(0, 0, width, height);
|
|
137
|
+
}
|
|
138
|
+
if (points) {
|
|
139
|
+
const [tl, tr, br, bl] = points;
|
|
140
|
+
// Main area
|
|
141
|
+
ctx.fillStyle = this.opts.overlayMainColor;
|
|
142
|
+
ctx.beginPath();
|
|
143
|
+
ctx.moveTo(tl.x, tl.y);
|
|
144
|
+
ctx.lineTo(tr.x, tr.y);
|
|
145
|
+
ctx.lineTo(br.x, br.y);
|
|
146
|
+
ctx.lineTo(bl.x, bl.y);
|
|
147
|
+
ctx.fill();
|
|
148
|
+
ctx.closePath();
|
|
149
|
+
// Finders
|
|
150
|
+
ctx.fillStyle = this.opts.overlayFinderColor;
|
|
151
|
+
for (const p of points) {
|
|
152
|
+
if (!('moduleSize' in p))
|
|
153
|
+
continue;
|
|
154
|
+
const x = p.x - 3 * p.moduleSize;
|
|
155
|
+
const y = p.y - 3 * p.moduleSize;
|
|
156
|
+
const size = 7 * p.moduleSize;
|
|
157
|
+
ctx.fillRect(x, y, size, size);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
drawImage(image, height, width) {
|
|
162
|
+
this.setSize(height, width);
|
|
163
|
+
const { context } = this.main;
|
|
164
|
+
context.drawImage(image, 0, 0, width, height);
|
|
165
|
+
const data = context.getImageData(0, 0, width, height);
|
|
166
|
+
const options = { cropToSquare: this.opts.cropToSquare };
|
|
167
|
+
if (this.bitmap)
|
|
168
|
+
options.imageOnBitmap = (img) => this.drawBitmap(img);
|
|
169
|
+
if (this.overlay)
|
|
170
|
+
options.pointsOnDetect = (points) => this.drawOverlay(points);
|
|
171
|
+
if (this.resultQR)
|
|
172
|
+
options.imageOnResult = (img) => this.drawResultQr(img);
|
|
173
|
+
try {
|
|
174
|
+
const res = (0, decode_ts_1.default)(data, options);
|
|
175
|
+
this.lastDetect = Date.now();
|
|
176
|
+
return res;
|
|
177
|
+
}
|
|
178
|
+
catch (e) {
|
|
179
|
+
if (this.overlay && Date.now() - this.lastDetect > this.opts.overlayTimeout)
|
|
180
|
+
this.drawOverlay();
|
|
181
|
+
}
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
clear() {
|
|
185
|
+
clearCanvas(this.main);
|
|
186
|
+
if (this.overlay)
|
|
187
|
+
clearCanvas(this.overlay);
|
|
188
|
+
if (this.bitmap)
|
|
189
|
+
clearCanvas(this.bitmap);
|
|
190
|
+
if (this.resultQR)
|
|
191
|
+
clearCanvas(this.resultQR);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
exports.QRCanvas = QRCanvas;
|
|
195
|
+
class QRCamera {
|
|
196
|
+
constructor(stream, player) {
|
|
197
|
+
this.stream = stream;
|
|
198
|
+
this.player = player;
|
|
199
|
+
this.setStream(stream);
|
|
200
|
+
}
|
|
201
|
+
setStream(stream) {
|
|
202
|
+
this.stream = stream;
|
|
203
|
+
const { player } = this;
|
|
204
|
+
player.setAttribute('autoplay', '');
|
|
205
|
+
player.setAttribute('muted', '');
|
|
206
|
+
player.setAttribute('playsinline', '');
|
|
207
|
+
player.srcObject = stream;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Returns list of cameras
|
|
211
|
+
* NOTE: available only after first getUserMedia request, so cannot be additional method
|
|
212
|
+
*/
|
|
213
|
+
async listDevices() {
|
|
214
|
+
if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices)
|
|
215
|
+
throw new Error('Media Devices not supported');
|
|
216
|
+
const devices = await navigator.mediaDevices.enumerateDevices();
|
|
217
|
+
return devices
|
|
218
|
+
.filter((device) => device.kind === 'videoinput')
|
|
219
|
+
.map((i) => ({
|
|
220
|
+
deviceId: i.deviceId,
|
|
221
|
+
label: i.label || `Camera ${i.deviceId}`,
|
|
222
|
+
}));
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Change stream to different camera
|
|
226
|
+
* @param deviceId - devideId from '.listDevices'
|
|
227
|
+
*/
|
|
228
|
+
async setDevice(deviceId) {
|
|
229
|
+
this.stop();
|
|
230
|
+
const stream = await navigator.mediaDevices.getUserMedia({
|
|
231
|
+
video: { deviceId: { exact: deviceId } },
|
|
232
|
+
});
|
|
233
|
+
this.setStream(stream);
|
|
234
|
+
}
|
|
235
|
+
readFrame(canvas, fullSize = false) {
|
|
236
|
+
const { player } = this;
|
|
237
|
+
if (fullSize)
|
|
238
|
+
return canvas.drawImage(player, player.videoHeight, player.videoWidth);
|
|
239
|
+
const size = (0, exports.getSize)(player);
|
|
240
|
+
return canvas.drawImage(player, size.height, size.width);
|
|
241
|
+
}
|
|
242
|
+
stop() {
|
|
243
|
+
for (const track of this.stream.getTracks())
|
|
244
|
+
track.stop();
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Creates new QRCamera from frontal camera
|
|
249
|
+
* @param player - HTML Video element
|
|
250
|
+
* @example
|
|
251
|
+
* const canvas = new QRCanvas();
|
|
252
|
+
* const camera = frontalCamera();
|
|
253
|
+
* const devices = await camera.listDevices();
|
|
254
|
+
* await camera.setDevice(devices[0].deviceId); // Change camera
|
|
255
|
+
* const res = camera.readFrame(canvas);
|
|
256
|
+
*/
|
|
257
|
+
async function frontalCamera(player) {
|
|
258
|
+
const stream = await navigator.mediaDevices.getUserMedia({
|
|
259
|
+
video: {
|
|
260
|
+
// Ask for screen resolution
|
|
261
|
+
height: { ideal: window.screen.height },
|
|
262
|
+
width: { ideal: window.screen.width },
|
|
263
|
+
// prefer front-facing camera, but can use any other
|
|
264
|
+
// NOTE: 'exact' will cause OverConstrained error if no frontal camera available
|
|
265
|
+
facingMode: 'environment',
|
|
266
|
+
},
|
|
267
|
+
});
|
|
268
|
+
return new QRCamera(stream, player);
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Run callback in a loop with requestAnimationFrame
|
|
272
|
+
* @param cb - callback
|
|
273
|
+
* @example
|
|
274
|
+
* const cancel = frameLoop((ns) => console.log(ns));
|
|
275
|
+
* cancel();
|
|
276
|
+
*/
|
|
277
|
+
function frameLoop(cb) {
|
|
278
|
+
let handle = undefined;
|
|
279
|
+
function loop(ts) {
|
|
280
|
+
cb(ts);
|
|
281
|
+
handle = requestAnimationFrame(loop);
|
|
282
|
+
}
|
|
283
|
+
handle = requestAnimationFrame(loop);
|
|
284
|
+
return () => {
|
|
285
|
+
if (handle === undefined)
|
|
286
|
+
return;
|
|
287
|
+
cancelAnimationFrame(handle);
|
|
288
|
+
handle = undefined;
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
function svgToPng(svgData, width, height) {
|
|
292
|
+
return new Promise((resolve, reject) => {
|
|
293
|
+
if (!(Number.isSafeInteger(width) &&
|
|
294
|
+
Number.isSafeInteger(height) &&
|
|
295
|
+
width > 0 &&
|
|
296
|
+
height > 0 &&
|
|
297
|
+
width < 8192 &&
|
|
298
|
+
height < 8192))
|
|
299
|
+
return reject(new Error('invalid width and height: ' + width + ' ' + height));
|
|
300
|
+
const domparser = new DOMParser();
|
|
301
|
+
const doc = domparser.parseFromString(svgData, 'image/svg+xml');
|
|
302
|
+
const svgElement = doc.documentElement;
|
|
303
|
+
svgElement.setAttribute('width', String(width));
|
|
304
|
+
svgElement.setAttribute('height', String(height));
|
|
305
|
+
const rect = doc.createElementNS('http://www.w3.org/2000/svg', 'rect');
|
|
306
|
+
rect.setAttribute('width', '100%');
|
|
307
|
+
rect.setAttribute('height', '100%');
|
|
308
|
+
rect.setAttribute('fill', 'white');
|
|
309
|
+
svgElement.insertBefore(rect, svgElement.firstChild);
|
|
310
|
+
const serializer = new XMLSerializer();
|
|
311
|
+
const source = serializer.serializeToString(doc);
|
|
312
|
+
const img = new Image();
|
|
313
|
+
img.src = 'data:image/svg+xml,' + encodeURIComponent(source);
|
|
314
|
+
img.onload = function () {
|
|
315
|
+
const canvas = document.createElement('canvas');
|
|
316
|
+
canvas.width = width;
|
|
317
|
+
canvas.height = height;
|
|
318
|
+
const ctx = canvas.getContext('2d');
|
|
319
|
+
if (!ctx)
|
|
320
|
+
return reject(new Error('was not able to create 2d context'));
|
|
321
|
+
ctx.drawImage(img, 0, 0, width, height);
|
|
322
|
+
const dataUrl = canvas.toDataURL('image/png');
|
|
323
|
+
resolve(dataUrl);
|
|
324
|
+
};
|
|
325
|
+
img.onerror = reject;
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
//# sourceMappingURL=dom.js.map
|
package/esm/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.ts';
|
|
26
|
+
import { Bitmap } from './index.ts';
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../src/decode.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;EAeE;AACF;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAiC,KAAK,EAAQ,KAAK,EAAE,MAAM,YAAY,CAAC;AACpF,OAAO,EAAE,MAAM,EAAS,MAAM,YAAY,CAAC;AAgB3C,MAAM,MAAM,YAAY,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;AA2B9D;;;GAGG;AACH,iBAAS,QAAQ,CAAC,GAAG,EAAE,KAAK,GAAG,MAAM,CAwEpC;AAGD,KAAK,OAAO,GAAG,KAAK,GAAG;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AA6I7D,iBAAS,UAAU,CAAC,CAAC,EAAE,MA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