-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomeScreen.js
207 lines (198 loc) · 5.23 KB
/
HomeScreen.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
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
import React from 'react';
import {
StyleSheet,
Text,
View,
Button,
Dimensions,
Image,
} from 'react-native';
import { Camera, Permissions, FaceDetector } from 'expo';
// eslint-disable-next-line import/no-extraneous-dependencies
import { Ionicons } from '@expo/vector-icons';
// import { WebBrowser } from 'expo';
export default class HomeScreen extends React.Component {
static navigationOptions = {
header: null,
};
state = {
hasCameraPermission: null,
type: Camera.Constants.Type.front,
image: null,
faces: [],
};
askPermission = async () => {
const { status } = await Permissions.askAsync(Permissions.CAMERA);
this.setState({ hasCameraPermission: status === 'granted' });
};
snap = async () => {
if (this.camera) {
const { uri } = await this.camera.takePictureAsync();
this.setState({ image: uri });
const { faces } = await this.detectFaces(uri);
this.setState({ faces });
}
};
detectFaces = async imageUri => {
const options = { mode: FaceDetector.Constants.Mode.fast };
// eslint-disable-next-line no-return-await
return await FaceDetector.detectFacesAsync(imageUri, options);
};
render() {
const { hasCameraPermission, type, image, faces } = this.state;
const { navigation } = this.props;
if (hasCameraPermission === null) {
return (
<View style={styles.containerCenter}>
<Text style={styles.text}>Click to open the Camera</Text>
<Text style={styles.textPermission}>
{`(Please allow if it asks for permission)`}
</Text>
<Button
onPress={this.askPermission}
title="Open Camera"
color="#008080"
/>
</View>
);
}
if (hasCameraPermission === false) {
return (
<View style={styles.containerCenter}>
<Text style={styles.text}>
{`No access to camera\nPlease allow access to camera from settings to continue`}
</Text>
</View>
);
}
return !image ? (
<View style={{ flex: 1 }}>
<Camera
style={{
height: '100%',
maxHeight: Dimensions.get('window').width + 100,
}}
type={type}
ref={ref => {
this.camera = ref;
}}
/>
<View style={styles.actionBtns}>
<Ionicons
name="md-reverse-camera"
size={35}
color="#008080"
onPress={() => {
this.setState({
type:
type === Camera.Constants.Type.back
? Camera.Constants.Type.front
: Camera.Constants.Type.back,
});
}}
/>
<Ionicons
name="md-radio-button-on"
size={80}
color="#808080"
onPress={() => this.snap()}
/>
<Ionicons
name="md-close-circle"
size={35}
color="#ff5500"
onPress={() => this.setState({ hasCameraPermission: null })}
/>
</View>
</View>
) : (
<View style={{ flex: 1 }}>
<Image
source={{ uri: image }}
style={{
height: '85%',
minHeight: Dimensions.get('window').width,
width: '100%',
}}
/>
{faces.length > 0 ? (
<View
style={{
justifyContent: 'center',
alignItems: 'center',
flexDirection: 'column',
height: '15%',
}}
>
<Text style={{ marginBottom: 10 }}>Hi there!</Text>
<Button
onPress={() => navigation.navigate('Quiz')}
title="Proceed"
color="#008080"
/>
</View>
) : (
<View
style={{
justifyContent: 'center',
alignItems: 'center',
flexDirection: 'column',
height: '15%',
}}
>
<Text style={{ marginBottom: 10 }}>No Face Found!</Text>
<Button
onPress={() => this.setState({ image: null })}
title="Retake"
color="#ff5500"
/>
</View>
)}
</View>
);
}
// _handleHelpPress = () => {
// WebBrowser.openBrowserAsync(
// 'https://docs.expo.io/versions/latest/guides/up-and-running.html#can-t-see-your-changes',
// );
// };
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
containerCenter: {
flex: 1,
backgroundColor: '#fff',
justifyContent: 'center',
alignItems: 'center',
paddingTop: 30,
},
text: {
fontSize: 17,
color: 'rgba(96,100,109, 1)',
textAlign: 'center',
fontFamily: 'Century-Gothic',
padding: 30,
paddingTop: 10,
paddingBottom: 0,
},
textPermission: {
fontSize: 13,
color: 'rgba(96,100,109, 1)',
textAlign: 'center',
fontFamily: 'Century-Gothic',
padding: 30,
paddingTop: 0,
paddingBottom: 10,
},
actionBtns: {
flex: 1,
flexDirection: 'row',
justifyContent: 'space-evenly',
alignItems: 'center',
backgroundColor: '#fff',
padding: 10,
},
});