-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
133 lines (120 loc) · 3.35 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
131
132
133
import React, { useState, useEffect, useRef } from 'react';
import { StyleSheet, Text, View, SafeAreaView, TouchableOpacity, Modal, Image, Button } from 'react-native';
import { CameraView, useCameraPermissions } from 'expo-camera';
import { FontAwesome } from '@expo/vector-icons';
export default function App() {
const camRef = useRef(null);
const [facing, setFacing] = useState('back');
const [permission, requestPermission] = useCameraPermissions();
const [capturedPhoto, setCapturedPhoto] = useState(null);
const [open, setOpen] = useState(false);
useEffect(() => {
(async () => {
const { status } = await Camera.requestCameraPermissionsAsync();
requestPermission(status === 'granted');
})();
}, []);
if (!permission) {
return <View />;
}
if (!permission.granted) {
return (
<View style={styles.container}>
<Text style={{ textAlign: 'center' }}>Acesso negado</Text>
<Button onPress={requestPermission} title="grant permission" />
</View>
);
}
function toggleCameraFacing() {
setFacing(current => (current === 'back' ? 'front' : 'back'));
}
async function takePicture() {
if (camRef) {
const data = await camRef.current.takePictureAsync();
setCapturedPhoto(data.uri);
setOpen(true);
console.log(data);
}
}
return (
<SafeAreaView style={styles.container}>
<CameraView style={styles.camera} facing={facing} ref={camRef}>
<View style={styles.contentButtons}>
<TouchableOpacity
style={styles.buttonFlip}
onPress={toggleCameraFacing}>
<FontAwesome name="exchange" size={23} color="red"></FontAwesome>
</TouchableOpacity>
<TouchableOpacity style={styles.buttonCamera} onPress={takePicture}>
<FontAwesome name="camera" size={23} color="#fff"></FontAwesome>
</TouchableOpacity>
</View>
</CameraView>
{capturedPhoto && (
<Modal animationType="slide" transparent={true} visible={open}>
<View style={styles.contentModal}>
<TouchableOpacity style={styles.closeButton} onPress={() => { setOpen(false) }}>
<FontAwesome name="close" size={50} color="#fff"></FontAwesome>
</TouchableOpacity>
<Image style={styles.imgPhoto} source={{ uri: capturedPhoto }}></Image>
</View>
</Modal>
)}
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
},
camera: {
width: '100%',
height: '100%',
},
contentButtons: {
flex: 1,
backgroundColor: 'transparent',
flexDirection: 'row',
},
buttonFlip: {
position: 'absolute',
bottom: 50,
left: 30,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#fff',
margin: 20,
height: 50,
width: 50,
borderRadius: 50,
},
buttonCamera: {
position: 'absolute',
bottom: 50,
right: 30,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'red',
margin: 20,
height: 50,
width: 50,
borderRadius: 50,
},
contentModal: {
flex: 1,
justifyContent: 'center',
alignItems: 'flex-end',
margin: 20,
},
closeButton: {
position: 'absolute',
top: 10,
left: 2,
margin: 10,
},
imgPhoto: {
width: '100%',
height: 400,
},
});