-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.js
270 lines (242 loc) · 7.37 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import {runOnJS, useDerivedValue} from 'react-native-reanimated';
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import {
Platform,
Pressable,
StyleSheet,
useWindowDimensions,
View,
} from 'react-native';
import {
useCameraDevices,
Camera,
useFrameProcessor,
useCameraFormat,
} from 'react-native-vision-camera';
// import {useIsFocused} from '@react-navigation/native';
import {scanFaces} from 'vision-camera-face-detector';
import IonIcon from 'react-native-vector-icons/Ionicons';
import {CONTENT_SPACING, MAX_ZOOM_FACTOR, SAFE_AREA_PADDING} from './Constants';
import {rotationDegrees} from './rotationDegrees';
import adjustToView from './adjustToView';
import Reanimated, {
useAnimatedProps,
useSharedValue,
} from 'react-native-reanimated';
import {useIsForeground} from './hooks/useIsForeground';
import {CaptureButton} from './CaptureButton';
const ReanimatedCamera = Reanimated.createAnimatedComponent(Camera);
Reanimated.addWhitelistedNativeProps({
zoom: true,
});
const BUTTON_SIZE = 40;
const App = () => {
const camera = useRef(null);
const format = useCameraFormat();
const devices = useCameraDevices('wide-angle-camera');
const [frameDimensions, setFrameDimensions] = useState();
const [hasPermission, setHasPermission] = useState(false);
const [isCameraInitialized, setIsCameraInitialized] = useState(false);
const [faces, setFaces] = useState();
const [cameraPosition, setCameraPosition] = useState('front');
const [flash, setFlash] = useState('off');
const device = devices[cameraPosition];
const zoom = useSharedValue(0);
const isPressingButton = useSharedValue(true);
const {width, height} = useWindowDimensions();
const minZoom = device?.minZoom ?? 1;
const maxZoom = Math.min(device?.maxZoom ?? 1, MAX_ZOOM_FACTOR);
const supportsFlash = device?.hasFlash ?? false;
// check if camera page is active
//const isFocussed = useIsFocused();
const isForeground = useIsForeground();
const isActive = true && isForeground;
const cameraAnimatedProps = useAnimatedProps(() => {
const z = Math.max(Math.min(zoom.value, maxZoom), minZoom);
return {
zoom: z,
};
}, [maxZoom, minZoom, zoom]);
const supportsCameraFlipping = useMemo(
() => devices.back != null && devices.front != null,
[devices.back, devices.front],
);
const style = useMemo(
() => ({position: 'absolute', top: 0, left: 0, width, height}),
[width, height],
);
useEffect(() => {
(async () => {
const status = await Camera.requestCameraPermission();
setHasPermission(status === 'authorized');
})();
}, []);
const onFlipCameraPressed = useCallback(() => {
setCameraPosition(p => (p === 'back' ? 'front' : 'back'));
}, []);
const onFlashPressed = useCallback(() => {
setFlash(f => (f === 'off' ? 'on' : 'off'));
}, []);
const onError = useCallback(error => {
console.error(error);
}, []);
const onInitialized = useCallback(() => {
console.log('Camera initialized!');
setIsCameraInitialized(true);
}, []);
const handleScan = useCallback((frame, rotationDegrees$, newFaces) => {
const isRotated = rotationDegrees$ === 90 || rotationDegrees$ === 270;
setFrameDimensions(
isRotated
? {
width: frame.height,
height: frame.width,
}
: {
width: frame.width,
height: frame.height,
},
);
setFaces(newFaces);
}, []);
const frameProcessor = useFrameProcessor(frame => {
'worklet';
const rotation = rotationDegrees(frame);
const scanface = scanFaces(frame);
runOnJS(handleScan)(frame, rotation, scanface);
}, []);
const onMediaCaptured = useCallback(media => {
console.log(`Media captured! ${JSON.stringify(media)}`);
// navigation.navigate('MediaPage', {
// path: media.path,
// type: type,
// });
}, []);
const setIsPressingButton = useCallback(
_isPressingButton => {
isPressingButton.value = _isPressingButton;
},
[isPressingButton],
);
return (
<View style={styles.container}>
{device != null && (
<Reanimated.View style={StyleSheet.absoluteFill}>
<ReanimatedCamera
ref={camera}
format={format}
style={StyleSheet.absoluteFill}
device={device}
fps={30}
hdr={true}
photo={true}
enableZoomGesture={true}
isActive={isActive}
onInitialized={onInitialized}
onError={onError}
animatedProps={cameraAnimatedProps}
frameProcessor={frameProcessor}
// frameProcessor={() => {
// randomWidth.value = Math.round(Math.random() * 350);
// }}
orientation="portrait"
frameProcessorFps={30}
/>
</Reanimated.View>
)}
<View style={style}>
{frameDimensions &&
(() => {
const mirrored =
Platform.OS === 'android' && cameraPosition === 'front';
const {adjustRect} = adjustToView(frameDimensions, {width, height});
return faces?.map((i, index) => {
const {left, ...others} = adjustRect(i.bounds);
return (
<View
key={index}
style={[
styles.boundingBox,
{
...others,
[mirrored ? 'right' : 'left']: left,
},
]}
/>
);
});
})()}
</View>
<CaptureButton
style={styles.captureButton}
camera={camera}
onMediaCaptured={onMediaCaptured}
cameraZoom={zoom}
minZoom={minZoom}
maxZoom={maxZoom}
flash={supportsFlash ? flash : 'off'}
enabled={isCameraInitialized && isActive}
setIsPressingButton={setIsPressingButton}
/>
<View style={styles.rightButtonRow}>
{supportsCameraFlipping && (
<Pressable
style={styles.button}
onPress={onFlipCameraPressed}
disabledOpacity={0.4}>
<IonIcon name="camera-reverse" color="white" size={24} />
</Pressable>
)}
{supportsFlash && (
<Pressable
style={styles.button}
onPress={onFlashPressed}
disabledOpacity={0.4}>
<IonIcon
name={flash === 'on' ? 'flash' : 'flash-off'}
color="white"
size={24}
/>
</Pressable>
)}
</View>
</View>
);
};
export default App;
const styles = StyleSheet.create({
boundingBox: {
position: 'absolute',
borderColor: '#fff',
borderWidth: 1,
},
container: {
flex: 1,
backgroundColor: 'black',
},
captureButton: {
position: 'absolute',
alignSelf: 'center',
bottom: SAFE_AREA_PADDING.paddingBottom,
},
button: {
marginBottom: CONTENT_SPACING,
width: BUTTON_SIZE,
height: BUTTON_SIZE,
borderRadius: BUTTON_SIZE / 2,
backgroundColor: 'rgba(140, 140, 140, 0.3)',
justifyContent: 'center',
alignItems: 'center',
},
rightButtonRow: {
position: 'absolute',
right: SAFE_AREA_PADDING.paddingRight,
top: SAFE_AREA_PADDING.paddingTop,
},
text: {
color: 'white',
fontSize: 11,
fontWeight: 'bold',
textAlign: 'center',
},
});