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.

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

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

 

Download Source Code

 

Creating scan barcode and QRcode programmatically in 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 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

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:

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

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 final result by running result.getContents() method and we have set it into TextView of MainActivity.

So it was all about scan barcode and QRcode programmatically in android studio example. Thank you 🙂

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

16 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.

Leave a Reply

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