-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
57 lines (52 loc) · 1.73 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import React from 'react';
import { StyleSheet, Text, Button, View,ToastAndroid } from 'react-native';
import * as Permissions from 'expo-permissions';
import { BarCodeScanner } from 'expo-barcode-scanner';
export default class App extends React.Component {
state = {
hasCameraPermission: false,
scanned: true
}
getPermissions = () => {
Permissions.askAsync(Permissions.CAMERA).then(permission => {
console.log('permission: ', permission);
this.setState({ hasCameraPermission: permission.status === 'granted' });
}).catch(err=>{
console.log('failed to get camera permission: ', err);
ToastAndroid.show('failed to get permission',ToastAndroid.SHORT);
});
}
handleBarCodeScanned = ({ type, data }) => {
this.setState({ scanned: true });
alert(`Bar code with type ${type} and data ${data} has been scanned!`)
}
render() {
const { hasCameraPermission, scanned } = this.state;
if (hasCameraPermission === false) {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>No access to camera</Text>
<Button title={'request camera permission'}
onPress={() => this.getPermissions()} />
</View>
)
}
return (
<View style={styles.container}>
<BarCodeScanner
onBarCodeScanned={scanned ? undefined : this.handleBarCodeScanned}
style={StyleSheet.absoluteFillObject} />
{scanned && (<Button title={'Tap to scan'}
onPress={() => this.setState({ scanned: false })} />)}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'flex-end',
},
});