到目前为止,这是我所做的。在这里,我可以打开相机电源,也可以使用ImagePicker访问图像。但是在单击图像时,我得到了类似保存图像的位置的信息,而且也无法正确选择图像。 当我单击相机上的图片时,会在控制台中得到它:
"Object {
"height": 4000,
"uri": "file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540himanshu717171%252FMyProject/Camera/373e905c-2ef8-4f0f-b4a4-78ac8a64732c.jpg",
"width": 3000,
}"
从电话中选择任何图像时,我会在控制台中收到以下响应:
"Object {
"cancelled": false,
"height": 721,
"type": "image",
"uri": "file:/data/user/0/host.exp.exponent/cache/ExperienceData/%2540himanshu717171%252FMyProject/ImagePicker/937bed14-dc51-4786-8836-09965b17d327.jpg",
"width": 1280,
}"
这是我的代码。请建议我任何需要的更改。
import React from 'react';
import { StyleSheet, Text, View ,TouchableOpacity,Platform, } from 'react-native';
import { Camera } from 'expo-camera';
import * as Permissions from 'expo-permissions';
import { FontAwesome, Ionicons,MaterialCommunityIcons } from '@expo/vector-icons';
import * as ImagePicker from 'expo-image-picker';
export default class App extends React.Component {
state = {
hasPermission: null,
cameraType: Camera.Constants.Type.back,
}
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' });
}
handleCameraType=()=>{
const { cameraType } = this.state
this.setState({cameraType:
cameraType === Camera.Constants.Type.back
? Camera.Constants.Type.front
: Camera.Constants.Type.back
})
}
takePicture = async () => {
if (this.camera) {
let photo = await this.camera.takePictureAsync();
console.log(photo);
}
}
pickImage = async () => {
let result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.Images
});
console.log(result);
}
render(){
const { hasPermission } = 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: 1 }} type={this.state.cameraType} ref={ref => {this.camera = ref}}>
<View style={{flex:1, flexDirection:"row",justifyContent:"space-between",margin:30}}>
<TouchableOpacity
style={{
alignSelf: 'flex-end',
alignItems: 'center',
backgroundColor: 'transparent'
}}
onPress={()=>this.pickImage()}>
<Ionicons
name="ios-photos"
style={{ color: "#fff", fontSize: 40}}
/>
</TouchableOpacity>
<TouchableOpacity
style={{
alignSelf: 'flex-end',
alignItems: 'center',
backgroundColor: 'transparent',
}}
onPress={()=>this.takePicture()}
>
<FontAwesome
name="camera"
style={{ color: "#fff", fontSize: 40}}
/>
</TouchableOpacity>
<TouchableOpacity
style={{
alignSelf: 'flex-end',
alignItems: 'center',
backgroundColor: 'transparent',
}}
onPress={()=>this.handleCameraType()}
>
<MaterialCommunityIcons
name="camera-switch"
style={{ color: "#fff", fontSize: 40}}
/>
</TouchableOpacity>
</View>
</Camera>
</View>
);
}
}
}
App.navigationOptions = {
title: 'Camera',
headerStyle: {
backgroundColor: '#ff6666',
},
headerTintColor: '#fff',
headerTitleStyle: {
fontWeight: 'bold',
fontSize:20
},
};