-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreactsample
97 lines (72 loc) · 2.12 KB
/
reactsample
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import React from "react";
class App extends React.PureComponent {
constructor(props) {
super(props);
this.myRef = React.createRef();
this.html5QrCode = null;
this.addedScript = null;
this.addJStoDom(process.env.PUBLIC_URL+'qrJS/html5-qrcode.min.js');
}
componentWillUnmount(){
this.removeJSFromDom();
}
addJStoDom(url) {
if (!!this.addedScript === false) {
this.addedScript = document.createElement("script");
this.addedScript.src = url;
this.addedScript.async = true;
document.body.appendChild(this.addedScript);
}
}
removeJSFromDom() {
document.body.removeChild(this.addedScript);
}
startCam() {
this.html5QrCode = new window.Html5Qrcode("reader", false);
console.log( this.html5QrCode );
window.Html5Qrcode.getCameras().then(devices => {
if (devices && devices.length) {
this.start(devices);
}else{
console.log("Error in identify camera");
alert("Error in identify camera1");
}
}).catch(err => {
console.log("Error in identify camera");
alert("Error in identify camera2");
});
}
start(devices){
const config = { fps: 10, qrbox: 400 };
if(devices.length > 1){
this.html5QrCode.start({ facingMode: { exact: "environment"} }, config, this.onScanSuccess,this.onErrorScan).catch(err => {
console.log("Unable to start scanning, error: "+err);
alert("Error in identify camera4");
});
}else{
this.html5QrCode.start({ deviceId: { exact: devices[0].id} }, config, this.onScanSuccess,this.onErrorScan).catch(err => {
console.log("Unable to start scanning, error: "+err);
alert("Error in identify camera5");
});
}
}
onScanSuccess(qrCodeMessage) {
console.log(qrCodeMessage);
alert(qrCodeMessage);
this.html5QrCode.stop();
}
onErrorScan(err){
}
stopScan(){
this.html5QrCode.stop();
}
render() {
return(
<>
<button onClick={this.startCam.bind(this)}>Start</button>
<div id={'reader'} ref={this.myRef} style={{width: "600px",backgroundColor: "black", margin: "auto"}}></div>
</>
);
}
}
export default App;