Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import * as React from 'react';
- import { Text, View, StyleSheet, Button } from 'react-native';
- import * as Permissions from 'expo-permissions';
- import { BarCodeScanner } from 'expo-barcode-scanner';
- export default class CaptureBarcode extends React.Component {
- constructor(props) {
- super(props)
- }
- state = {
- hasCameraPermission: null,
- scanned: false,
- };
- async componentDidMount() {
- this.getPermissionsAsync();
- }
- getPermissionsAsync = async () => {
- const { status } = await Permissions.askAsync(Permissions.CAMERA);
- this.setState({ hasCameraPermission: status === 'granted' });
- };
- render() {
- const { hasCameraPermission, scanned } = this.state;
- if (hasCameraPermission === null) {
- return <Text>Requesting for camera permission</Text>;
- }
- if (hasCameraPermission === false) {
- return <Text>No access to camera</Text>;
- }
- return (
- <View
- style={{
- flex: 1,
- flexDirection: 'column',
- justifyContent: 'flex-end',
- }}>
- <BarCodeScanner
- onBarCodeScanned={scanned ? undefined : this.handleBarCodeScanned}
- style={StyleSheet.absoluteFillObject}
- />
- {scanned && (
- <Button title={'Tap to Scan Again'} onPress={() => this.setState({ scanned: false })} />
- )}
- </View>
- );
- }
- handleBarCodeScanned = ({ type, data }) => {
- this.setState({ scanned: true });
- let message = `Bar code with type ${type} and data ${data} has been scanned!`
- alert(message)
- this.sendData(message)
- };
- sendData = (message) => {
- this.props.parentCallback(message);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement