Reactjs 如何在单击按钮时打开本机Expo摄像头

Reactjs 如何在单击按钮时打开本机Expo摄像头,reactjs,native,expo,Reactjs,Native,Expo,这是相机的代码。我想要一个打开照相机的按钮。我不希望它在应用程序启动时自动打开。还有没有办法检测图像中的文本并保存 import React from 'react'; import { Text, View, TouchableOpacity } from 'react-native'; import { Camera, Permissions } from 'expo'; export default class CameraExample extends React.Component

这是相机的代码。我想要一个打开照相机的按钮。我不希望它在应用程序启动时自动打开。还有没有办法检测图像中的文本并保存

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

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

  async componentWillMount() {
    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>
            </View>
          </Camera>
        </View>
      );
    }
  }
}
从“React”导入React;
从“react native”导入{Text,View,TouchableOpacity};
从“expo”导入{Camera,Permissions};
导出默认类CameraExample扩展React.Component{
状态={
hasCameraPermission:null,
类型:Camera.Constants.type.back,
};
异步组件willmount(){
const{status}=wait Permissions.askAsync(Permissions.CAMERA);
this.setState({hasCameraPermission:status=='grated'});
}
render(){
const{hasCameraPermission}=this.state;
if(hasCameraPermission===null){
返回;
}else if(hasCameraPermission===false){
不允许进入摄像机;
}否则{
返回(
{
这是我的国家({
类型:this.state.type==Camera.Constants.type.back
?摄像头.常数.类型.前部
:Camera.Constants.Type.back,
});
}}>
{'}翻转{'}
);
}
}
}

您可以将视图放入变量中,然后按按钮进行渲染。在我的相机中,我这样做是为了在拍摄照片时进行图像预览:

if (BUTTON PRESS CONDITION) {
  imageView = (
    <View style={styles.previewContainer}>
      <Image style={styles.imagePreview} source={{uri: `data:image/gif;base64,${base64Post}`}} />

      <View style={styles.container}>
          <TouchableOpacity style={styles.sendButton} onPress={() => Alert.alert("Not implemented!")}>
            <Image style={{width: 70, height: 70}} source={require('../assets/Send.png')}/>
          </TouchableOpacity>                
      </View>

      <View style={styles.container} key={this.state.uniqueValue}>
          <TouchableOpacity style={styles.undoButton} onPress={() => this.setState({ persist64: null})}>
            <Image style={{width: 70, height: 70}} source={require('../assets/undoButton.png')}/>
          </TouchableOpacity>                
      </View>                                          
    </View>
    )           
}    
如果你需要进一步解释,请告诉我


或者,您可以为您的相机创建一个单独的页面,并使用一个按钮导航到相机页面。

您只需连接按钮,然后单击即可导航到相机。这是在react或react native中导航的标准。。因此,只需使用您正在使用的任何导航库,并使用导航道具重定向到camera onPress()

{imageView}