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 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.