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
I was looking for ID Card printer on google and there i saw your blog post regarding barcode scanner which is really helpful because you have mentioned many good points in that post....keep doing well and keep updating with new topic
ReplyDeleteThank you so much Azad. :D Really Happy to know that this blog post helped you.
ReplyDelete