Skip to main content

Barcode Scanner in Android using zxing Library


Hi Everyone,
Last week I tried to implement a barcode scanner using the zxing library. Writing this blog on this topic because I found the tutorials about barcode scanner using zxing is either pretty much older or confusing. Let's start by importing these two libraries in our Android project.
 compile 'com.journeyapps:zxing-android-embedded:3.0.2@aar'  
 compile 'com.google.zxing:core:3.2.0'  
Practically , we only use the back camera for scanning the barcode because it has more clarity and auto-focus feature. But , I will also show how to implement the same with the front camera. Create a new layout and add two buttons for opening the barcode scanner with front camera and back camera. activity_main.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <RelativeLayout  
   xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:app="http://schemas.android.com/apk/res-auto"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   tools:context="org.bestdoc.jitha.barcodereaderexample.MainActivity">  
   <LinearLayout  
     android:id="@+id/start_scanning_lin"  
     android:layout_centerInParent="true"  
     android:layout_width="match_parent"  
     android:gravity="center"  
     android:layout_height="wrap_content"  
     android:orientation="horizontal">  
   <TextView  
     android:id="@+id/start_scanning_front_camera"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Front Camera"  
     android:padding="10dp"  
     android:background="#009688"  
     android:layout_marginRight="25dp"  
     android:textColor="#FAFAFA"  
     app:layout_constraintBottom_toBottomOf="parent"  
     app:layout_constraintLeft_toLeftOf="parent"  
     app:layout_constraintRight_toRightOf="parent"  
     app:layout_constraintTop_toTopOf="parent" />  
     <TextView  
       android:id="@+id/start_scanning_back_camera"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:text="Back Camera"  
       android:padding="10dp"  
       android:background="#009688"  
       android:textColor="#FAFAFA"  
       app:layout_constraintBottom_toBottomOf="parent"  
       app:layout_constraintLeft_toLeftOf="parent"  
       app:layout_constraintRight_toRightOf="parent"  
       app:layout_constraintTop_toTopOf="parent" />  
   </LinearLayout>  
   <TextView  
     android:id="@+id/txt_content"  
     android:layout_below="@+id/start_scanning_lin"  
     android:layout_centerHorizontal="true"  
     android:layout_marginTop="10dp"  
     android:textSize="20sp"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     />  
 </RelativeLayout>  

Now, inside your onCreate() method put this.
  start_scanning_front_camera.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View view) {  
         new IntentIntegrator(MainActivity.this)  
             .setCameraId(1)  
             .initiateScan();  
       }  
     });  
     start_scanning_back_camera.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View view) {  
         new IntentIntegrator(MainActivity.this).initiateScan();  
       }  
     });  
We have created an Instance of the Intent Integrator class we just imported. Now we can use this class to start scanning. initiateScan() is the method which will start scanning. You must have noticed that , there is an extra line of code in front camera onclickListener block. The setCameraId(1) is the method which tells the app to open the front camera instead the default back camera.
Now we have to receive the result of the barcode scan. We have to make sure we receive them inside the onActivityResult() method.
  public void onActivityResult(int requestCode, int resultCode, Intent intent)  
   {  
 //retrieve scan result  
     IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);  
     if (scanningResult != null) {  
       String scanContent = scanningResult.getContents();  
       txt_content.setText(scanContent);  
     }  
   }  
We must parse the result into an Zxing Intentresult we just created and retrieve the content as a String value.
I found that , using front camera it's pretty much difficult to scan the entire barcode and the zxing library marked it as a bug in their Github repo. So, if you found any easy way to implement the front camera scanning , Please share.
Full project is available on Github for free , please check

Comments

Popular posts from this blog

Customize Run Time Permission Dialog in Android

Hello. We all know how run time permissions came into effect from Android 6.0 ( Marshmallow ). The advantage is that it's more user friendly and it gives the user an idea about why he has to give such permissions for the app. Such as giving his contact details , his location details etc. But, even-though there is one flaw in it. Maybe exactly not a flaw, but we can't really customize that run-time permission dialog or it's contents. Android OS won't allow us to customize it's default settings in this case. So, the only option now remaining is to pop up a dialog just before the original run-time permission dialog. You can then describe to your user what exactly you are going to do by accessing such permissions. I have found many answers on StackOverflow and I combined those answers to my required one.
What we are going to do today is asking the permission of user for accessing his location. Simply said, asking him to turn on his GPS.
There is a method called chec…

Expandable ListView in Android on Complex Layout

Hello,
Last day while at work I encountered an issue such as when I try to implement a expandable listview the override method "@getChildView" is not being called. When I looked around, I found out that the issue is mainly related to the expandable listview being inside a scrollview or any such complex layouts. I fixed the issue after searching through some answers on stack overflow, I have provided the links of the original answer at the end of the post
Response will be of the format
{ "movie_details": [ { "name": "Fight Club", "type": "Movie", "id": 1 }, { "name": "13 reasons why", "type": "Series", "id": 2 }, { "name": "Dunkirk", "type": "Movie", "id": 3 }, { "name": "Game of thrones", &quo…