Tutorial

Creating QR-codes in Android

Noräs Salman Noräs Salman |

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 {
    jcenter()

    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) {
            e.printStackTrace();
        }
        return null;
    }

Calling the method

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

About the author

Noräs Salman

"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 © 2019 - nindoda.com