Monday 26 June 2017

Generating and Reading QR Code (Two-Dimensional Barcode)

If you are looking for solution to generate and read QR code in your java application, I think you are visiting right post. In this post I am going to demonstrate how to generate and read bar code in Java.

About QR (Quick Response) Code 

QR Code is a two-dimensional barcode that is readable by smartphones. It allows to encode over 4000 characters in a two dimensional barcode. 

From Wikipedia: A QR code (abbreviated from Quick Response code) is a specific matrix barcode (or two-dimensional code) that is readable by dedicated QR barcode readers, camera telephones, and to a less common extent, computers with webcams. The code consists of black modules arranged in a square pattern on a white background. The information encoded may be text, URL, or other data.

QR codes are plastered on advertisements, billboards, business windows, and products. Now a days, these are being so popular and being utilized by different technical solutions. Paytm is one of the great example which has gained tremendous popularity where you can just scan QR code and pay. With the help of  QR code you can reduce typing effort for your app users. 

Open Source Lib for Barcode Image Processing 

ZXing ("zebra crossing") is an open-source, multi-format 1D/2D barcode image processing library implemented in Java. To get more detail refer this

Using ZXing, its very easy to generate/read QR code. If you are interested to generate/read QR code in your java code, you need to add below dependency in your maven java project:


<dependency>
               <groupId>com.google.zxing</groupId>
               <artifactId>javase</artifactId>
               <version>2.0</version>
</dependency>


How to Generate QR Code?
Here you will find the java example where you can generate QR code for your given string. In this example I am using my blog URL 'http://nverma-tech-blog.blogspot.com/' for which I want to generate QR code.

import java.io.File;
import java.io.FileOutputStream;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

public class QRCodeGenerator {

       public static void main(String[] args) throws Exception {

// this is the text that we want to encode          
String text = "http://nverma-tech-blog.blogspot.com/";
      
int width = 400;
       int height = 300; // change the height and width as per your requirement

       // (ImageIO.getWriterFormatNames() returns a list of supported formats)
// could be "gif", "tiff", "jpeg"
       String imageFormat = "png";

       BitMatrix bitMatrix =
new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, width, height);

MatrixToImageWriter.writeToStream(bitMatrix, imageFormat, new FileOutputStream(new File("MyBlogQRCode.png")));
      
}
}


Once you execute this program, it will generate QR code image named 'MyBlogQRCode.png' at the location where your program is executed. If you open QR code image, it will be like below:

Since I have encoded my blog URL 'http://nverma-tech-blog.blogspot.com/' and generated this QR code, if you scan this QR code from your mobile's camera, my blog URL will be opened on browser. 

During writing this post, I did same on my mobile (Motorola X Play) and sharing same flow with you. Ensure that QR code scanner is enabled on your mobile. 

As you scan above QR code on your mobile camera, you will see red marked icon displayed. 




Now click on this icon, two options will be displayed.


Now if you click on View Website, browser is opened and you will see my blog is accessed as per decoded URL 'http://nverma-tech-blog.blogspot.com/' in QRCode.



Now you can read my blog on your mobile too without typing actual URL on your mobile browser :). 

How to Read QR Code?

Here you will find the java example where you can decode your generated QR code and get the same string back which is encoded in QRCode.

import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.imageio.ImageIO;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Reader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

public class BarCodeReader {
      
       public static void main(String[] args) throws Exception {

InputStream barCodeInputStream = new FileInputStream("MyBlogQRCode.png");
            
BufferedImage barCodeBufferedImage = ImageIO.read(barCodeInputStream);

LuminanceSource source = new BufferedImageLuminanceSource(barCodeBufferedImage);
            
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
            
Reader reader = new MultiFormatReader();
             Result result = reader.decode(bitmap);

             System.out.println("Decoded barcode text is - " + result.getText());
            
       }
}


Output of this program is


Decoded barcode text is - http://nverma-tech-blog.blogspot.com/


Others
You can find other good references below on QR code:

Hope this post help you. If you have any question or feedback do write comment. I will try to assist you.


23 comments:

  1. Qrcode reader and barcode generate, there are many online free to use, but there are restrictions, such as can not set dpi, image size and so on.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. In this article understand the most important thing, the item will give you a keyword rich link a great useful website page: Bitcoin QR Code

    ReplyDelete
    Replies
    1. Hi there,
      Thank you so much for the post you do and also I like your post, Are you looking for bitcoin halving countdown in the whole USA? We are providing bitcoin halving countdown, Goldpreis ethereum kurs, bitcoin kurs live, s&p 500, bitcoin a euro, precio, bitcoin dolar, bitcoin fiyatı and our services are very fast.
      Click here for Contact: +1 626 245 8049 Email: disujarock2312@gmail.com


      Delete
  4. Thanks for this amazing Posts i really like your blog you are doing good work. Keep sharing good blogs and new ideas, features & latest trends in this technology.
    Asset Management Software
    Asset Management Software UAE
    Asset Management Software Abu Dhabi
    Asset Tracking Software
    IT Asset Management Software
    Fixed Asset Management Software

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Nice share! QR Code is widely used in our life. It is now changing the way how we live.
    How to Generate QR Code, How to Read QR Code

    ReplyDelete
  7. Hi there,
    Thank you so much for the post you do and also I like your post, Are you looking for Bitcoin QR Maker in the whole USA? We are providing Where to Bitcoin QR Maker, bitcoin qr generator with amount, bitcoin qr code generator blockchain, Bitcoin QR Code Generator with the well price and our services are very fast.
    Click here for vISIT https://www.bitcoinbarcodegenerator.com/

    ReplyDelete
  8. Hi there,
    Thank you so much for the post you do and also I like your post, Are you looking for Buy Methylone Online in the whole USA? We are providing bitcoin halving countdow, bitcoin win moon bitcoin live, bitcoin vault price, bitcoin halving 2020, bitcoin halving date with the well price and our services are very fast.
    Click here for Contact +16262458049
    Email:disujarock2312@gmail.com


    ReplyDelete
  9. Hi there,
    Thank you so much for the post you do and also I like your post, Are you looking for Buy Methylone Online in the whole USA? We are providing bitcoin halving countdow, bitcoin win moon bitcoin live, bitcoin vault price, bitcoin halving 2020, bitcoin halving date with the well price and our services are very fast.
    Click here for Contact +16262458049
    Email:disujarock2312@gmail.com


    ReplyDelete
  10. Hi there,
    Thank you so much for the post you do and also I like your post, Are you looking for Goldpreis ethereum kurs in the whole USA? We are providing Goldpreis
    ethereum kurs, bitcoin kurs live, s&p 500, bitcoin a euro, precio bitcoin dolar, bitcoin fiyatı with the well price and our services are very fast.
    Click here for Contact: +1 626 245 8049 Email: disujarock2312@gmail.com

    ReplyDelete
  11. Hi there,
    Thank you so much for the post you do and also I like your post, Are you looking for bitcoin qr code generator blockchain in the whole USA? We are providing bitcoin qr code generator blockchain, Bitcoin QR Code Generator, Bitcoin QR Code, Bitcoin QR, Bitcoin Address To QR Code
    bitcoin price x2 double your btc moon bitcoin live, Bitcoin vault our services are very fast.
    Click here for Contact: +1 626 245 8049 Email: disujarock2312@gmail.com

    ReplyDelete
  12. Great blog, very informative! Thank you for sharing. I think everyone appreciates your posts, they are always very helpful.

    Xerox Printer Repair Miami

    ReplyDelete
  13. Hi there,
    Thank you so much for the post you do and also I like your post, Are you looking for bitcoin halving countdown in the whole USA? We are providing bitcoin qr generator with amount bitcoin qr code generator blockchain, Bitcoin QR CodeGenerator, Bitcoin QR Code, Bitcoin QR, Bitcoin Address To QR Code and our services are very fast.
    Click here for Contact: +1 626 245 8049 Email: disujarock2312@gmail.com

    ReplyDelete
  14. Hi there,
    Thank you so much for the post you do and also I like your post, Are you looking for bitcoin halving countdown in the whole USA? We are providing qrcode btc, QR Code Bitcoin, Free Bitcoin QR Codes, BTC QR Code, BTC QR, BTC Address To QRCode and our services are very fast.
    Click here for Contact: +1 626 245 8049 Email: disujarock2312@gmail.com

    ReplyDelete
  15. Hi there,
    Thank you so much for the post you do and also I like your post, Are you looking for bitcoin halving countdown in the whole USA? We are providing Free Bitcoin QR Codes, BTC QR Code, BTC QR, BTC Address To QRCode, Bitcoin QR Maker, bitcoin qr generator with amount, bitcoin qr code generator blockchain, Bitcoin QR CodeGenerator and our services are very fast.
    Click here for Contact: +1 626 245 8049 Email: disujarock2312@gmail.com

    ReplyDelete
  16. Hi there,
    Thank you so much for the post you do and also I like your post, Are you looking for Most secure cryptocurrency in the whole USA? We are providing Augment Trade, most secure cryptocurrency, digital trading, Blockchain Technology, buy bitcoins online, Bitcoin users, The Cryptocurrency, bitcoin/Forex broker, bitcoin address , Bitcoin investment, with the well price and our services are very fast.
    Click here for MORE DETAILS......
    Email us at: info@augment-trade1.com

    ReplyDelete
  17. Hi there,
    Thank you so much for the post you do and also I like your post, Are you looking for Bitcoin Investment in the whole USA? We are providing Augment Trade, most secure cryptocurrency, digital trading, Blockchain Technology, buy bitcoins online, Bitcoin users, The Cryptocurrency, bitcoin/Forex broker, bitcoin address , Bitcoin investment, with the well price and our services are very fast.
    Click here for MORE DETAILS......
    Email us at: info@augment-trade1.com

    ReplyDelete
  18. Hi there,
    Thank you so much for the post you do and also I like your post, Are you looking for The cryptocurrency in the whole USA? We are providing Augment Trade, most secure cryptocurrency, digital trading, Blockchain Technology, buy bitcoins online, Bitcoin users, The Cryptocurrency, bitcoin/Forex broker, bitcoin address , Bitcoin investment, with the well price and our services are very fast.
    Click here for MORE DETAILS......
    Email us at: info@augment-trade1.com

    ReplyDelete
  19. Hi there,
    Thank you so much for the post you do and also I like your post, Are you looking for The bitcoin investment in the whole USA? We are providing Augment Trade, most secure cryptocurrency, digital trading, Blockchain Technology, buy bitcoins online, Bitcoin users, The Cryptocurrency, bitcoin/Forex broker, bitcoin address , Bitcoin investment, with the well price and our services are very fast.
    Click here for MORE DETAILS......
    Email us at: info@augment-trade1.com

    ReplyDelete
  20. Hi there,
    Thank you so much for the post you do and also I like your post, Are you looking for The bitcoin investmen The Digital trading online in the whole USA? We are providing Augment Trade, most secure cryptocurrency, digital trading, Blockchain Technology, buy bitcoins online, Bitcoin users, The Cryptocurrency, bitcoin/Forex broker, bitcoin address , Bitcoin investment, with the well price and our services are very fast.
    Click here for MORE DETAILS......
    Email us at: info@augment-trade1.com

    ReplyDelete
  21. Hi there,
    Thank you so much for the post you do and also I like your post, Are you looking for digital trading in the whole USA? We are providing Augment Trade, most secure cryptocurrency, Bitcoin users, The Cryptocurrency, bitcoin/Forex broker,digital trading, Blockchain Technology, buy bitcoins online, bitcoin address , Bitcoin investment, with the well price and our services are very fast.
    Click here for MORE DETAILS......
    Email us at: info@augment-trade1.com

    ReplyDelete
  22. Hi there,
    Thank you so much for the post you do and also I like your post, Are you looking for Buy Methylphenidate in the whole USA? We are providing bitcoin private key cracker, bitcoin private key hack online, bitcoin private key generator, bitcoin private key finder online bitcoin private key finder with balance, free private key bitcoin with balance, bitcoin private key hack online, bitcoin private key hack online, blockchain private key finder online blockchain private key finder online, bitcoin unconfirmed transaction hack software with the well price and our services are very fast. Click here for Contact: +15104732515, Email: admin@btcprivatekey.net

    ReplyDelete