Home > Mobile >  How to scan a qr code in React Native cli?
How to scan a qr code in React Native cli?

Time:12-15

Is there anyone who has a tutorial or code how to read a qr code in React Native cli that actually works in the current React Native version? I tried so many tutorials and docs and nothing works. It would be nice if it works in tsx.

CodePudding user response:

Use the react-native-qrcode-scanner package in React Native to scan QR codes. Here is an illustration of how you may employ it:

import QRCodeScanner from 'react-native-qrcode-scanner';

const MyQRCodeScanner = () => {
  const onSuccess = (e) => {
    console.log(e.data);
    // e.data contains the QR code data
  };

  return (
    <QRCodeScanner onRead={onSuccess} />
  );
};

CodePudding user response:

you can use two libraries for QR Code scanner then add all the permissions

react-native-qrcode-scanner 
react-native-camera

Code :

const [barcode, setBarcode] = useState(null);


<View style={styles.screen}>
        <View style={styles.caption}>
          <Text style={styles.captionTitleText}>QR Code</Text>
        </View>

        {barcode ? (
          <View style={{alignContent: 'center', alignItems: 'center'}}>
            <TouchableOpacity
              style={{
                backgroundColor: 'navy',
                borderRadius: 10,
                paddingHorizontal: 15,
              }}
              onPress={() =>
                navigation.navigate('Your next screen')
              }>
              <Text style={styles.rmCameraResultText2}>
                Scan Successfully. Tap to fill the Audit.
              </Text>
            </TouchableOpacity>
          </View>
        ) : (
          <RNCamera style={styles.rnCamera} onBarCodeRead={setBarcode} />
        )}

        <View style={styles.cameraControl}>
          <TouchableOpacity style={styles.btn} onPress={() => setBarcode(null)}>
            <Text style={styles.btnText}>New QR Scan</Text>
          </TouchableOpacity>
        </View>
      </View>


const styles = StyleSheet.create({
  screen: {
    flex: 1,
    backgroundColor: '#F2F2FC',
  },
  saveArea: {
    backgroundColor: '#62d1bc',
  },
  topBar: {
    height: 50,
    backgroundColor: '#62d1bc',
    alignItems: 'center',
    justifyContent: 'center',
  },
  topBarTitleText: {
    color: '#ffffff',
    fontSize: 20,
  },
  caption: {
    height: 120,
    justifyContent: 'center',
    alignItems: 'center',
  },
  captionTitleText: {
    color: '#121B0D',
    fontSize: 16,
    fontWeight: '600',
  },
  btn: {
    width: 240,
    borderRadius: 4,
    backgroundColor: '#326A81',
    paddingHorizontal: 20,
    paddingVertical: 10,
    marginVertical: 8,
  },
  btnText: {
    fontSize: 18,
    color: '#ffffff',
    textAlign: 'center',
  },
  rnCamera: {
    flex: 1,
    width: '94%',
    alignSelf: 'center',
  },
  rmCameraResult: {
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#eeeeee',
  },

  rmCameraResultText: {
    fontSize: 15,
    color: '#326A81',
  },
  rmCameraResultText2: {
    fontSize: 20,
    color: 'white',
  },
  cameraControl: {
    height: 180,
    justifyContent: 'center',
    alignItems: 'center',
  },
  • Related