How to Open React Native Expo Camera On Button Click
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
add a comment |
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
add a comment |
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
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
reactjs native expo
edited Dec 1 '18 at 7:16
Cœur
18.6k9110150
18.6k9110150
asked Jun 7 '18 at 22:42
borderalborderal
212
212
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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().
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
edited Oct 23 '18 at 11:34
answered Oct 19 '18 at 15:13
DirtyDaverDirtyDaver
436
436
add a comment |
add a comment |
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().
add a comment |
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().
add a comment |
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().
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().
answered Nov 25 '18 at 20:33
Yashank VarshneyYashank Varshney
349
349
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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