How to Create QR code scanner Android app
May 9, 2022, Originally published at cambotutorial.com ・4 min read
In this article, we will build a QR code and barcode reader in an Android app. You may follow the steps below, and I believe you would finish it quickly.
Step 1: Create a new project
Go to File -> New -> Project -> Select Empty Activity -> Name project QRScanner -> Finish
Step 2: Insert Dependency
Go to build.gradle file (Module: app). app -> build.gradle(Module: app)
implementation 'com.journeyapps:zxing-android-embedded:4.3.0'Then press the Sync link to download the library and wait for it to finish.
Step 3: Design Layout
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: Java Implementation
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 AppCompatActivityGet cambotutorial’s stories in your inbox
Join Medium for free to get updates from this writer.
{
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();
}
});}
Step 5: Implement CaptureActivity
Create new Class and implement nothing
package com.cambotutorial.sovary.qrscanner;
import com.journeyapps.barcodescanner.CaptureActivity;
public class CaptureAct extends CaptureActivity
{
}Step 6: Add Theme & Register Activity
Open and edit file in app -> manifests -> AndroidManifest.xml
<activity android:name=".CaptureAct"
android:screenOrientation="portrait"
android:stateNotNeeded="true"
android:theme="@style/zxing_CaptureTheme"/>Step 7: Run App
Finally we can run test application
You might Also Like:










