Our aim for this article is to understand the usage of BarCodeScanner component by Expo, by developing a simple Scanner app.
Before moving onto the development part, we need to initialize the project and install the dependencies. So, open up the terminal and enter the following commands:
expo init // Choose the Blank template when prompted
Move into the project folder and then run the following command:
expo install expo-barcode-scanner
Now that we have installed the scanner component, it’s time to write the code. Let us start by importing the required components, code for which is shown below:
Now it’s time to get inside the App class and carry out the development process step by step
Requesting the permission to access the camera:
In this step we are going to prompt user, for the permission to access camera. Status of this request is stored in the state of the component, code for which is shown below:
Now, inside the render function we are going to return different views depending upon the status of our request as show below:
After the user has given us the permission to access camera, we will return the BarCodeScanner component which is demonstrated in the next part.
Scan the code.
Got the permission, it’s time to move on to the BarCodeScanner Component.
It’s time to define the barCodeScanned function to access the response data which is demonstrated in the next step.
Access the response data.
We are going to alert the user with the response data as an example.
We have successfully completed the development process of this simple BarCodeScanner application.
Github Repo Link: https://github.com/notnotparas/expo-BarCodeScanner
API Reference: https://docs.expo.io/versions/latest/sdk/bar-code-scanner/