Scan Barcode And QRcode Programmatically In Android Studio

In scan barcode and QRcode programmatically in android tutorial, we will implement scan functionality in our android app programmatically.

Scan feature is needed in the app regarding grocery stores and inventory android apps.

Every barcode contains unique format which represents the product id. Using this app, you will be able to find out that id from the barcode by scanning it.

Scanning barcode programmatically with android device is always a topic of discussion among android developers.

There are certain ways using which you can accomplish your goal.

  • Write your own algorithm to scan the image based barcodes.
  • Using google’s mobile vision API.
  • Web based Solution, where you upload the barcode on the server, Web API scan it and returns the results in the text format.
  • Use third party library which will use android devices camera to scan the barcode

We will use fourth option in this tutorial. Using google’s api and third party library are almost same. You need to import classes and use them in your app.

In the end, our Android app will be able to scan both Barcode as well as QRCode.

Check out what will be final result in the following video output.

Creating scan barcode and QRcode programmatically in android example

Step 1: Create a new project in Android Studio.

When you are making new activity, select empty activity as a default activity.

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

Add following code into dependencies{} 

Here we are using ZBar, but you can also use ZXing.

For using ZXing library follow this : zxing barcode qrcode scanner android

Github link for ZBar or ZXing library : ZXing or ZBar

This line will integrate required classes from the github.

Step 3: Updating AndroidManifest.xml file

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

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

For using camera, we have to get permission from the user.

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.

When the user will click the button, camera preview will be opened and you can scan barcode or QRCode from here.

After successful scanning, system will enter the value of the barcode in the textview.

Step 5: Updating MainActivity.java file:

Update MainActivity.java as per below code:

Here we will open ScanActivity on the button click.

Step 6: Opening new activity in android studio

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

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

You can use below image for more reference.

scan barcode qrcode android

open 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 layout file.

Step 8: Brief Description of ScanActivity.java

  • In onCreate() method, we have set 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.getContents() method. This method returns the value of barcode in text format.
  • we have set this barcode text into TextView of MainActivity.
  • onBackPressed() method will return the user to the Main Activity.

So it was all about scan barcode and QRcode programmatically in android studio example. 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...

20 Responses

  1. cesar edinson says:

    Many thanks … excellent posting … and they work correctly .. Greetings from Peru

  2. enrique hernandez says:

    Thanks a lot Hardik, i had many troubles with other tutorial.
    Your tutorial is short but works fine.
    Greetings from México

  3. Arindu says:

    Thanks for this code.Great work.But how to automatically provide the permission for camera ?

  4. Nehal says:

    Hello sir

    when i run this application in real device….My application is getting stopped unfortunately.

  5. Waqas AHmed says:

    Hello sir i have developed a qr code app but now i want to add buttons for call , save contact and open url when they are detected but i don’t know how can i do this can you help me please

  6. ajithbharathi says:

    my does not open

    • Hardik Parsania says:

      First download source code and run it in your android studio. Tell whether downloaded source code works perfect or not?

  7. ajithbharathi says:

    my camera does not open. my scan page displays white screen

  8. Cindie says:

    How can I then take the textView data gotten from the QR code and save it in a new activity?

    • Hardik Parsania says:

      result.getContents()
      Above method gives you QRcode data in string format. Save this data in string variable and then use this variable to in activity you like!

  9. Mike says:

    This is a great example, well illustrated and easy to follow.

    When I run this example, it works and finds bar codes.
    However the preview (or live camera feed) is not appearing – only a white screen. Everything else works – the can works.

    Can you please tell me why the camera feed is only a white screen?

  10. Mike says:

    From additional reading, I believe this is because my SDK is 27. At SDK 25 Camera API 2 was implemented. Due to other programming needs, I can not use SDK 24. How can I force my app to use camera API 1 and use your program?

    thank you

    • Hardik Parsania says:

      I suggest you to stick with latest sdk. Just copy and paste source code. It should definitely work. Also try with other android device also.

  11. ren says:

    hello. thanks for the code. that’s a great example.
    however, when I run on my device, “Result will be here” text was not change. do you know why? thanks. sorry if my english is bad 🙂

  12. Ajay krishna says:

    Hi,
    Hardik,
    here my question is? I have Scanned a text using OCR text detection after scanning the text.The Scanned text, i want store in Sqlitedatabase.Please help me how i will achevie this.
    Thank you.

Leave a Reply

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