-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.js
115 lines (97 loc) · 3.16 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
import React, { useState, useEffect, useRef } from 'react';
import { Text, View, TouchableOpacity, SafeAreaView, Animated } from 'react-native';
import { Camera } from 'expo-camera';
import * as FaceDetector from 'expo-face-detector';
// import Animated, { useCode } from 'react-native-reanimated';
export default function App() {
const [hasPermission, setHasPermission] = useState(null);
const [type, setType] = useState(Camera.Constants.Type.back);
const transX = useRef(new Animated.Value(0)).current;
const transY = useRef(new Animated.Value(0)).current;
const [boxSize, setBoxSize] = useState({
height: 0,
width: 0
})
const [hasFaceInFrame, setHasFaceInFrame] = useState(false)
useEffect(() => {
(async () => {
const { status } = await Camera.requestPermissionsAsync();
setHasPermission(status === 'granted');
})();
}, []);
if (hasPermission === null) {
return <View />;
}
if (hasPermission === false) {
return <Text>No access to camera</Text>;
}
const handleFacesDetected = (faces) => {
setHasFaceInFrame(() => faces.faces.length >= 1)
if (faces.faces.length < 1) return
Animated.timing(transX, {
useNativeDriver: true,
toValue: faces.faces[0].bounds.origin.x,
}).start();
Animated.timing(transY, {
useNativeDriver: true,
toValue: faces.faces[0].bounds.origin.y,
}).start()
setBoxSize({
height: faces.faces[0].bounds.size.height,
width: faces.faces[0].bounds.size.width
})
}
return (
<Camera style={{ flex: 1 }} zoom={0} type={type}
onFacesDetected={handleFacesDetected}
faceDetectorSettings={{
mode: FaceDetector.Constants.Mode.accurate,
detectLandmarks: FaceDetector.Constants.Landmarks.none,
runClassifications: FaceDetector.Constants.Classifications.none,
minDetectionInterval: 100,
tracking: true,
}}
>
{hasFaceInFrame && <Animated.View style={{
position: 'absolute',
width: boxSize.width,
height: boxSize.height,
borderWidth: 2,
borderRadius: 6,
borderColor: 'green',
transform: [
{
translateX: transX,
},
{
translateY: transY,
}
],
}} />}
<SafeAreaView style={{ flex: 1 }}>
{hasFaceInFrame && <Text style={{ position: 'absolute', bottom: '20%', left: '30%', fontSize: 20, color: 'red' }}>Face Detected</Text>}
<View
style={{
flex: 1,
backgroundColor: 'transparent',
flexDirection: 'row',
}}>
<TouchableOpacity
style={{
alignSelf: 'flex-start',
alignItems: 'center',
}}
onPress={() => {
setType(
type === Camera.Constants.Type.back
? Camera.Constants.Type.front
: Camera.Constants.Type.back
);
}}>
<Text style={{ fontSize: 18, marginBottom: 10, color: 'red', marginLeft: 10, fontWeight: 'bold' }}> Flip Camera </Text>
</TouchableOpacity>
</View>
</SafeAreaView>
</Camera>
);
}