-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCameraPage.js
More file actions
122 lines (109 loc) · 3.63 KB
/
CameraPage.js
File metadata and controls
122 lines (109 loc) · 3.63 KB
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
import React from 'react';
import { Text, View, TouchableOpacity, Platform } from 'react-native';
import { Camera } from 'expo-camera';
import * as Permissions from 'expo-permissions';
import * as ImagePicker from 'expo-image-picker';
import * as MediaLibrary from 'expo-media-library';
import { FontAwesome, Ionicons } from '@expo/vector-icons';
class CameraPage extends React.Component {
constructor(props) {
super(props)
this.state = {
userID: this.props.route.params.userID,
hasPermission: null,
type: Camera.Constants.Type.back,
imagefile: ''
}
}
async componentDidMount() {
this.getPermissionAsync()
}
getPermissionAsync = async () => {
// Camera roll Permission
if (Platform.OS === 'ios') {
const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
if (status !== 'granted') {
alert('Sorry, we need camera roll permissions to make this work!');
}
}
// Camera Permission
const { status } = await Permissions.askAsync(Permissions.CAMERA);
this.setState({ hasPermission: status === 'granted' });
}
takePicture = async () => {
const { uri } = await this.camera.takePictureAsync();
const asset = await MediaLibrary.createAssetAsync(uri);
console.log('Button Pressed');
MediaLibrary.createAlbumAsync('Expo', asset)
.then(() => {
this.setState({ imagefile: uri });
this.props.navigation.navigate('EditPage', {userID: this.state.userID, image: uri });
})
.catch(error => {
console.log('err', error);
});
}
pickImage = async () => {
let result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.All,
allowsEditing: true,
aspect: [4, 3],
quality: 1,
});
if (!result.cancelled) {
this.setState({ imagefile: result.uri });
this.props.navigation.navigate('EditPage', {userID: this.state.userID, image: result.uri})
}
};
render() {
const { hasPermission, image } = this.state
if (hasPermission === null) {
return <View />;
} else if (hasPermission === false) {
return <Text>No access to camera</Text>;
} else {
return (
<View style={{ flex: 1 }}>
<Camera style={{ flex: 8 }} ref={ (ref) => {this.camera = ref} } type={this.state.cameraType}>
</Camera>
<View style={{flex: 2, flexDirection:"row",alignItems: 'center',justifyContent:"space-between", backgroundColor: '#1d1d2a'}}>
<TouchableOpacity
style={{
marginLeft: 20,
backgroundColor: 'transparent',
}}
onPress={()=>this.props.navigation.navigate('CarStatus')}
>
<FontAwesome
name="window-close"
style={{ color: "#fff", fontSize: 40}}
/>
</TouchableOpacity>
<TouchableOpacity
style={{
backgroundColor: 'transparent',
}}
onPress={()=>this.takePicture()}>
<FontAwesome
name="camera"
style={{ color: "#fff", fontSize: 40}}
/>
</TouchableOpacity>
<TouchableOpacity
style={{
marginRight: 20,
backgroundColor: 'transparent',
}}
onPress={()=>this.pickImage()}>
<Ionicons
name="ios-photos"
style={{ color: "#fff", fontSize: 40}}
/>
</TouchableOpacity>
</View>
</View>
);
}
}
}
export default CameraPage;