forked from brandingbrand/react-native-barcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scanner.js
82 lines (74 loc) · 2.1 KB
/
Scanner.js
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
import React, { Component } from "react";
import { View, Button, Alert, StyleSheet } from "react-native";
import * as RNLBarCode from "@yyyyu/react-native-barcode";
const Scanner = RNLBarCode.Scanner;
export default class App extends Component {
state = {
visible: true,
scanEnable: true,
scannerSize: 300,
decoder: RNLBarCode.Decoder.Auto,
formats: RNLBarCode.Type.Common.QR_CODE,
torch: RNLBarCode.TorchMode.Off
};
render() {
return (
<View style={styles.container}>
{this.state.visible && (
<Scanner
enable={this.state.scanEnable}
decoder={this.state.decoder}
formats={this.state.formats}
torch={this.state.torch}
onResult={this.handleResult}
style={{
width: this.state.scannerSize,
height: this.state.scannerSize
}}
/>
)}
<Button onPress={this.toggleVisible} title={"Toggle Visible"} />
<Button onPress={this.toggleScanEnable} title={"Toggle ScanEnable"} />
<Button onPress={this.toggleTorch} title={"Toggle Torch"} />
</View>
);
}
handleResult = (result, error) => {
this.setState({ scanEnable: false });
if (error) {
Alert.alert(RNLBarCode.Errors[error.code], error.message, [
{ text: "Ok" }
]);
} else {
Alert.alert(
"Success!",
`format: ${RNLBarCode.Type.Common[result.format]}, content: ${
result.content
}`,
[{ text: "Ok", onPress: () => this.setState({ scanEnable: true }) }]
);
}
};
toggleVisible = () => {
this.setState({ visible: !this.state.visible });
};
toggleScanEnable = () => {
this.setState({ scanEnable: !this.state.scanEnable });
};
toggleTorch = () => {
this.setState({
torch:
this.state.torch === RNLBarCode.TorchMode.Off
? RNLBarCode.TorchMode.On
: RNLBarCode.TorchMode.Off
});
};
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "#fff"
}
});