How to Open React Native Expo Camera On Button Click












1















Here is the code for the camera. I want a button to open the camera. I don't want it to automatically open on app startup. Also is there any way to detect text in the image and save it?



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>
);
}
}
}









share|improve this question





























    1















    Here is the code for the camera. I want a button to open the camera. I don't want it to automatically open on app startup. Also is there any way to detect text in the image and save it?



    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>
    );
    }
    }
    }









    share|improve this question



























      1












      1








      1








      Here is the code for the camera. I want a button to open the camera. I don't want it to automatically open on app startup. Also is there any way to detect text in the image and save it?



      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>
      );
      }
      }
      }









      share|improve this question
















      Here is the code for the camera. I want a button to open the camera. I don't want it to automatically open on app startup. Also is there any way to detect text in the image and save it?



      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>
      );
      }
      }
      }






      reactjs native expo






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 1 '18 at 7:16









      Cœur

      18.6k9110150




      18.6k9110150










      asked Jun 7 '18 at 22:42









      borderalborderal

      212




      212
























          2 Answers
          2






          active

          oldest

          votes


















          1














          You can put your view in a variable and render it on button press. In my camera I've done this for an image preview when the photo is taken:



          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>
          )
          }


          You could set up some boolean that when you press the button it is true renders the view of your camera. Then take the name of the view you made and put it somewhere in your render. like so:



          {imageView}


          If you need further explanation let me know.



          Alternatively you could just create a separate page for your camera and have a button that navigates to the camera page.






          share|improve this answer

































            0














            You can simply connect the button and navigate to the camera on click. It is the very standard of navigating in both react or react native.. So just use whatever navigation library youre using, and use the nav props to redirect to camera onPress().






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50751104%2fhow-to-open-react-native-expo-camera-on-button-click%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              You can put your view in a variable and render it on button press. In my camera I've done this for an image preview when the photo is taken:



              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>
              )
              }


              You could set up some boolean that when you press the button it is true renders the view of your camera. Then take the name of the view you made and put it somewhere in your render. like so:



              {imageView}


              If you need further explanation let me know.



              Alternatively you could just create a separate page for your camera and have a button that navigates to the camera page.






              share|improve this answer






























                1














                You can put your view in a variable and render it on button press. In my camera I've done this for an image preview when the photo is taken:



                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>
                )
                }


                You could set up some boolean that when you press the button it is true renders the view of your camera. Then take the name of the view you made and put it somewhere in your render. like so:



                {imageView}


                If you need further explanation let me know.



                Alternatively you could just create a separate page for your camera and have a button that navigates to the camera page.






                share|improve this answer




























                  1












                  1








                  1







                  You can put your view in a variable and render it on button press. In my camera I've done this for an image preview when the photo is taken:



                  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>
                  )
                  }


                  You could set up some boolean that when you press the button it is true renders the view of your camera. Then take the name of the view you made and put it somewhere in your render. like so:



                  {imageView}


                  If you need further explanation let me know.



                  Alternatively you could just create a separate page for your camera and have a button that navigates to the camera page.






                  share|improve this answer















                  You can put your view in a variable and render it on button press. In my camera I've done this for an image preview when the photo is taken:



                  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>
                  )
                  }


                  You could set up some boolean that when you press the button it is true renders the view of your camera. Then take the name of the view you made and put it somewhere in your render. like so:



                  {imageView}


                  If you need further explanation let me know.



                  Alternatively you could just create a separate page for your camera and have a button that navigates to the camera page.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Oct 23 '18 at 11:34

























                  answered Oct 19 '18 at 15:13









                  DirtyDaverDirtyDaver

                  436




                  436

























                      0














                      You can simply connect the button and navigate to the camera on click. It is the very standard of navigating in both react or react native.. So just use whatever navigation library youre using, and use the nav props to redirect to camera onPress().






                      share|improve this answer




























                        0














                        You can simply connect the button and navigate to the camera on click. It is the very standard of navigating in both react or react native.. So just use whatever navigation library youre using, and use the nav props to redirect to camera onPress().






                        share|improve this answer


























                          0












                          0








                          0







                          You can simply connect the button and navigate to the camera on click. It is the very standard of navigating in both react or react native.. So just use whatever navigation library youre using, and use the nav props to redirect to camera onPress().






                          share|improve this answer













                          You can simply connect the button and navigate to the camera on click. It is the very standard of navigating in both react or react native.. So just use whatever navigation library youre using, and use the nav props to redirect to camera onPress().







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 25 '18 at 20:33









                          Yashank VarshneyYashank Varshney

                          349




                          349






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50751104%2fhow-to-open-react-native-expo-camera-on-button-click%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Futebolista

                              Feedback on college project

                              Albești (Vaslui)