-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathqrcode-scanner.ts
60 lines (51 loc) · 1.42 KB
/
qrcode-scanner.ts
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
49
50
51
52
53
54
55
56
57
58
59
60
import { css, html, LitElement, TemplateResult } from 'lit';
import { customElement } from 'lit/decorators/custom-element.js';
import { query } from 'lit-element';
import { Html5QrcodeError, Html5QrcodeResult } from 'html5-qrcode/esm/core';
import { Html5QrcodeScanner } from 'html5-qrcode';
@customElement('qrcode-scanner')
export class QRCodeScanner extends LitElement {
static styles = [
css`
:host {
display: block;
}
`,
];
@query('#reader')
reader: HTMLElement;
protected render(): TemplateResult {
return html` <div id="reader"></div> `;
}
protected firstUpdated(): void {
const onScanSuccess = (
decodedText: string,
decodedResult: Html5QrcodeResult
) => {
// handle the scanned code as you like
console.log(`Code matched = ${decodedText}`, decodedResult);
};
const onScanFailure = (errorMessage: string, error: Html5QrcodeError) => {
// handle scan failure, usually better to ignore and keep scanning
console.warn(`Code scan error = ${errorMessage}`, error);
};
const config = {
fps: 10,
qrbox: {
width: 350,
height: 250,
},
};
const html5QrcodeScanner = new Html5QrcodeScanner(
this.reader,
config,
false
);
html5QrcodeScanner.render(onScanSuccess, onScanFailure);
}
}
declare global {
interface HTMLElementTagNameMap {
'qrcode-scanner': QRCodeScanner;
}
}