【发布时间】:2019-11-14 16:17:47
【问题描述】:
我正在尝试为 react-native expo 应用程序拍照,但我无法弄清楚,以下关于堆栈溢出的答案没有帮助:How to snap pictures using expo react native camera?。
我的代码主要来自他们网站 (https://docs.expo.io/versions/latest/sdk/camera/#takepictureasync) 上的博览会演示,除了我添加了一个我想用来拍照的图片按钮。有人可以帮我吗?
我已经尝试过使用上述堆栈溢出帮助,但它不起作用。
import React from 'react';
import { Text, View, TouchableOpacity, Image } from 'react-native';
import * as Permissions from 'expo-permissions';
import { Camera } from 'expo-camera';
export default class CameraExample extends React.Component {
state = {
hasCameraPermission: null,
type: Camera.Constants.Type.back,
};
async componentDidMount() {
const { status } = await Permissions.askAsync(Permissions.CAMERA);
this.setState({ hasCameraPermission: status === 'granted' });
}
render() {
const { hasCameraPermission } = this.state;
if (hasCameraPermission === null) {
return <View />;
} else if (hasCameraPermission === false) {
return <Text>No access to camera</Text>;
} else {
return (
<View style={{ flex: 1 }}>
<Camera style={{ flex: 1 }} type={this.state.type}>
<View
style={{
flex: 1,
backgroundColor: 'transparent',
flexDirection: 'row',
}}>
<TouchableOpacity
style={{
flex: 0.1,
alignSelf: 'flex-end',
alignItems: 'center',
}}
onPress={() => {
this.setState({
type:
this.state.type === Camera.Constants.Type.back
? Camera.Constants.Type.front
: Camera.Constants.Type.back,
});
}}>
<Text style={{ fontSize: 18, marginBottom: 10, color: 'white' }}> Flip </Text>
</TouchableOpacity>
<TouchableOpacity>
<Image source={require("./images/camera.jpeg")}
style={{width: 100,
height: 100}} /> /* this is my button for taking the picture*/
</TouchableOpacity>
</View>
</Camera>
</View>
);
}
}
}
我现在只想拍张照片并显示到控制台。
【问题讨论】:
标签: react-native expo