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.

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

Try Our App

Download DemoNuts app to learn anytime, anywhere: DemoNuts Android App

How to get maximum benefit from scan barcode and QRcode using zxing Android Tutorial

Download Source Code

Try Our App

Download DemoNuts app to learn anytime, anywhere: Click to get Demonuts App

Or search “demonuts” in play store from android mobile device.

Creating scan barcode and qrcode using zxing android Example

If you are using adblocker, then you will not be able to show our site smoothly. For example, you find step 6 after step 1 or similar to this. So please disable adblocker to fully utilize all resources of DemoNuts

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:

Step 5: Updating MainActivity.java file:

Update MainActivity.java as per below code:

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

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

How to get exact Android example while searching in Google

Click to get Important Google searching tips

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

14 Comments
  1. Andrew
  2. Wai Lay
    • Hardik Parsania
  3. WQ Yew
    • Hardik Parsania
      • WQ Yew
  4. WQ Yew
  5. Sary
    • Hardik Parsania
  6. Alex
    • Hardik Parsania
      • Puzzlo
  7. pranish
  8. meowww

Leave a Reply

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