Android QR Code Scanner

Introduction

In today's world of social distancing and working from home, marketers are seeing a detonating use of QR Codes. The use of QR codes is rapidly expanding. Now all products have their own unique QR code.

And now all devices have their own built-in QR Code Reader App.

However, there are times when we need a QR Code Scanner in our App.

So, here we learn about how to create a QR Code Scanner Application on Android.

Step 1

Create a new project in the Android Studio and select an empty activity.

Step 2

Give the project a name, select the save location folder, and click on the finish button.

Step 3

Add the following dependency in the build.gradle app level file.

implementation 'com.journeyapps:zxing-android-embedded:4.3.0'

Step 4

Create the activity_main.xml file as shown below. 

<?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/btnScan" />
</RelativeLayout>

Step 5

Declare a Button object and include the following code in the onCreate method in the MainActivity.

Button btnScan;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnScan = (Button) findViewById(R.id.btnScan);
    btnScan.setOnClickListener(v - > {
        ScanCode();
    });
}

Step 6

Create the ScanCode method in the MainActivity as shown below. 

private void ScanCode() {
    ScanOptions options = new ScanOptions();
    options.setPrompt("Volume up to flash on");
    options.setBeepEnabled(true);
    options.setOrientationLocked(true);
    options.setCaptureActivity(CaptureActivity.class);
    barLauncher.launch(options);
}

Step 7

Create a CaptureActivity Class as shown below. 

package com.Uday.qrcodescanner;
public class CaptureActivity extends com.journeyapps.barcodescanner.CaptureActivity {}

Step 8

Include CaptureActivity in the Manifiest File.

<activity android:name=".CaptureActivity"
     android:screenOrientation="portrait"
     android:stateNotNeeded="true"
     android:theme="@style/zxing_CaptureTheme" />

Step 9

Make a barLauncher object in the MainActivity File as shown below.

ActivityResultLauncher < ScanOptions > barLauncher = 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();
    }
});

Step 10

Include Camera Permission in the Manifests File. 

<uses-permission android:name="android.permission.CAMERA"/>

Final MainActivity.Java File

package com.Uday.qrcodescanner;

import androidx.activity.result.ActivityResultLauncher;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
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 btnScan;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnScan = (Button) findViewById(R.id.btnScan);

        btnScan.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(CaptureActivity.class);
        barLauncher.launch(options);
    }

    ActivityResultLauncher<ScanOptions> barLauncher = 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();
        }
    });
}

Step 11

Now run your app and allow camera permission as per the following and scan any QR code.

Summary

So you see, it's very easy to create a QR Code Scanner in Android with little coding.

In this article, we will learn how to create a QR Code Scanner on Android.

Thank you. Enjoy Coding.


Similar Articles