Skip to content

Instantly share code, notes, and snippets.

@aramis-it
Created October 19, 2021 04:58
Show Gist options
  • Save aramis-it/b0c3b0719eddae89d65c9872d064a43f to your computer and use it in GitHub Desktop.
Save aramis-it/b0c3b0719eddae89d65c9872d064a43f to your computer and use it in GitHub Desktop.
expo take photo
import { StatusBar } from 'expo-status-bar';
import React, { useState, useEffect, useRef } from 'react';
import { Platform, StyleSheet, TouchableOpacity } from 'react-native';
import { Camera } from 'expo-camera';
import { Text, View } from '../components/Themed';
import { FontAwesome5 } from '@expo/vector-icons';
export default function ModalScreen() {
const [hasPermission, setHasPermission] = useState(false);
const [type, setType] = useState(Camera.Constants.Type.back);
const cam = useRef<Camera | null>(null)
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 _takePhoto = async () => {
if (cam.current) {
const photo = await cam.current.takePictureAsync()
.then(() => {
console.debug(photo)
})
} else {
console.log('asdfas')
}
}
return (
<View style={styles.container}>
<Text style={styles.title}>Modal</Text>
<View style={styles.separator} lightColor="#eee" darkColor="rgba(255,255,255,0.1)" />
<Camera style={styles.camera} type={type} ref={cam}>
<View style={styles.buttonContainer}>
<TouchableOpacity
style={styles.button}
onPress={() => {
setType(
type === Camera.Constants.Type.back
? Camera.Constants.Type.front
: Camera.Constants.Type.back
);
}}>
<FontAwesome5
name="flipboard"
size={25}
color="white"
style={{ marginRight: 15 }}
/>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={_takePhoto}>
<FontAwesome5
name="camera"
size={25}
color="white"
style={{ marginRight: 15 }}
/>
</TouchableOpacity>
</View>
</Camera>
{/* Use a light status bar on iOS to account for the black space above the modal */}
<StatusBar style={Platform.OS === 'ios' ? 'light' : 'auto'} />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1
},
title: {
fontSize: 20,
fontWeight: 'bold',
},
separator: {
marginVertical: 30,
height: 1,
width: '80%',
},
camera: {
flex: 1,
},
buttonContainer: {
flex: 1,
backgroundColor: 'transparent',
flexDirection: 'row',
margin: 20,
},
button: {
flex: 0.1,
alignSelf: 'flex-end',
alignItems: 'center',
},
text: {
fontSize: 18,
color: 'white',
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment