Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I shut of camera/scanner in react-native-qrcode-scanner?

Tags:

Current code:

import QRCodeScanner from 'react-native-qrcode-scanner';
function ScanScreen({ navigation }) {
  return (
    <SafeAreaView style={styles.screen}>
      <QRCodeScanner reactivate={true} reactivateTimeout={3000}
        onRead={data => navigation.navigate('Third', {target:data.data})}
      />
    </SafeAreaView>
  );
}

It works, but here's what I want to do: The user can navigate between screens, one of them being a QR code scanner. While scanning, I need to debounce the scanner so it doesn't keep generating onRead events. The user could a) start a scan in the scan screen without reading a QR code and navigate manually to another screen. b) read a QR and automatically move to another screen for processing, then go back to scan again. For this reason I need to re-enable the scanner after some reasonable time. I can't just set reactivate to false because then the QR scanner is inactive until I restart the app. The problem is that when the user stays in Another screen, the QR scanner re-activates after the timeout and tries to scan when it is not desired. What I ideally would like to do is to deactivate the QR scanner while the user is not in the scan screen, and re-activate it with the above mentioned parameters whenever the user enters the scan screen. Is there any way to do this? Thanks!

like image 273
Knut Avatar asked Feb 20 '20 10:02

Knut


People also ask

How do I turn on my QR scanner on my camera?

Go to Settings. Select System Apps. Tap on Camera. Then toggle to enable Scan QR codes.

How do I remove the QR code from my camera?

Open the Settings app and scroll down to the Camera app's preference and tap it. On the Camera screen, turn the 'Scan QR codes' switch off.

Can you scan QR code without camera?

Yes. Just like iPhones, Android 9 (Android Pie) and Android 10 have an in-built QR Code reader. Even the Android 8 or Oreo does not need an app to scan QR Codes.


2 Answers

I had almost the same problem. The scanner does not stop scanning while showing another View (using reactivate={true}). I am using react-navigation and so I came up to following solution.

You can listen on what is happening with your view with focus and blur.

this.props.navigation.addListener('focus', () => {
    this.setState({viewFocused: true});
});

this.props.navigation.addListener('blur', () => {
    this.setState({viewFocused: false});
});

Note: you place this piece of code in componentDidMount or using React.useEffect.

Based on the state viewFocused you can render the QR Code Scanner.

this.state.viewFocused && (
    <QRCodeScanner
        onRead={onRead}
        reactivate={true}
        reactivateTimeout={2000}
    /> );

This helps me to solve my problem. To not scan while showing other views but to scan if the view is shown again. Credits to pkyeck on github.com

like image 169
K.E. Avatar answered Sep 29 '22 15:09

K.E.


With React hooks:

let scanner = useRef(null);

<QRCodeScanner
onRead={onSuccess}
ref={node => { scanner = node;}}
/>

Then you can attach it to a button. For example:

onPress={() => scanner.reactivate()}
like image 33
osama aburabie Avatar answered Sep 29 '22 15:09

osama aburabie