Android QR Code Scanner Tutorial using Zxing Library

Hey guys in this tutorial we will learn creating Android QR Code Scanner. So if you need something like a Barcode or QR Code Scanner in your android application, then you are at the right …

Android QR Code Scanner Tutorial

Hey guys in this tutorial we will learn creating Android QR Code Scanner. So if you need something like a Barcode or QR Code Scanner in your android application, then you are at the right place. So lets start creating our Android QR Code Scanner.

What is a QR Code?

You may already seen QR Code many times. Whatsapp web also uses QR Code for login. QR Code stands for Quick Response Code. QR Code is actually a 2 dimensional bar code. A QR Code consist of black squares over a white background, and it can store some data, which can be read by camera or other imaging devices. Below you can see a sample QR Code.

qr code
Sample QR Code

Generating a QR Code

To scan a QR Code the first thing needed is itself a QR Code.  So for this tutorial we will generate a Simple QR Code consisting of Name and Address.  Below is the QR Code I already generated a free service.

qr code
Sample QR Code

To generate your own QR Code you can go to this link. I have given JSON string as QR Data you can see in the below screenshot.

generating qr code

Creating Android QR Code Scanner

The following project will work for Barcode as well.

Creating a new Project

  • As always create a new Android Studio project.
  • I just created AndroidQRCodeScanner.
android qr code scanner
Android QR Code Scanner

Adding Zxing Library

  • For reading QR Code we will be using Zxing Library.
  • So once your project is loaded go to your app level build.gradle file and add the following dependency.

  • Now sync your project with gradle.

Creating User Interface

  • We have two values (Name, Address) in the QR Code that we generated. So I designed the following layout that will display Name and Address. We also have a button in the layout that will open up camera for scanning the QR Code.
android qr code scanner
Android QR Code Scanner
  • For designing the above layout you can use the following xml code.

Building Android QR Code Scanner

Defining View Objects

  • Now come inside MainActivity.java and first define the view objects.

  • Now we will attach OnClickListener to button.

Attaching OnClickListener

  • Now we will attach OnClickListener to our button.

Android QR Code Scanner

  • Now its time to scan the QR Code.
  • For scanning modify the code of MainActivity.java as below.

  • Now you can try running your application.
android qr code scanner
Android QR Code Scanner
  • Bingo! Our Android QR Code Scanner is working absolutely fine.

So thats all for this Android QR Code Scanner Tutorial friends. Feel free to leave out your comments if having any confusions or queries. Thank You 🙂

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x