-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBarCodeScan.js
145 lines (130 loc) · 4.15 KB
/
BarCodeScan.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { StatusBar } from 'expo-status-bar';
import React, { useState, useEffect } from 'react';
import { Dimensions, Text, View, StyleSheet, Button, Platform, Image, Alert } from 'react-native';
import { BarCodeScanner } from 'expo-barcode-scanner';
const { width } = Dimensions.get('window');
import * as Linking from 'expo-linking';
import { useNavigation } from '@react-navigation/native';
export default function BarCodeScan() {
const [hasPermission, setHasPermission] = useState(null);
const [scanned, setScanned] = useState(false);
useEffect(() => {
(async () => {
const { status } = await BarCodeScanner.requestPermissionsAsync();
setHasPermission(status === 'granted');
})();
}, []);
const handleBarCodeScanned = ({ type, data }) => {
setScanned(true);
//Alert.alert(`Bar code with type ${type} and data ${data} has been scanned!`);
Alert.alert(
'Alert Title',
`Bar code with type ${type} and data ` + data + `has been scanned!`,
[
{
text: 'Url',
onPress: () => Linking.openURL(data)
},
{
text: 'Cancel',
onPress: () => console.log('Cancel Pressed'),
style: 'cancel'
},
{ text: 'OK', onPress: () => console.log('OK Pressed') }
],
{ cancelable: false }
);
};
if (hasPermission === null) {
return <Text>Requesting for camera permission</Text>;
}
if (hasPermission === false) {
return <Text>No access to camera</Text>;
}
const navigation = useNavigation();
return (
<BarCodeScanner
onBarCodeScanned={scanned ? undefined : handleBarCodeScanned}
style={[StyleSheet.absoluteFill, styles.container]}>
<View
style={{
position: 'absolute',
top: 0,
left: 0,
width: '100%',
height: '100%',
backgroundColor: 'rgba(0, 0, 0, 0)',
alignItems: 'center',
justifyContent: 'space-around',
}}
>
<Text style={styles.description}>Scan your QR code</Text>
{/* <Image
style={styles.qr}
source={require('./assets/img/QR.png')}
/> */}
<View
style={{
width: 300,
height: 300,
backgroundColor: 'transparent',
backgroundColor: 'rgba(0, 0, 0, 0)',
borderColor: 'white',
borderWidth: 0.5,
}}
/>
<Text
onPress={() => {
navigation.goBack();
}}
style={styles.cancel}>
Cancel
</Text>
{scanned && <Button title={'Tap to Scan Again'} onPress={() => setScanned(false)} />}
</View>
</BarCodeScanner>
);
}
/*
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
*/
const deviceHeight = Dimensions.get("window").height;
const deviceWidth = Dimensions.get("window").width;
const opacity = 'rgba(0, 0, 0, .6)';
const qrSize = width * 0.7
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
width: '100%',
height: '100%',
backgroundColor: 'rgba(0, 0, 0, 1)',
},
qr: {
marginTop: '20%',
marginBottom: '20%',
width: qrSize,
height: qrSize,
},
description: {
fontSize: width * 0.06,
marginTop: '10%',
textAlign: 'center',
width: '60%',
color: 'white',
},
cancel: {
fontSize: width * 0.04,
textAlign: 'center',
width: '100%',
color: 'white',
}
});