Skip to content

Commit d9b65e4

Browse files
committed
feat: try change qrcode scaner logic to html5-qrcode
1 parent 6702901 commit d9b65e4

File tree

4 files changed

+182
-134
lines changed

4 files changed

+182
-134
lines changed

mobile/package-lock.json

Lines changed: 9 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mobile/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"@trpc/client": "^10.45.2",
3636
"@trpc/server": "^10.45.2",
3737
"@zxing/browser": "^0.1.5",
38+
"html5-qrcode": "^2.3.8",
3839
"ionicons": "^7.0.0",
3940
"jsqr": "^1.4.0",
4041
"lucide": "^0.555.0",
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import {
2+
AfterViewInit,
3+
Component,
4+
ElementRef,
5+
OnDestroy,
6+
OnInit,
7+
Renderer2,
8+
ViewChild,
9+
Output,
10+
Input,
11+
EventEmitter,
12+
} from '@angular/core';
13+
import { Html5Qrcode } from 'html5-qrcode';
14+
import { Html5QrcodeCameraScanConfig } from 'html5-qrcode/esm/html5-qrcode';
15+
import { Html5QrcodeResult } from 'html5-qrcode/esm/core';
16+
17+
@Component({
18+
selector: 'html5-qrcode',
19+
template: ` <div #reader id="reader" width="600px"></div> `,
20+
styles: [],
21+
})
22+
export class NgxHtml5QrcodeComponent
23+
implements OnInit, AfterViewInit, OnDestroy
24+
{
25+
@ViewChild('reader') reader: ElementRef | undefined;
26+
html5QrCode!: Html5Qrcode;
27+
cameraId: string = '';
28+
29+
@Input() useFrontCamera: boolean = false;
30+
@Input() config: Html5QrcodeCameraScanConfig = {
31+
fps: 10,
32+
qrbox: { width: 200, height: 200 },
33+
};
34+
@Output() decodedText: EventEmitter<string> = new EventEmitter<string>();
35+
@Output() decodedResult: EventEmitter<Html5QrcodeResult> =
36+
new EventEmitter<Html5QrcodeResult>();
37+
38+
constructor() {}
39+
40+
ngOnInit(): void {}
41+
42+
ngAfterViewInit() {
43+
// This method will trigger user permissions
44+
Html5Qrcode.getCameras()
45+
.then((devices) => {
46+
if (devices && devices.length) {
47+
// .. use this to start scanning.
48+
this.cameraId = this.useFrontCamera ? devices[1].id : devices[0].id;
49+
this.startHtmlQrCode();
50+
}
51+
})
52+
.catch((err) => {
53+
console.log(err);
54+
});
55+
}
56+
57+
qrCodeSuccessCallback(decodedText: any, decodedResult: Html5QrcodeResult) {
58+
/* handle success */
59+
this.decodedText.emit(decodedText);
60+
this.decodedResult.emit(decodedResult);
61+
}
62+
63+
qrCodeErrorCallback(errorMessage: any) {
64+
/* handle success */
65+
// console.error(errorMessage);
66+
}
67+
68+
startHtmlQrCode() {
69+
this.html5QrCode = new Html5Qrcode(this.reader?.nativeElement?.id);
70+
71+
this.html5QrCode
72+
.start(
73+
{ deviceId: { exact: this.cameraId } },
74+
this.config,
75+
this.qrCodeSuccessCallback.bind(this),
76+
this.qrCodeErrorCallback.bind(this)
77+
)
78+
.catch((err) => {
79+
// Start failed, handle it.
80+
console.log(err);
81+
});
82+
}
83+
84+
ngOnDestroy() {
85+
this.html5QrCode
86+
.stop()
87+
.then((ignore) => {
88+
// QR Code scanning is stopped.
89+
})
90+
.catch((err) => {
91+
// Stop failed, handle it.
92+
});
93+
}
94+
}

0 commit comments

Comments
 (0)