Skip to content

Instantly share code, notes, and snippets.

@leleofg
Last active April 23, 2019 13:25
Show Gist options
  • Save leleofg/29ea503b6ffadd1be97c53facf933740 to your computer and use it in GitHub Desktop.
Save leleofg/29ea503b6ffadd1be97c53facf933740 to your computer and use it in GitHub Desktop.
camera.js
import React, { useState } from "react";
import { StyleSheet, TouchableOpacity, Text, ImageBackground } from "react-native";
import { RNCamera } from "react-native-camera";
export default Camera = () => {
const [imageUri, setImageUri] = useState(null);
takePicture = async () => {
try {
if (this.camera) {
const options = {
quality: 0.5,
base64: true,
forceUpOrientation: true,
fixOrientation: true
};
const { uri } = await this.camera.takePictureAsync(options);
setImageUri(uri);
}
} catch (err) {
alert(err.message);
}
}
return (
imageUri ?
<ImageBackground style={styles.preview} source={{ uri: imageUri }}>
</ImageBackground>
:
<RNCamera
ref={camera => { this.camera = camera; }}
style={styles.camera}
type={RNCamera.Constants.Type.front}
autoFocus={RNCamera.Constants.AutoFocus.on}
flashMode={RNCamera.Constants.FlashMode.off}
permissionDialogTitle={"Permission to use camera"}
permissionDialogMessage={"We need your permission to use your camera phone"}
>
<TouchableOpacity onPress={takePicture} style={styles.button}>
<Text>PICTURE</Text>
</TouchableOpacity>
</RNCamera>
)
}
const styles = StyleSheet.create({
camera: {
flex: 1
},
button: {
alignSelf: "center",
backgroundColor: "blue",
color: "#fff"
},
preview: {
width: "100%",
height: "100%",
flex: 1
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment