How to Read QR Code Android Using Zxing Library

0
Shares
How to Read QR Code Android Using Zxing Library
4.8 (95%) 4 votes

After barcodes, QR codes have become universal in past few years. You might have also noticed on advertisements and/or certain brand products. In simple terms, QR codes are images that are designed to be read by all machines, just like the barcodes. Both QR code Android and iOS represent a small string like a shortened URL and/or a contact number.

Here’s a sample QR code that contains URL of Space-O Technologies.

static_qr_code_without_logo

Unlike barcodes, the QR codes can be scanned accurately with a smartphone having a decent camera. And it doesn’t require any specialized hardware like the barcodes.

Understanding QR Code

QR code or Quick response code is basically a two-dimensional barcode that decode contents at higher speed. The first QR code system was invented by a japanese company called Dense-Wave in 1994.

And today, even a smartphone having a barcode reader app can scan and decode any QR code. Therefore in this Android app tutorial, we’re going to demonstrate the process of scanning image of QR code Android at click of button using Zxing library.

Let’s Get Started

Create a new project in Android Studio.

Add aar dependency with Gradle.

Add the following to your build.gradle file:

repositories {

    jcenter()

}

dependencies {

    compile 'com.journeyapps:zxing-android-embedded:3.4.0'

    compile 'com.android.support:appcompat-v7:23.1.0'   // Version 23+ is required

}

android {

    buildToolsVersion '23.0.2' // Older versions may give compile errors

}

Next, use IntentInegrator

Launch the intent with the default options:

Start Activity For Result

new IntentIntegrator(this).initiateScan(); // `this` is the current Activity

Get and Handle Result

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);

    if(result != null) {

        if(result.getContents() == null) {

//cancel

        } else {

//Scanned successfully

        }

    } else {

        super.onActivityResult(requestCode, resultCode, data);

    }

}

Adding Customization Options

Set params

IntentIntegrator integrator = new IntentIntegrator(this);

integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);

integrator.setPrompt("Scan a barcode");

integrator.setCameraId(0);  // Use a specific camera of the device

integrator.setBeepEnabled(false);

integrator.setBarcodeImageEnabled(true);

integrator.initiateScan();

Changing The Orientation

Add Activity in AndroidManifest.xml

<activity

android:name="com.journeyapps.barcodescanner.CaptureActivity"

android:screenOrientation="portrait"

tools:replace="screenOrientation" />

Set params in Intent

IntentIntegrator integrator = new IntentIntegrator(this);

integrator.setOrientationLocked(false);

integrator.initiateScan();

Add Following in String.xml

<string name="scan">Scan</string>

<string name="scan">Scan</string>

Start Code Integration

  • MainActivity.java
public class MainActivity extends AppCompatActivity implements OnClickListener {

private Button scanBtn;

private TextView tvScanFormat, tvScanContent;

private LinearLayout llSearch;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

scanBtn = (Button) findViewById(R.id.scan_button);

tvScanFormat = (TextView) findViewById(R.id.tvScanFormat);

tvScanContent = (TextView) findViewById(R.id.tvScanContent);

llSearch = (LinearLayout) findViewById(R.id.llSearch);

scanBtn.setOnClickListener(this);

}

public void onClick(View v) {

llSearch.setVisibility(View.GONE);

IntentIntegrator integrator = new IntentIntegrator(this);

integrator.setPrompt("Scan a barcode or QRcode");

integrator.setOrientationLocked(false);

integrator.initiateScan();

//        Use this for more customization

//        IntentIntegrator integrator = new IntentIntegrator(this);

//        integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);

//        integrator.setPrompt("Scan a barcode");

//        integrator.setCameraId(0);  // Use a specific camera of the device

//        integrator.setBeepEnabled(false);

//        integrator.setBarcodeImageEnabled(true);

//        integrator.initiateScan();

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);

if (result != null) {

if (result.getContents() == null) {

llSearch.setVisibility(View.GONE);

Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();

} else {

llSearch.setVisibility(View.VISIBLE);

tvScanContent.setText(result.getContents());

tvScanFormat.setText(result.getFormatName());

}

} else {

super.onActivityResult(requestCode, resultCode, data);

}

}

}
  • Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/activity_main"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context="com.qrcodescanner.MainActivity">

<Button

android:id="@+id/scan_button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:text="@string/scan" />

<LinearLayout

android:id="@+id/llSearch"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@id/scan_button"

android:layout_marginTop="20dp"

android:orientation="vertical"

android:visibility="gone">

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:minEms="4"

android:text="Format :"

android:textColor="@color/colorPrimary"

android:textIsSelectable="true"

android:textSize="16dp"

android:textStyle="bold" />

<TextView

android:id="@+id/tvScanContent"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:layout_marginLeft="10dp"

android:layout_weight="1.0"

android:textColor="@android:color/black"

android:textIsSelectable="true"

android:textSize="14sp" />

</LinearLayout>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"

android:orientation="horizontal">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:minEms="4"

android:text="Content :"

android:textColor="@color/colorPrimary"

android:textIsSelectable="true"

android:textSize="16dp"

android:textStyle="bold" />

<TextView

android:id="@+id/tvScanFormat"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:layout_marginLeft="10dp"

android:layout_weight="1.0"

android:textColor="@android:color/black"

android:textIsSelectable="true"

android:textSize="14sp" />

</LinearLayout>

</LinearLayout>

</RelativeLayout>

Congratulations!

Now when we run the demo, this is how it will perform.

If you’ve followed the exact step, then you’ll successfully integrate QR code Android scanner in your Android app. However, if you face any problem implementing it, you can contact our Android developers for help.

Even though this was just a simple QR code reader demo, but you can also add certain features like automatically saving URLs and/or contact numbers in your smartphone. And in case if you need expert’s help to build an Android application, contact us to hire Android app developer.

Grab a free copy of QR Code Android demo from Github.

 
0
Shares
 

Want to Develop Android App? Contact Us Now

Get your free consultation now