How to Read QR Code using Zxing Library in Android?
Last Updated : 17 Oct, 2024
Zxing stands for Zebra Crossing, it is one of the most popular open-source API for integrating QR(Quick Response) Code processing. It is a barcode image processing library implemented in Java, with ports to other languages. It has support for the 1D product, 1D industrial, and 2D barcodes. Google uses ZXing by web search to obtain millions of barcodes on the web indexable. It also creates the foundation of Android’s Barcode Scanner app and is combined into Google Product and Book Search.
Note: To read QR code using CAMView Library you may refer to How to Read QR Code using CAMView Library in Android?
QR Code
It is an abbreviation for Quick Response Code. It is a combination of white and black squares and can be read easily with a QR Scanner. It generally uses four encoding modes
- Numeric
- Alphanumeric
- Byte/Binary
- Kanji
It is used for authentication and online payments. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
In this project, we are creating a basic QR Scanner application which is used to scan a QR Code and display the result over the screen.
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Note: Choose API 24 and onwards as Minimum SDK.
Step 2: Adding dependencies
In order to use the Zxing library in our application we need to add it's dependency in our application's gradle file. For adding the dependency Go to Gradle Scripts > build.gradle(Module: app) and add the following dependencies. After adding the dependency you need to click on Sync Now.
For build.gradle Groovy
dependencies {
implementation 'com.journeyapps:zxing-android-embedded:4.1.0'
}
For build.gradle.kts
dependencies {
implementation("com.journeyapps:zxing-android-embedded:4.1.0")
}
Before moving further let’s add some color attributes in order to enhance the app bar. Go to app > res > values > colors.xml and add the following color attributes.
colors.xml <resources> <color name="colorPrimary">#0F9D58</color> <color name="colorPrimaryDark">#16E37F</color> <color name="colorAccent">#03DAC5</color> </resources>
Step 3: Creating the layout file activity_main.xml
In this step, we will create the layout of our application, which is having a Button for scan and two TextView one is for the message content of QR Code, and the second one is for the format of the scanned message. Go to app > res > layout > activity_main.xml and add the following code snippet.
activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:id="@+id/textContent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textSize="40sp" android:layout_margin="10dp" android:text="messageContent"/> <TextView android:id="@+id/textFormat" android:text="messageFormat" android:layout_width="wrap_content" android:layout_gravity="center" android:layout_margin="10dp" android:textSize="30dp" android:layout_height="wrap_content" /> <Button android:id="@+id/scanBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:textSize="30sp" android:backgroundTint="#0F9D58" android:layout_gravity="center" android:text="Scan"/> </LinearLayout>
Step 4: Working with MainActivity.java file
In this step, we will work with the MainActivity.java file where we first initialize the button and the two TextViews. In onClick() behavior of the button we create the object of IntentIntegrator class which is used to call initiateScan() method for scan process. After that in the onActivityResult() method, we'll check if the scanned message is null then toast a message as "Cancelled" otherwise we'll set the scanned message and its format over the TextViews.
MainActivity.java package com.ishaanbhela.qr_reader; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import com.google.zxing.integration.android.IntentIntegrator; import com.google.zxing.integration.android.IntentResult; // implements onClickListener for the onclick behaviour of button public class MainActivity extends AppCompatActivity { Button scanBtn; TextView messageText, messageFormat; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // referencing and initializing // the button and textviews scanBtn = findViewById(R.id.scanBtn); messageText = findViewById(R.id.textContent); messageFormat = findViewById(R.id.textFormat); // adding listener to the button scanBtn.setOnClickListener(v -> { // we need to create the object // of IntentIntegrator class // which is the class of QR library IntentIntegrator intentIntegrator = new IntentIntegrator(this); intentIntegrator.setPrompt("Scan a barcode or QR Code"); intentIntegrator.setOrientationLocked(true); intentIntegrator.initiateScan(); // Initiate the scan, which opens the camera. // Once the scan is completed or cancelled, onActivityResult will be called. }); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); // if the intentResult is null then // toast a message as "cancelled" if (intentResult != null) { if (intentResult.getContents() == null) { Toast.makeText(getBaseContext(), "Cancelled", Toast.LENGTH_SHORT).show(); } else { // if the intentResult is not null we'll set // the content and format of scan message messageText.setText(intentResult.getContents()); messageFormat.setText(intentResult.getFormatName()); } } else { super.onActivityResult(requestCode, resultCode, data); } } }
Output:
Similar Reads
How to Read QR Code using CAMView Library in Android?
CAMView Library is a simple solution for accessing users' device camera. By using this library we can access users' cameras and use to perform so many functions of the camera such as scanning barcodes that are done by using a built-in ZXing decoding engine. This library contains a set of components
5 min read
Android - JSON Parsing using Retrofit Library with Kotlin
JSON is a format with the help of which we can exchange the data from the server within our application or a website. For accessing this data from the server within android applications. There are several libraries that are available such as Volley and Retrofit. In this article, we will take a look
6 min read
How to Use FoldingCell Library in Android App?
In this article, we are going to implement the FoldingCell feature. This feature can be used to show instructions on a page. Like when we create a quiz app then we can have this feature so that users can view the instruction whenever they wish to. Let's see the implementation of this feature. A samp
2 min read
How to Use Picasso Image Loader Library in Android?
Picasso is open source and one of the widely used image download libraries in Android. It is created and maintained by Square. It is among the powerful image download and caching library for Android. Picasso simplifies the process of loading images from external URLs and displays them on your applic
7 min read
How to Use Universal Image Loader Library in Android?
UIL (Universal Image Loader) is a similar library to that of Picasso and Glide which performs loading images from any web URL into ImageView of Android. This image loading library has been created to provide a powerful, flexible, and customizable solution to load images in Android from Server. This
4 min read
Generate QR Code in Android using Kotlin
Many applications nowadays use QR codes within their application to hide some information. QR codes are seen within many payment applications which are used by the users to scan and make payments. The QR codes which are seen within these applications are generated inside the application itself. In t
4 min read
JSON Parsing in Android using Volley Library
JSON is also known as (JavaScript Object Notation) is a format to exchange the data from the server. The data stored in JSON format is lightweight and easy to handle. With the help of JSON, we can access the data in the form of JsonArray, JsonObject, and JsonStringer. In this article, we will specif
6 min read
How to Add OpenCV library into Android Application using Android Studio?
OpenCV is the huge open-source library for computer vision, machine learning, and image processing and now it plays a major role in real-time operation which is very important in todayâs systems. By using it, one can process images and videos to identify objects, faces, or even the handwriting of a
3 min read
How to Build a Book Library App using Google Books API in Android?
If you are looking for building a book library app and you want to load a huge data of books then for adding this feature, you have to use a simple API which is provided by Google, and of course, it's free. In this article, we will take a look at the implementation of this API in our Android App. Wh
15+ min read
How to Generate QR Code in Android?
QR codes are used in many apps to display data in machine-readable form. These codes are used to represent data in a secure manner that is readable only by machines and not by humans. We have seen many apps that provide QR codes and we can scan those QR codes with our mobile device. In this article,
4 min read