Creating QR-codes in Android

noras noras |

Tags: #Android #Java

Creating a qr-code will definitely be a plus to your android application if you intent to share small pieces of data between devices using qr-code scanning . Earlier I’ve a way to create and use the qr-code (or bar-code) scanning in android apps. You can check the article here Scanning QR-code with Android.

Adding the dependencies

In app level build.gradle

repositories {

    maven { url 'https://jitpack.io' }
    maven { url 'https://maven.google.com' }


dependencies {

compile 'com.journeyapps:zxing-android-embedded:3.0.2@aar'
compile 'com.google.zxing:core:3.2.0'

The awesome method

public static Bitmap getQRCODE(String qrCodeString) {
        QRCodeWriter writer = new QRCodeWriter();
        try {
            BitMatrix bitMatrix = writer.encode(qrCodeString, QR_CODE, 512, 512);
            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);
            return bmp;

        } catch (WriterException e) {
        return null;

Calling the method

imageView.setBitmap(getQRCODE("Any text here will be encoded in the qr-code"));

About the author


"Senior Software Engineer. MSc in Computer systems and Networks with big interest in security. Loves to play with Android code and does security research for fun and profit. Speaks 4 languages and codes in much more."

Related articles

Tags: #Android #Java

Copyright © 2020 - nindoda.com