-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
129 lines (116 loc) · 3.11 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
import React, { useState, useEffect, useRef } from 'react';
import { StyleSheet, Text, View, TouchableOpacity, Dimensions, Alert } from 'react-native';
import { Camera } from 'expo-camera';
import HorizontalScrollView from './components/HorizontalScrollView';
import { ImageBackground } from 'react-native-web';
export default function App() {
const [hasPermission, setHasPermission] = useState(null);
const [type, setType] = useState(Camera.Constants.Type.back);
const [showCamera, setShowCamera] = useState(true);
// camera ref to access Camera
const cameraRef = useRef(null);
/**
* called to take the photo
*/
const takePhoto = async () => {
if (cameraRef) {
console.log("in take picture");
try {
let photo = await cameraRef.current.takePictureAsync({
allowsEditing: true,
aspect: [4,3],
quality: 1,
});
return photo
} catch (e) {
console.log(e);
}
}
}
useEffect(() => {
(async () => {
const { status } = await Camera.requestCameraPermissionsAsync();
setHasPermission(status === 'granted');
})();
}, []);
if (hasPermission === null) {
return <View />;
}
if (hasPermission === false) {
return <Text>No access to camera</Text>;
}
return (
<View style={styles.container}>
<Camera style={styles.camera} type={type} ref={cameraRef}>
<View style={styles.flipButtonContainer}>
<TouchableOpacity
style={styles.flipButton}
onPress={() => {
setType(
type === Camera.Constants.Type.back
? Camera.Constants.Type.front
: Camera.Constants.Type.back
);
}}>
<Text style={styles.text}> Flip </Text>
</TouchableOpacity>
</View>
<View style={styles.cameraCaptureButtonContainer}>
<TouchableOpacity
style={styles.cameraCaptureButton}
onPress={async () => {
const r = await takePhoto();
Alert.alert("DEBUG", JSON.stringify(r))
}}
>
</TouchableOpacity>
</View>
</Camera>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
cameraCaptureButtonContainer: {
flex: 1,
backgroundColor: 'transparent',
justifyContent: 'center',
flexDirection: 'row',
margin: 20
},
cameraCaptureButton: {
flex: 0.5,
alignSelf: 'flex-end',
alignItems: 'center',
width: Dimensions.get('window').width / 5,
height: Dimensions.get('window').width / 5,
borderRadius: 50,
borderWidth: 3,
borderColor: '#4169e1'
},
flipButtonContainer: {
flex: 1,
backgroundColor: 'transparent',
flexDirection: 'column',
margin: 20
},
flipButton: {
flex: 0.1,
alignSelf: 'flex-end',
alignItems: 'center'
},
text: {
fontSize: 18,
color: 'red'
},
camera: {
flex: 1,
width: Dimensions.get('window').width,
height: Dimensions.get('window').height
}
});