background-shape

QR Code scanner or Barcode scanner for android features are present in many apps to read some useful data.

Barcodes can scan things ranging from URL, Contact info to Geolocation, WIFI, Driver license IDs.

QR Code is the more popular format and is commonly seen in many applications from digital payments, automating production, enabling attendance for employees for a business and many more.

Below, we’ll be developing an application that scans the QR Code value from a bitmap image as well as detects QR Code through a camera and perform the relevant actions, without need of a internet connection.

We shall follow the following steps to implement QR code system in our application:

Step 1

Add following gradle dependencies to your app-level gradle file.

implementation 'com.google.android.gms:play-services-vision:20.1.3'
implementation 'com.google.zxing:core:3.2.1'
implementation 'com.journeyapps:zxing-android-embedded:3.2.0@aar'

Step 2

Add the following permission to your manifest to access camera to scan QR code that we will generate.

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

Step 3

Add this following into your manifest file too, as when you open your QR scanner to capture QR code this activity will open.

<activity
    android:name="com.journeyapps.barcodescanner.CaptureActivity"
    android:screenOrientation="portrait"
    android:stateNotNeeded="true"
    tools:replace="android:screenOrientation" />

Step 4 (Generation)

To generate QR just call this function along with the String you want to put into it.

private void qrGeneration(String qrText) {
        QRCodeWriter writer = new QRCodeWriter();
        try {
            BitMatrix bitMatrix = writer.encode(qrText, BarcodeFormat.QR_CODE, 564, 564);
            int width = bitMatrix.getWidth();
            int height = bitMatrix.getHeight();
            Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
                }
            }
            bitmap = bmp;
            ((ImageView) findViewById(R.id.qrImage)).setImageBitmap(bmp);

        } catch (WriterException e) {
            e.printStackTrace();
        }
    }
// Just set the bitmap to any imageView you want it to show.

Step 5 (Scanning)

Now we have generated the QR code we need to scan it. We will create an IntentIntegrator.

// Call it inside your on click method of scan
IntentIntegrator integrator = new IntentIntegrator(QrCodeActivity.this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt(getString(R.string.scan_qr_mssg));
integrator.setCameraId(0);
integrator.setOrientationLocked(true);
integrator.setBeepEnabled(true);
integrator.setBarcodeImageEnabled(false);
integrator.setBarcodeImageEnabled(true);
integrator.initiateScan();

Step 6

Now that we have scanned our QR code we need to extract the contents of QR for further use for our application for that we will override the onActivtyResult method.

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if (result != null) {
        if (result.getContents() == null) {
            Log.e("Scan*******", "Cancelled scan");
        } else {
            Log.e("Scan", "Scanned");
            String scanContents = result.getContents();
                        useThisResult(scanContent);   
        }
    } else {
        // This is important, otherwise the result will not be passed to the fragment or activiy
        super.onActivityResult(requestCode, resultCode, data);
    }
}

Congrats. 🥳You have implemented simple and efficient solution to implement QR code for your android application.

Hope it helped. Happy Coding 😀