forked from vivekjne/react-native-barcode-scanner-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
171 lines (157 loc) · 4.23 KB
/
App.js
File metadata and controls
171 lines (157 loc) · 4.23 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import React, {useState, useEffect} from 'react'
import {
Text,
View,
StyleSheet,
Button,
Animated,
TouchableOpacity,
Image,
} from 'react-native'
import {Permissions} from 'react-native-unimodules'
import {BarCodeScanner} from 'expo-barcode-scanner'
const App: () => React$Node = () => {
const [hasCameraPermission, setCameraPermission] = useState(null)
const [scanned, setScanned] = useState(false)
const [animationLineHeight, setAnimationLineHeight] = useState({
x: 0,
y: 0,
width: 0,
height: 0,
})
const [focusLineAnimation, setFocusLineAnimation] = useState(
new Animated.Value(0),
)
useEffect(() => {
getPermissionsAsync()
animateLine()
}, [])
const animateLine = () => {
Animated.sequence([
Animated.timing(focusLineAnimation, {
toValue: 1,
duration: 1000,
}),
Animated.timing(focusLineAnimation, {
toValue: 0,
duration: 1000,
}),
]).start(animateLine)
}
const getPermissionsAsync = async () => {
const {status} = await Permissions.askAsync(Permissions.CAMERA)
const isPermissionGranted = status === 'granted'
console.log(isPermissionGranted)
setCameraPermission(isPermissionGranted)
}
const handleBarCodeScanned = ({type, data}) => {
setScanned(true)
alert(`Bar code with type ${type} and data ${data} has been scanned!`)
}
if (hasCameraPermission === null) {
return (
<View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
<Text>Requesting for camera permission</Text>
</View>
)
}
if (hasCameraPermission === false) {
return (
<View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
<Text>No access to camera</Text>
</View>
)
}
return (
<View style={styles.container}>
<BarCodeScanner
onBarCodeScanned={scanned ? undefined : handleBarCodeScanned}
style={StyleSheet.absoluteFillObject}
/>
<View style={styles.overlay}>
<View style={styles.unfocusedContainer}></View>
<View style={styles.middleContainer}>
<View style={styles.unfocusedContainer}></View>
<View
onLayout={e =>
setAnimationLineHeight({
x: e.nativeEvent.layout.x,
y: e.nativeEvent.layout.y,
height: e.nativeEvent.layout.height,
width: e.nativeEvent.layout.width,
})
}
style={styles.focusedContainer}>
{!scanned && (
<Animated.View
style={[
styles.animationLineStyle,
{
transform: [
{
translateY: focusLineAnimation.interpolate({
inputRange: [0, 1],
outputRange: [0, animationLineHeight.height],
}),
},
],
},
]}
/>
)}
{scanned && (
<TouchableOpacity
onPress={() => setScanned(false)}
style={styles.rescanIconContainer}>
<Image
source={require('./rescan.png')}
style={{width: 50, height: 50}}
/>
</TouchableOpacity>
)}
</View>
<View style={styles.unfocusedContainer}></View>
</View>
<View style={styles.unfocusedContainer}></View>
</View>
{/* {scanned && (
<Button title={'Tap to Scan Again'} onPress={() => setScanned(false)} />
)} */}
</View>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
position: 'relative',
},
overlay: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
},
unfocusedContainer: {
flex: 1,
backgroundColor: 'rgba(0,0,0,0.7)',
},
middleContainer: {
flexDirection: 'row',
flex: 1.5,
},
focusedContainer: {
flex: 6,
},
animationLineStyle: {
height: 2,
width: '100%',
backgroundColor: 'red',
},
rescanIconContainer: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
})
export default App