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 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 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 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 🙂

47 thoughts on “Android QR Code Scanner Tutorial using Zxing Library”

  1. Great tutorial! Can you kindly show me how to open the qr code within a fragment and with the camera not at full screen but only the middle part of the fragment is the camera

  2. sir please make the scanner view with square shape and delete the red horizontal line in between the scanner view.

    I need this requirement sir.please post it…

  3. awesome man!!!
    you ROCK <3. its working fine. just i wanted to send this result data in an edittext instead of showing result in a toast.

    anyone!!! plz help me in this.

  4. what if I wanted to retrieve data from SQL database, the QR code contains an ID number that is unique in the database and I want to retrieve that specific record by scanning the ID .. can you help me, please?

  5. Hi Sir,
    I am Getting Error
    Error:Execution failed for task ‘:app:transformClassesWithDexForDebug’.
    > org.gradle.process.internal.ExecException: Process ‘command ‘C:\Program Files\Java\jdk1.7.0_79\bin\java.exe” finished with non-zero exit value 1

  6. i want to fetch textview scan result and match with my localhost data how i can do this kindly give me code i have no idea

  7. I am having an error on this part of the code:


    the ‘integration’ part. Can someone please help me? Pleeeaasseee T.T

  8. hi i am invoke the coding is working perfectly so next how to open flash light for barcoding scanner in adroid please help me its very urgent guyes

  9. How can i change the orientation to portrait mode?
    what code to paste?
    ScreenOrientation= “Portrait”
    is not working in manifest file

  10. this work good.

    but when i scan other QR then always print in TOAST so any other way to scan all QR and get data in TextView ?

  11. Hi…the code is running perfectly fine when on the main activity, I was wondering how will I be able to call this activity from another activity on button click. I have tried using Intent but it does not seem to work and the app crashes, do you know why this is happening and how to solve it?

  12. hello. i love your work. it works on me. thumbs up. i was wondering if the qr code scanning can fetch data from mysql . instead of displaying data, i want to scan the qr code and when it (QR CODE = QR CODE) something like this. it display all the data from mysql (SELECT * FROM data WHERE QRCODE = QRCODE – php file) when scanned. and it will display in listview . thanks. i hope you read this. and i hope you will help me. thank you very much. its for thesis thing. we are making an mobile pos system using qr code scanning. thank you very much

  13. what if I wanted to retrieve data from SQL database, the QR code contains an ID number that is unique in the database and I want to retrieve that specific record by scanning the ID .. can you help me, please?

  14. Hey, I am using this code but it is taking near about 20 secs to scan Bar code, can you please help me to read bar code fast?

  15. i have two button to get qrcode but problem is how to identify the requestCode.
    and how i can pass the request code

  16. This will work for scanning a single QRCode and this is working well. Do You have a code for continuous scanning of QRCodes.
    Thank you.


Leave a Comment