React Native Scan QR Code

React Native Scan QR Code

This is an Example of React Native Scan QR Code. To make a QR Code Scanner in our React Native, Wix has provided a very good library named react-native-camera-kit. Which is very easy to integrate into your app.

If you are making an app which needs to share small code/data/URL between users in physical presence then the QR Code is the cool thing to integrate into your App. For this process, we have to implement a QR Code Scanner and QR Code Generators in our app. In this example, you will make the QR Code Scanner Using React Native Camera.

In this example, we will make a home screen with a button to open A QR code scanner. On click of a button, we will open a Camera with QR Scan functionality and after scanning of the QR code, we will take the code back to the home screen. If the Scanned string is a link/URL then we will show an additional button to open the URL in default browser using React Native Linking Component. So let’s get started.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run
npm install -g react-native-cli
Run the following commands to create a new React Native project
react-native init ProjectName
This will make a project structure with an index file named App.js in your project directory.

Installation of Dependency

To use <CameraKitCameraScreen /> we need to install react-native-camera-kit package. To install this

Open the terminal and jump into your project

Run the following command

This command will copy all the dependencies into your node_module directory, You can find the directory in node_module the directory named react-native-camera-kit.

–save is optional, it is just to update the react-native-camera-kit dependency in your package.json file.

Linking of Dependency

After installing the dependencies you need to link it with your project using

After linking the dependency if you face  Could not Compile Settings File project/android/settings.gradle error while building and installing the project then you can find the solution here.

Dependency Version for this Example Used is

Permission to use the Camera for Android

We are using a Native API Camera to scan the QR code so we need to add permission to the AndroidManifest.xml file to access it. So we are going to add the following permissions in the AndroidMnifest.xml

PermissionPurpose
CAMERATo access the camera


On devices before SDK version 23, the permissions are automatically granted if they appear in the manifest but after SDK version 23 we have to ask run time permission also, so we will ask for permission in code also. For more about the permission, you can see this post.

Permission to use the Camera for IOS

1. Open the project CameraExample>ios>CameraExample.xcodeproj in XCode. Click on Project (CameraExample in my case) from the left sidebar and you will see multiple options in the workspace.

2. Select info tab which is info.plist

3. Click on the plus button to add a permission key “Privacy-Camera Usage Description” and a value which will be visible when permission dialog pops up.

Code to Make a QR Code Scanner

Open App.js in any code editor and replace the code with the following code.

App.js

We have used this website to generate the QR Code.

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

Android

      
      

IOS

   

That was the React Native Scan QR Code. If you have any doubt or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

How useful was this post?

Click on a star to rate us!

Average rating / 5. Vote count:

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

2 thoughts on “React Native Scan QR Code”

  1. Great tutorial, can confirm it works. Just have one problem when flipping the camera side ways it gets distorted. Is there a fix for this? Cheers

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

About React will use the information you provide on this form to be in touch with you and to provide updates and marketing.