$ npm install rn-qr-generator --save
$ react-native link rn-qr-generator
Linking is not needed anymore. react-native@0.60.0+
supports dependencies auto linking. For iOS you also need additional step to install auto linked Pods (Cocoapods should be installed):
cd ios && pod install && cd ../
Libraries
➜ Add Files to [your project's name]
node_modules
➜ rn-qr-generator
and add RNQrGenerator.xcodeproj
libRNQrGenerator.a
to your project’s Build Phases
➜ Link Binary With Libraries
Cmd+R
)<android/app/src/main/java/[...]/MainActivity.java
import com.gevorg.reactlibrary.RNQrGeneratorPackage;
to the imports at the top of the filenew RNQrGeneratorPackage()
to the list returned by the getPackages()
methodAppend the following lines to android/settings.gradle
:
include ':rn-qr-generator'
project(':rn-qr-generator').projectDir = new File(rootProject.projectDir, '../node_modules/rn-qr-generator/android')
Insert the following lines inside the dependencies block in android/app/build.gradle
:
compile project(':rn-qr-generator')
import RNQRGenerator from 'rn-qr-generator';
RNQRGenerator.generate({
value: 'https://github.com/gevgasparyan/rn-qr-generator', // required
height: 100,
width: 100,
base64: false, // default 'false'
backgroundColor: 'black', // default 'white'
color: 'white', // default 'black'
padding: { // default no padding
top: 20,
left: 20,
bottom: 20,
right: 20,
},
correctionLevel: 'L', // default is 'H', also available 'M' and 'Q'.
})
.then(response => {
const { uri, width, height, base64 } = response;
this.setState({ imageUri: uri });
})
.catch(error => console.log('Cannot create QR code', error));
// Detect QR code in image
RNQRGenerator.detect({
uri: PATH_TO_IMAGE, // local path of the image. Can be skipped if base64 is passed.
base64: imageBase64String, // If uri is passed this option will be skipped.
})
.then(response => {
const { values } = response; // Array of detected QR code values. Empty if nothing found.
})
.catch(error => console.log('Cannot detect QR code in image', error));
Example of 2FA QR code with Time Based (TOTP) or Counter Based (HOTP)
RNQRGenerator.generate({
...
value: 'otpauth://totp/Example:google@google.com?secret=HKSWY3RNEHPK3PXP&issuer=Issuer',
})
More information about totp can be found here.
Some simulators may not generate qr code properly. Use real device if you get an error.
Author: gevgasparyan
Source Code: https://github.com/gevgasparyan/rn-qr-generator
#react-native #react #mobile-apps