In this article we will see how to generate QR code in java. We are going to use ZXing (Zebra Crossing) library for this. QR stands for Quick Response. Below is an example of QR Code. Open your mobile's camera and put that in front of this image (QR Code) and you will see URL as http://www.infybuzz.com which is basically data this QR Code is containing.
You may have used this kind of QR code to pay while buying milk or grocery. May be you are using PayPal, Google Pay or any other digital payment methods.
So lets get started by adding ZXing dependencies. Add below dependencies in your pom.xml OR build.gradle file.
<dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.4.1</version> </dependency>
After this lets move to code part to generate QR code for text data that is http://www.infybuzz.com
import java.nio.file.Paths; import com.google.zxing.BarcodeFormat; import com.google.zxing.MultiFormatWriter; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; public class GenerateQRCode { public static void main(String[] args) throws Exception { String data = "http://www.infybuzz.com"; String path = "D:\\QR-Code\\infybuzz.jpg"; BitMatrix matrix = new MultiFormatWriter() .encode(data, BarcodeFormat.QR_CODE, 500, 500); MatrixToImageWriter.writeToPath(matrix, "jpg", Paths.get(path)); } }
After generating this QR code if you want to decode it then you can use any online QR code decoder. One of those is ZXing Decoder Online
0 Comments