Native Mobile App Development With Java

Scan QR Android

27-Sep-2024

Scan QR Android


Step 1: Add dependencies


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


Step 2: Uses-permission manifest



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


Step 3:  MainAitivity


package com.grt.scanqrcodeandroid;

import android.os.Bundle;
import android.widget.Toast;
import android.content.Intent;

import androidx.appcompat.app.AppCompatActivity;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE);
integrator.setPrompt("Scan a QR code");
integrator.setCameraId(0);
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) {

String qrCodeValue = result.getContents();
Toast.makeText(this, "QR Code: " + qrCodeValue, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "No QR code found", Toast.LENGTH_SHORT).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}

Comments