想要使用摄影机捕捉图像

时间:2018-11-09 10:23:09

标签: react-native

我正在尝试从博览会相机捕获图像,但是当我提醒我的照片时,这里没有显示完整的组件代码

import React from 'react';
 import { Text, View, TouchableOpacity } from 'react-native';
 import { Camera, Permissions } from 'expo';
 import {
Container,

Title,
Content,
Header,
Button,
Switch,
Left,
Body,
Right,

List,ListItem,Thumbnail,Footer,FooterTab
 } from "native-base";
  import {Icon} from 'react-native-elements';

 export default class CameraEx extends React.Component {
   static navigationOptions = {
    header: null
}
  state = {
   hasCameraPermission: null,
    type: Camera.Constants.Type.back,
  };

  async componentDidMount() {
   const { status } = await Permissions.askAsync(Permissions.CAMERA);
this.setState({ hasCameraPermission: status === 'granted' });
  }
  takePicture = async function() {
  if (this.camera) {
    let photo = await this.camera.takePictureAsync();
  alert(photo);

  }

   }

    render() {
const { hasCameraPermission } = this.state;
if (hasCameraPermission === null) {
  return <View />;
} else if (hasCameraPermission === false) {
  return <Text>No access to camera</Text>;
} else {
  return (

    <Container style={{ flex: 1 }}>
     <Header transparent>
      <Left>
      <Button transparent onPress={() =>this.props.navigation.navigate('Home') }>
       <Icon name='arrow-back' />
      </Button>
      </Left>

    </Header>
      <Camera style={{ flex: 1 }} type={this.state.type}>
        <View
          style={{
            flex: 1,
            backgroundColor: 'transparent',
            flexDirection: 'row',
            justifyContent:'space-between',
            width:"50%"
          }}>
          <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
            style={{
             // flex: 0.1,
              alignSelf: 'flex-end',
              alignItems: 'center',
            }}
            onPress={()=>this.takePicture()}
           >
            <Text
              style={{ fontSize: 18, marginBottom: 10, color: 'white' }}>
              {' '}Take Picture{' '}
            </Text>
          </TouchableOpacity>
        </View>
      </Camera>
    </Container>
  );
}
   }
   }

这是我组件的完整代码,请让我知道我如何捕获图像并将其存储为新手状态....................... ................................................... ................................................... .................. ....

1 个答案:

答案 0 :(得分:0)

您无法在警报框中显示图片。您需要传递图片的source或base64来响应本地Image组件。

从“ expo”导入{ImagePicker};

const options = {
    base64: true,
    allowsEditing: true
  };
  const data = await ImagePicker.launchCameraAsync(options);

  if (data.cancelled !== true) {
    this.setState({imageBase64: data.base64});
  }

然后您可以像这样使用:

<Image source={`data:image/png;base64, ${this.state.imageBase64}`} />