QR Code, Barcode can be found on grocery goods cartons which is can carry some info with it. To read those strip images we need a scanner that can interpreted data to human readable. There are many Android apps in Play Store which can read QR code and Barcode. Is it easy to build those application?
This tutorial we are going to implement QR code and barcode scanner in Android App. You might follow steps below and I believed you may take short time to finish it.
Step 1: Create a new project in Android Studio (File -> New -> Project -> Select Empty Activity -> Name project QRScanner -> Finish)
Step 2: Insert Zxing Android Library to build.gradle file (Module: app). (app -> build.gradle(Module: app))
implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
Then click Sync Now on top right to download library and wait to completed
Step 3: We are going to design layout by insert a button for click and activate camera to scanning QR code in main_activity.xml. Open the following to edit the layout (app -> res -> layout -> 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=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scan"
android:layout_centerInParent="true"
android:id="@+id/btn_scan"
/>
</RelativeLayout>
Step 4: We are going to implement Java code in MainActivity.java class. Open file the following (app -> java -> MainActivity.java)
package com.cambotutorial.sovary.qrscanner;
import androidx.activity.result.ActivityResultLauncher;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Button;
import com.journeyapps.barcodescanner.ScanContract;
import com.journeyapps.barcodescanner.ScanOptions;
public class MainActivity extends AppCompatActivity
{
Button btn_scan;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_scan =findViewById(R.id.btn_scan);
btn_scan.setOnClickListener(v->
{
scanCode();
});
}
}
We assign the button reference from view and set the action click listener with it and we are not implement scanCode();
method yet.
Step 5: Define and implement method scanCode();
within ScanOptions
object to set some setting and to launch the camera but we still not complete yet.
private void scanCode()
{
ScanOptions options = new ScanOptions();
options.setPrompt("Volume up to flash on");
options.setBeepEnabled(true);
options.setOrientationLocked(true);
options.setCaptureActivity(CaptureAct.class);
barLaucher.launch(options);
}
There are two errors at compile time. CaptureAct
and barLauncher
are not implement yet.
Step 6: We are going to create an empty class CaptureAct
that we are going to extend from CaptureActivity
class which is from Zxing library.
package com.cambotutorial.sovary.qrscanner;
import com.journeyapps.barcodescanner.CaptureActivity;
public class CaptureAct extends CaptureActivity
{
}
CaptureAct class no need to implement anything because everything is from its super class (CaptureActivity
). This class will handle to launch the camera including run time asking permission camera.
Step 7: We are going insert an activity attribute in manifest file . Open and edit file in app -> manifests -> AndroidManifest.xml
<activity android:name=".CaptureAct"
android:screenOrientation="portrait"
android:stateNotNeeded="true"
android:theme="@style/zxing_CaptureTheme"/>
Basically, every time we create activities this AndroidManifest.xml will automatically insert kind of these attributes above to tell how many activities in application? how each activity should act by the setting.
This CaptureAct
class is also treat as an activity that's why we have to manually add in this file too.
Final Step : Implement barLauncher
object to get result and display in alert dialog (popup result)
ActivityResultLauncher<ScanOptions> barLaucher = registerForActivityResult(new ScanContract(), result->
{
if(result.getContents() !=null)
{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Result");
builder.setMessage(result.getContents());
builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
dialogInterface.dismiss();
}
}).show();
}
});
Okay, now let's see full snippet code below so that you can verify or download the source code in below link
package com.cambotutorial.sovary.qrscanner;
import androidx.activity.result.ActivityResultLauncher;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Button;
import com.journeyapps.barcodescanner.ScanContract;
import com.journeyapps.barcodescanner.ScanOptions;
public class MainActivity extends AppCompatActivity
{
Button btn_scan;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_scan =findViewById(R.id.btn_scan);
btn_scan.setOnClickListener(v->
{
scanCode();
});
}
private void scanCode()
{
ScanOptions options = new ScanOptions();
options.setPrompt("Volume up to flash on");
options.setBeepEnabled(true);
options.setOrientationLocked(true);
options.setCaptureActivity(CaptureAct.class);
barLaucher.launch(options);
}
ActivityResultLauncher<ScanOptions> barLaucher = registerForActivityResult(new ScanContract(), result->
{
if(result.getContents() !=null)
{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Result");
builder.setMessage(result.getContents());
builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
dialogInterface.dismiss();
}
}).show();
}
});
}
Download source code in description
You might Also Like:
As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him