QR code scanner using ZXing Library in Android

--

In this blog, we’re going to discuss how a scan QR code using the ZXing library in Android. Before going to implement the actual code first, let’s understand what is ZXing library.

ZXing

ZXing (“zebra crossing”) is an open-source, multi-format 1D/2D barcode image processing library implemented in Java, with ports to other languages. ZXing is also hosted under Google Open Source Project.

The latest version up to this date is 3.3.3.

QR code scanner example

Dependency

dependencies {
implementation ‘com.google.zxing:core:<latest:version>’
implementation(‘com.journeyapps:zxing-android-embedded:3.6.0’) { transitive = false }
}

Code

activity_main.xml

<?xml version=”1.0" encoding=”utf-8"?>
<LinearLayout 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”
android:orientation=”vertical”
android:layout_gravity=”center”
android:background=”#fff”
>

<
LinearLayout
android:orientation=”vertical”
android:layout_width=”match_parent”
android:layout_height=”250dp”
android:background=”@drawable/geadient_background”
>

<
TextView
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:fontFamily=”@font/asap_bold”
android:text=”@string/qr_code”
android:textColor=”#ffffff”
android:textSize=”32sp”
android:layout_marginTop=”80dp”
android:layout_marginStart=”16dp”
android:textStyle=”bold”
/>

<
TextView
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_marginStart=”16dp”
android:fontFamily=”@font/asap”
android:text=”@string/scanner”
android:textColor=”#FFFFFF”
android:textSize=”24sp”
/>

</
LinearLayout>

<
Button
android:layout_width=”250dp”
android:layout_height=”wrap_content”
android:id=”@+id/scan”
android:padding=”4dp”
android:layout_marginTop=”20sp”
android:layout_gravity=”center”
android:text=”Scan”
android:fontFamily=”@font/asap_bold”
android:textSize=”19sp”
android:textStyle=”bold”
android:textColor=”#ffffff”
android:background=”@drawable/button_background”
/>

</
LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
private Button scan;

scan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { IntentIntegrator intentIntegrator = new IntentIntegrator(MainActivity.this); intentIntegrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE); intentIntegrator.setCameraId(0); intentIntegrator.setOrientationLocked(false); intentIntegrator.setPrompt(“scanning”); intentIntegrator.setBeepEnabled(true); intentIntegrator.setBarcodeImageEnabled(true); intentIntegrator.initiateScan();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); if (result != null && result.getContents() != null) {
new AlertDialog.Builder(MainActivity.this).setTitle(“Scan Result”) .setMessage(result.getContents()) .setPositiveButton(“Copy”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) { ClipboardManager manager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); ClipData data = ClipData.newPlainText(“result”, result.getContents()); manager.setPrimaryClip(data); }
}).setNegativeButton(
“Cancel”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).create().show();
}
super.onActivityResult(requestCode, resultCode, data);
}
}

Output:

--

--

Golap Gunjan Barman

Hi everyone, myself Golap an Android app developer with UI/UX designer.