-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.js
130 lines (129 loc) · 3.78 KB
/
App.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import React, { Component } from 'react';
import { Text, View, Linking, TouchableHighlight, PermissionsAndroid, Platform, StyleSheet} from 'react-native';
import { CameraKitCameraScreen, } from 'react-native-camera-kit';
export default class App extends Component {
constructor() {
super();
this.state = {
//variable to hold the qr value
qrvalue: '',
openScanner: false,
};
}
onOpenlink() {
//Function to open URL, If scanned
Linking.openURL(this.state.qrvalue);
//Linking used to open the URL in any browser
}
onBarcodeScan(qrvalue) {
//called after te successful scanning of QRCode/Barcode
this.setState({ qrvalue: qrvalue });
this.setState({ openScanner: false });
}
onopenScanner() {
var that =this;
//To Start Scanning
if(Platform.OS === 'android'){
async function requestCameraPermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.CAMERA,{
'title': 'CameraExample App Camera Permission',
'message': 'CameraExample App needs access to your camera '
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
//If CAMERA Permission is granted
that.setState({ qrvalue: '' });
that.setState({ openScanner: true });
} else {
alert("CAMERA permission denied");
}
} catch (err) {
alert("Camera permission err",err);
console.warn(err);
}
}
//Calling the camera permission function
requestCameraPermission();
}else{
that.setState({ qrvalue: '' });
that.setState({ openScanner: true });
}
}
render() {
let displayModal;
//If qrvalue is set then return this view
if (!this.state.openScanner) {
return (
<View style={styles.container}>
<Text style={styles.heading}>React Native QR Code Example</Text>
<Text style={styles.simpleText}>{this.state.qrvalue ? 'Scanned QR Code: '+this.state.qrvalue : ''}</Text>
{this.state.qrvalue.includes("http") ?
<TouchableHighlight
onPress={() => this.onOpenlink()}
style={styles.button}>
<Text style={{ color: '#FFFFFF', fontSize: 12 }}>Open Link</Text>
</TouchableHighlight>
: null
}
<TouchableHighlight
onPress={() => this.onopenScanner()}
style={styles.button}>
<Text style={{ color: '#FFFFFF', fontSize: 12 }}>
Open QR Scanner
</Text>
</TouchableHighlight>
</View>
);
}
return (
<View style={{ flex: 1 }}>
<CameraKitCameraScreen
showFrame={false}
//Show/hide scan frame
scanBarcode={true}
//Can restrict for the QR Code only
laserColor={'blue'}
//Color can be of your choice
frameColor={'yellow'}
//If frame is visible then frame color
colorForScannerFrame={'black'}
//Scanner Frame color
onReadCode={event =>
this.onBarcodeScan(event.nativeEvent.codeStringValue)
}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor:'white'
},
button: {
alignItems: 'center',
backgroundColor: '#2c3539',
padding: 10,
width:300,
marginTop:16
},
heading: {
color: 'black',
fontSize: 24,
alignSelf: 'center',
padding: 10,
marginTop: 30
},
simpleText: {
color: 'black',
fontSize: 20,
alignSelf: 'center',
padding: 10,
marginTop: 16
}
});