Scan Barcode And QRcode Using ZXing Android Studio Programmatically

Hello, developers. In scan barcode and QRcode using zxing Android example, we will learn how to scan the barcode and read QRcode using a zxing library in Android studio.

Barcode or QRCode contains an unique id which is associated with any particular product.

This scanning feature will read that unique id from barcode logo on product box or from the computer screen.

You can develop barcode scanning feature using below methods

  • Using a web API. Means that you upload the barcode to server first where web based API will scan it and after that it will return the result to the android device.
  • You develop or write your own image scanning algorithm which is really complex.
  • You can use google’s mobile vision API.
  • Using Github third party library and integrate it in your android studio project.

In this tutorial example, I am going to use last option: Github third party library.

We will use the camera of the android device to scan the barcode.

First of all check output of this scan barcode and QRcode using zxing android tutorial then we will implement it programmatically.

Creating scan barcode and qrcode using zxing android Example

Step 1: Create a new project in Android Studio.

Step 2: Updating build.gradle(Module:app) file

add following code into dependencies{} 

Here we are using ZXing library. You can also use ZBar library.

Follow this tutorial for ZBar: Scan Barcode QRcode Android

Github link for ZBar or ZXing library: ZXing or ZBar

Step 3: Updating AndroidManifest.xml file

 add camera permission between <manifest>….</manifest> tag.

Note: If you are targeting SDK version above 22 (Above Lollipop)  then you need to ask a user for granting runtime permissions. Check marshmallow runtime permission for more information.

Step 4: Updating activity_main.xml file:

Copy following code into android_main.xml file:

I have taken one button and one textview in the main layout file.

When the user clicks on the button, system will open a Scan activity.

Unique id from the barcode will appear in the textview after successful scanning.

Step 5: Updating MainActivity.java file:

Update MainActivity.java as per below code:

Here we will open ScanActivity on the button’s click.

We will set up the logic for the scanning task in ScanActivity.

Step 6: Opening new activity in android studio

To open new activity in the android studio, click on File tab which is present at the left top bar.

Now go to File->New->Activity->Empty Activity and place name of activity as ScanActivity.

zxing scan barcode qrcode scanner android

new activity

Step 7: Updating ScanActivity.java file:

Add below code to ScanActivity.java

Note: We do not need to update activity_scan.xml file because we will open camera preview here so no need to do anything in the layout file.

Step 8: Brief Description of ScanActivity.java

  • In onCreate() method, we have set the view as scannerview instead of xml file layout.
  • In onResume() method, we have set the resulthandler and started camera preview.
  • In onPause() method, we are stopping camera preview.

Step 9: Explaining handleResult() method

  • As you can see in above code, we get result object which contains resulting Barcode or QRcode after scanning is completed.
  • We will get the final result by running result.getText() method and we have set it into TextView of MainActivity.

Kotlin Version

Kotlin version of this tutorial : Android Kotlin Scan Barcode QRCode ZXing

We will use same library but will integrate it with kotlin.

So it was all about scan barcode and qrcode using zxing android example programmatically.

Use comment section if you find any difficulty while developing this example. You can also share your reviews in the comment section.

Thank you.

Download Source Code

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

25 Responses

  1. Andrew says:

    Easy scanning. Thanks!

  2. Wai Lay says:

    I cann’t start scan view in DrawerActivity .Please help me.

  3. WQ Yew says:

    Hi, I am unable to start my scanActivity.

    • Hardik Parsania says:

      Is your app crashing?

      • WQ Yew says:

        I realised that my MinSDKTarget was too high which caused my app to crash. So i have fixed that problem. On the other hand, is the scanner ‘scan’ time slow? I tried scanning some sample qr codes. for some codes, the scanner took a short time to decode, while for others, it took a long time, some even went for more than a minute long! Please help. Thanks

  4. WQ Yew says:

    Hi! I am unable to connect to my camera to start the ScanActivity java class. I have already added the camera permission in the manifest. Please help me. Thanks.

  5. Sary says:

    Hello I am news Android learner and I want to know How to open webview by this scan?

  6. Alex says:

    Hi when I press the scan button it only opens the activity_scan.xml file and doesn’t start the camera. I have done the steps exactly as shown. How can I get the button to start the camera?

  7. pranish says:

    Great tutorial. But what I have found is the qr scanned works a bit slow compared to zxing’s app. Whats the reason?

  8. meowww says:

    So Great ~~~ It is work very very well ~~ Thank you so much

  9. Muhammad Ayaz says:

    thanks it work perfect but i want the url text in the scan activity not on the main activity. how can i do that ?thank IA

  10. Mathews Augusto says:

    I’m Brazilian, I do not speak much English, but thank you for posting, thank you very much, that helps me a lot.

  11. Walter Suarez says:

    Hello, at my app the camera is working fine, but i have two button open the camera, this two button working fine too, but when use 1 button erase de info capture in button 2 and viceversa

  12. Muhammad Umair says:

    scanning. Thanks

  13. Burak Çaparlar says:

    do we have a chance to open the camera in a layout what do you think about this stuation ?

  14. Balaji Dharani says:

    Dear Hardik,
    Can you please help me as
    I am receiving the following error when i build the gradle.

    Gradle sync failed: Could not find method compile() for arguments [me.dm7.barcodescanner:zxing:1.9] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
    Consult IDE log for more details
    Thanks ,
    Balaji Dharani

Leave a Reply

Your email address will not be published. Required fields are marked *