Generate barcode in android app using Zxing.

Aanand Shekhar Roy
1 min readApr 14, 2017

Recently I was working on a wallet app as a freelancer where I had to implement a barcode generator in the customer side of the app which enables easy transactions. I used Zxing library for generating QRcode. It is very easy and QR code can be generated within a few lines of code.

First, add the following dependencies in the build.gradle file of app directory.

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

After that, you can create an ImageView in your layout file and generate the QR code using following snippet.

String text="" // Whatever you need to encode in the QR code
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
try {
BitMatrix bitMatrix = multiFormatWriter.encode(text, BarcodeFormat.QR_CODE,200,200);
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
imageView.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}

I created a demo application which you can find in this repository.

Thanks for reading this article. Be sure to click ❤ below to recommend this article if you found it helpful. It would let others get this article in feed and spread the knowledge.

For more about programming, follow me, so you’ll get notified when I write new posts.

--

--

Aanand Shekhar Roy

Senior Software Engineer @Joist, Author of Kotlin Programming Cookbook.