Advertisement
Guest User

Untitled

a guest
Oct 10th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as React from 'react';
  2. import { Text, View, StyleSheet, Button } from 'react-native';
  3. import * as Permissions from 'expo-permissions';
  4. import { BarCodeScanner } from 'expo-barcode-scanner';
  5.  
  6. export default class CaptureBarcode extends React.Component {
  7.  
  8.     constructor(props) {
  9.         super(props)
  10.     }
  11.  
  12.     state = {
  13.         hasCameraPermission: null,
  14.         scanned: false,
  15.     };
  16.  
  17.     async componentDidMount() {
  18.         this.getPermissionsAsync();
  19.     }
  20.  
  21.     getPermissionsAsync = async () => {
  22.         const { status } = await Permissions.askAsync(Permissions.CAMERA);
  23.         this.setState({ hasCameraPermission: status === 'granted' });
  24.     };
  25.  
  26.     render() {
  27.         const { hasCameraPermission, scanned } = this.state;
  28.  
  29.         if (hasCameraPermission === null) {
  30.             return <Text>Requesting for camera permission</Text>;
  31.         }
  32.         if (hasCameraPermission === false) {
  33.             return <Text>No access to camera</Text>;
  34.         }
  35.         return (
  36.             <View
  37.                 style={{
  38.                     flex: 1,
  39.                     flexDirection: 'column',
  40.                     justifyContent: 'flex-end',
  41.                 }}>
  42.                 <BarCodeScanner
  43.                     onBarCodeScanned={scanned ? undefined : this.handleBarCodeScanned}
  44.                     style={StyleSheet.absoluteFillObject}
  45.                 />
  46.  
  47.                 {scanned && (
  48.                     <Button title={'Tap to Scan Again'} onPress={() => this.setState({ scanned: false })} />
  49.                 )}
  50.             </View>
  51.         );
  52.     }
  53.  
  54.     handleBarCodeScanned = ({ type, data }) => {
  55.         this.setState({ scanned: true });
  56.         let message = `Bar code with type ${type} and data ${data} has been scanned!`
  57.         alert(message)
  58.         this.sendData(message)
  59.     };
  60.  
  61.     sendData = (message) => {
  62.         this.props.parentCallback(message);
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement