What is QR Code?
QR Code (Quick Response Code) is the two-dimensional barcode that can be read by smartphones and QR Code reader scanners. A QR code uses four standardized encoding modes (numeric, alphanumeric, byte/binary, and kanji) to store data efficiently.QR Code can be used for multiple purposes some of them are
- Contact Infomation
- URL Link
- Wifi Network link
- Website Authentication
- Calendar Event
- Text Infomation
- Payment Infomation
- GEO Location
- App Store/Google Play Apps

If you want to know more about QR Code then you can find it at QR Code Wiki.
Generate QR Code in Java
ZXing maven dependencies
If you want to generate QR Code then you need to add below dependency in your project. I am using 3.3.2 version of this API as its the latest version of this library till today.
<!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.2</version>
</dependency>
Program to generate the QR Code
Now we will write a program to generate the QR Code, Here we are generating the QR code to an image file, so you need to provide a file path for the image where you want to generate it.
I have created a separate method to create a QR code, where I am passing, below values
int imageSize = 240;
String imagePath = "qrcode.png";
String imageType = "png";
String qrContent = "https://www.codelessons.in/";
Here is the program
package in.codelessons.qr;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.EnumMap;
import java.util.Map;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
/**
* @author Praveen Rawat
*
*/
public class QRCodeExample {
public static void main(String[] args) throws WriterException, IOException {
int imageSize = 240;
String imagePath = "qrcode.png";
String imageType = "png";
String qrContent = "https://www.codelessons.in/";
generateQRCode(imagePath, qrContent, imageSize, imageType);
System.out.println("QR Code Generated.");
}
public static void generateQRCode(String imagePath, String content, int size, String imageType)
throws WriterException, IOException {
File file = new File(imagePath);
Map hints = new EnumMap<>(EncodeHintType.class);
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size, hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
}
}
ImageIO.write(image, imageType, file);
}
}
You can find this Example at the official GitHub account of Code Lesson. Below is the link of the GitHub repository.
You can download this Project from our GitHub Repository.
No comments
Post your comment.