Generate QR Codes [Android Dev]

In one of the previous posts, I quickly created a Covid Certificate QR code, so in this post I will slowly and in detail explain the creation of QR codes by creating a simple Android app in Android Studio. Guess you already know something like that, if not, there are enough videos on YT on how to install the IDE and run your first app.

The Library

Of course, nothing will be done manually because there are already a huge number of libraries for something like this. I opted for the most popular and latest version of: ZXing Android Embedded v4.3.0.

In order to add this library (which can both read and create QR codes) to the application, it is necessary to add this line in Gradle Scripts/build.gradle(Module:app).

plugins {...}

android {...}

dependencies {
    ...
    implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
}

Don’t forget to Sync now the project, after that you can use all the following classes in your application.

The Code

This simple method takes the content and the size of the QR code and returns the final image.

import android.util.Log;
import android.graphics.Bitmap;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.journeyapps.barcodescanner.BarcodeEncoder;
...
public static Bitmap generateQR(String content, int size) {
    Bitmap bitmap = null;
    try {
        BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
        bitmap = barcodeEncoder.encodeBitmap(content,
            BarcodeFormat.QR_CODE, size, size);
    } catch (WriterException e) {
        Log.e("generateQR()", e.getMessage());
    }
    return bitmap;
}

Android Activity

To display this image, you need to add an ImageView to the Activity and set the image into it.

app/res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
</androidx.constraintlayout.widget.ConstraintLayout>
app/java/MainActivity.java
import android.graphics.Bitmap;
import android.widget.ImageView;
...
public class MainActivity extends AppCompatActivity {

    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = findViewById(R.id.imageView);

        String content = "https://www.dragan.ba/";
        int screenWidth = getResources().getDisplayMetrics().widthPixels;

        Bitmap qrImage = generateQR(content, screenWidth);
        if (null != qrImage) {
            imageView.setImageBitmap(qrImage);
        }
    }
	
}

Hints

The first one looks good, but it can always get better. As another parameter we can pass “hints” which will slightly change the QR code.

import java.util.Map;
import java.util.HashMap;
...
Map hints = new HashMap();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q);
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.MARGIN,0);

bitmap = barcodeEncoder.encodeBitmap(content,
    BarcodeFormat.QR_CODE, size, size, hints);

Error Correction Level

Determines which degree of error correction to use.

ErrorCorrectionLevel.L
~7% Correction

ErrorCorrectionLevel.M
~15% Correction

ErrorCorrectionLevel.Q
~25% Correction

ErrorCorrectionLevel.H
~30% correction

Leave a Reply

Your email address will not be published. Required fields are marked *