QRCode is abbreviated as Quic Response Code and we are quite familiar with QRCodes now a days. It is used for authenticated and quick online payments. A QR code uses four standardized encoding modes (numeric, alphanumeric, byte/binary, and kanji) to store data efficiently; extensions may also be used.
A QRCode is an arrangement of black and white squares and can be read with various QRCode Scanners and is convenient today because every smartphone has a QRcode scanner app.
Library used for generating QRCodes (ZXing)
ZXing (“Zebra Crossing”) is the popular API for QR code processing in Java. Its library has multiple components and we will be using the ‘core’ for QR code creation in our Java example.
How to generate QR code?
Following code is an example to create a QR code image.
- Download the ZXING library from here.
- Add ZXING dependency in maven file.
<dependencies><dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.3.0</version></dependency><dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.3.0</version></dependency></dependencies>chevron_rightfilter_none - Write the code to generate QR code and save it as a jpg file in the native folder.
// Java code to generate QR codeimportjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.IOException;importjava.util.HashMap;importjava.util.Map;importjavax.imageio.ImageIO;importcom.google.zxing.BarcodeFormat;importcom.google.zxing.BinaryBitmap;importcom.google.zxing.EncodeHintType;importcom.google.zxing.MultiFormatReader;importcom.google.zxing.MultiFormatWriter;importcom.google.zxing.NotFoundException;importcom.google.zxing.Result;importcom.google.zxing.WriterException;importcom.google.zxing.client.j2se.BufferedImageLuminanceSource;importcom.google.zxing.client.j2se.MatrixToImageWriter;importcom.google.zxing.common.BitMatrix;importcom.google.zxing.common.HybridBinarizer;importcom.google.zxing.qrcode.decoder.ErrorCorrectionLevel;publicclassQRCode {publicstaticvoidmain(String[] args)throwsWriterException,IOException,NotFoundException{// The data that the QR code will containString data ="www.geeksforgeeks.org";// The path where the image will get savedString path ="F:\user\QRCodes";// Encoding charsetString charset ="UTF-8";Map<EncodeHintType,ErrorCorrectionLevel>hashMap=newHashMap<EncodeHintType,ErrorCorrectionLevel>();hintMap.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.L);// Create the QR code and save// in the specified folder// as a jpg filecreateQRCode(fileName, path,charset, hashMap,200,200);System.out.println("QR Code Generated!!! ");}// Function to create the QR codepublicstaticvoidcreateQR(String data, String path,String charset, Map hashMap,intheight,intwidth)throwsWriterException, IOException{BitMatrix matrix=newMultiFormatWriter().encode(newString(qrCodeData.getBytes(charset),charset),BarcodeFormat.QR_CODE, width, height);MatrixToImageWriter.writeToFile(matrix,path.substring(path.lastIndexOf('.') +1),newFile(path));}}chevron_rightfilter_none - Output file will be by name gog.jpg
- On scanning this QRCode you will be redirected to geeksforgeeks home page.
How to generate QR code?
After generating QR code, we can also read the QR code image file using ZXing library. Below is the code to do so.
// Java code to read the QR code import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.BinaryBitmap; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatReader; import com.google.zxing.MultiFormatWriter; import com.google.zxing.NotFoundException; import com.google.zxing.Result; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.common.HybridBinarizer; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; public class QRCode { public static void main(String[] args) throws WriterException, IOException, NotFoundException { // Path where the QR code is saved String path = "F:\user\QRCodes"; // Encoding charset String charset = "UTF-8"; Map<EncodeHintType, ErrorCorrectionLevel> hashMap = new HashMap<EncodeHintType, ErrorCorrectionLevel>(); hintMap.put( EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); System.out.println( "QRCode output: " + readQRCode(filePath, charset, hintMap)); } // Function to read the QR file public static String readQR( String path, String charset, Map hashMap) throws FileNotFoundException, IOException, NotFoundException { BinaryBitmap binaryBitmap = new BinaryBitmap( new HybridBinarizer( new BufferedImageLuminanceSource( ImageIO.read( new FileInputStream(path))))); Result result = new MultiFormatReader() .decode(binaryBitmap); return result.getText(); } } |
Output:
QRCode output: www.geeksforgeeks.org
Recommended Posts:
- Image Processing in Java | Set 1 (Read and Write)
- SDL library in C/C++ with examples
- Python | OpenCV program to read and save an Image
- Chain Code for 2D Line
- Morse Code Translator In Python
- Understanding Code Reuse and Modularity in Python 3
- Project Idea | (Static Code Checker for C++)
- Project Idea (Augmented Reality - QR Code Scanner)
- Creating a C/C++ Code Formatting tool with help of Clang tools
- Project Idea (Augmented Reality - ARuco Code Detection and Estimation)
- Tesseract OCR with Java with Examples
- Generating Password and OTP in Java
- Image Processing In Java | Set 2 (Get and set Pixels)
- A Group chat application in Java
- Introduction to Project Lombok in Java and How to get started?
- Java | Converting an Image into Grayscale using cvtColor()
- Image Processing in Java | Set 14 ( Comparison of two images )
- Image Processing in Java | Set 12 ( Contrast Enhancement )
- Image Processing in Java | Set 9 ( Face Detection )
- Pizza Shop Billing System using Java Swing
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


