Newer
Older
1
2
3
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
export class ScanResult {
constructor() {
this.data = null;
this.cornerPoints = null;
}
}
export class QrCodeScannerEngine {
constructor() {
this._engine = null;
this._canvas = document.createElement('canvas');
this._scanner = null;
}
/**
* Scan am image like thing for a QR code. Returns null if none is found.
* The region to scan in can be restricted via "options".
*
* @param {*} image
* @param {?object} options
* @param {number} options.x
* @param {number} options.y
* @param {number} options.width
* @param {number} options.height
* @returns {?ScanResult}
*/
async scanImage(image, options = null) {
if (this._scanner === null) {
this._scanner = (await import('qr-scanner')).default;
}
if (this._engine === null) {
this._engine = await this._scanner.createQrEngine();
}
try {
let tmp = await this._scanner.scanImage(image, {
scanRegion: options ?? null,
qrEngine: this._engine,
canvas: this._canvas,
});
let res = new ScanResult();
res.data = tmp.data;
res.cornerPoints = tmp.cornerPoints;
return res;
} catch (e) {
return null;
}
}
}