-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathexample.tsx
227 lines (224 loc) · 6.42 KB
/
example.tsx
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import {StatusBar} from 'expo-status-bar'
import React, {useState, useEffect} from 'react'
import {StyleSheet, Text, View, TouchableOpacity, ImageBackground} from 'react-native'
import {Camera} from 'expo-camera'
const tag = '[CAMERA]'
export default function App() {
const [hasPermission, setHasPermission] = useState<any>(null)
const [previewVisible, setPreviewVisible] = useState(false)
const [capturedImage, setCapturedImage] = useState<any>(null)
const [startOver, setStartOver] = useState(true)
const [type, setType] = useState(Camera.Constants.Type.back)
let camera: Camera
useEffect(() => {
;(async () => {
const {status} = await Camera.requestPermissionsAsync()
setHasPermission(status === 'granted')
})()
}, [])
const __closeCamera = () => {
setStartOver(true)
}
const __takePicture = async () => {
if (!camera) return
const photo = await camera.takePictureAsync()
console.log(photo)
setPreviewVisible(true)
setCapturedImage(photo)
}
const __savePhoto = async () => {}
return (
<View
style={{
flex: 1
}}
>
{startOver ? (
<View
style={{
flex: 1,
backgroundColor: '#fff',
justifyContent: 'center',
alignItems: 'center'
}}
>
<TouchableOpacity
onPress={() => setStartOver(false)}
style={{
width: 130,
borderRadius: 4,
backgroundColor: '#14274e',
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
height: 40
}}
>
<Text
style={{
color: '#fff',
fontWeight: 'bold',
textAlign: 'center'
}}
>
Take picture
</Text>
</TouchableOpacity>
</View>
) : (
<View
style={{
flex: 1
}}
>
{previewVisible ? (
<ImageBackground
source={{uri: capturedImage && capturedImage.uri}}
style={{
flex: 1
}}
>
<View
style={{
flex: 1,
flexDirection: 'column',
padding: 15,
justifyContent: 'flex-end'
}}
>
<View
style={{
flexDirection: 'row',
justifyContent: 'space-between'
}}
>
<TouchableOpacity
onPress={() => setPreviewVisible(false)}
style={{
width: 130,
height: 40,
alignItems: 'center',
borderRadius: 4
}}
>
<Text
style={{
color: '#fff',
fontSize: 20
}}
>
Re-take
</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={__savePhoto}
style={{
width: 130,
height: 40,
alignItems: 'center',
borderRadius: 4
}}
>
<Text
style={{
color: '#fff',
fontSize: 20
}}
>
save photo
</Text>
</TouchableOpacity>
</View>
</View>
</ImageBackground>
) : (
<Camera
style={{flex: 1}}
type={type}
ref={(r) => {
camera = r
}}
>
<View
style={{
flex: 1,
backgroundColor: 'transparent',
flexDirection: 'row'
}}
>
<View
style={{
position: 'absolute',
top: '5%',
right: '5%'
}}
>
<TouchableOpacity onPress={__closeCamera}>
<Text
style={{
color: '#fff',
fontSize: 20
}}
>
X
</Text>
</TouchableOpacity>
</View>
<TouchableOpacity
style={{
position: 'absolute',
top: '5%',
left: '5%'
}}
onPress={() => {
setType(type === Camera.Constants.Type.back ? Camera.Constants.Type.front : Camera.Constants.Type.back)
}}
>
<Text style={{fontSize: 18, marginBottom: 10, color: 'white'}}> Flip </Text>
</TouchableOpacity>
<View
style={{
position: 'absolute',
bottom: 0,
flexDirection: 'row',
flex: 1,
width: '100%',
padding: 20,
justifyContent: 'space-between'
}}
>
<View
style={{
alignSelf: 'center',
flex: 1,
alignItems: 'center'
}}
>
<TouchableOpacity
onPress={__takePicture}
style={{
width: 70,
height: 70,
bottom: 0,
borderRadius: 50,
backgroundColor: '#fff'
}}
/>
</View>
</View>
</View>
</Camera>
)}
</View>
)}
</View>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#000',
alignItems: 'center',
justifyContent: 'center'
}
})