react-native-vision-camera/example/src/Settings.tsx

111 lines
3.0 KiB
TypeScript
Raw Normal View History

2021-02-19 12:05:02 -07:00
import React, { useCallback } from 'react';
import { StyleSheet, View, Text, Linking } from 'react-native';
2021-02-20 09:39:04 -07:00
import { Navigation, NavigationFunctionComponent } from 'react-native-navigation';
import Slider from '@react-native-community/slider';
import { useState } from 'react';
import { useEffect } from 'react';
import { Camera } from 'react-native-vision-camera';
import { SAFE_AREA_PADDING } from './Constants';
import { useSelector } from 'pipestate';
import { FpsSelector } from './state/selectors';
export const Settings: NavigationFunctionComponent = ({ componentId }) => {
const [fpsSelector, setFpsSelector] = useSelector(FpsSelector);
const [fps, setFps] = useState(fpsSelector);
const [minFps, setMinFps] = useState<number>();
const [maxFps, setMaxFps] = useState<number>();
2021-02-19 10:02:24 -07:00
2021-02-19 12:05:02 -07:00
const onCuventPressed = useCallback(() => {
2021-02-20 09:07:10 -07:00
Linking.openURL('https://cuvent.com');
2021-02-19 12:05:02 -07:00
}, []);
2021-02-20 09:39:04 -07:00
useEffect(() => {
const loadFormats = async (): Promise<void> => {
const devices = await Camera.getAvailableCameraDevices();
const formats = devices.flatMap((d) => d.formats);
let max = 0,
min = 0;
formats.forEach((format) => {
const frameRates = format.frameRateRanges.map((f) => f.maxFrameRate).sort((left, right) => left - right);
const highest = frameRates[frameRates.length - 1] as number;
const lowest = frameRates[0] as number;
if (highest > max) max = highest;
if (lowest < min) min = lowest;
});
setMaxFps(max);
setMinFps(min);
};
loadFormats();
}, []);
useEffect(() => {
const listener = Navigation.events().registerScreenPoppedListener((event) => {
2021-02-20 09:39:41 -07:00
if (event.componentId === componentId) setFpsSelector(Math.round(fps));
2021-02-20 09:39:04 -07:00
});
return () => {
listener.remove();
};
}, [componentId, fps, setFpsSelector]);
2021-02-19 10:02:24 -07:00
return (
<View style={styles.container}>
2021-02-20 09:39:04 -07:00
<View style={styles.vControl}>
2021-02-20 09:39:41 -07:00
<Text>Frame Rate (FPS): {Math.round(fps)}</Text>
2021-02-20 09:39:04 -07:00
{minFps != null && maxFps != null && <Slider minimumValue={minFps} maximumValue={maxFps} value={fps} onValueChange={setFps} />}
</View>
<View style={styles.spacer} />
2021-02-20 09:07:10 -07:00
<Text style={styles.aboutText}>
Vision Camera is powered by{' '}
<Text style={styles.hyperlink} onPress={onCuventPressed}>
Cuvent
</Text>
.
2021-02-19 12:05:02 -07:00
</Text>
2021-02-19 10:02:24 -07:00
</View>
);
2021-02-20 09:07:10 -07:00
};
2021-02-19 10:02:24 -07:00
2021-02-19 12:05:02 -07:00
Settings.options = {
topBar: {
visible: true,
title: {
2021-02-20 09:07:10 -07:00
text: 'Settings',
2021-02-19 12:05:02 -07:00
},
backButton: {
id: 'back',
showTitle: true,
2021-02-20 09:07:10 -07:00
},
},
};
2021-02-19 12:05:02 -07:00
2021-02-19 10:02:24 -07:00
const styles = StyleSheet.create({
container: {
flex: 1,
2021-02-20 09:39:04 -07:00
alignItems: 'stretch',
2021-02-19 10:02:24 -07:00
justifyContent: 'center',
backgroundColor: 'white',
2021-02-20 09:39:04 -07:00
...SAFE_AREA_PADDING,
2021-02-19 10:02:24 -07:00
},
2021-02-19 12:05:02 -07:00
aboutText: {
2021-02-20 09:39:04 -07:00
alignSelf: 'center',
2021-02-19 12:05:02 -07:00
fontSize: 14,
fontWeight: 'bold',
color: '#A9A9A9',
maxWidth: '50%',
textAlign: 'center',
},
hyperlink: {
color: '#007aff',
fontWeight: 'bold',
2021-02-19 10:02:24 -07:00
},
2021-02-20 09:39:04 -07:00
vControl: {
width: '100%',
},
spacer: {
flex: 1,
},
2021-02-19 10:02:24 -07:00
});