Java – Generate QR Code using zxing | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

Read QR Code Article : Link

We’ll use Google’s Zxing library to generate QR codes for our application.

Zxing, pronounced as Zebra Crossing, is an open source, multi-format 1D/2D barcode image processing library implemented in java.

Add below dependencies in pom.xml.

<dependency>
	<groupId>com.google.zxing</groupId>
	<artifactId>core</artifactId>
	<version>3.4.0</version>
</dependency>

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

QRCodeGenerator.java

package com.example.java.programming;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;

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

/**
 * @author code.factory
 *
 */
public class QRCodeGenerator {
	
	private static final String IMAGE_PATH = "QR.png";
	private static final String IMAGE_FORMAT = "PNG";
	
	public static void main(String... args) {
		try {
			generateQR("Code Factory", 400, 400, IMAGE_PATH);
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	private static void generateQR(String msg, int width, int height, String imagePath) throws WriterException, IOException {
		QRCodeWriter writer = new QRCodeWriter();
		BitMatrix matrix = writer.encode(msg, BarcodeFormat.QR_CODE, width, height);
		Path path = FileSystems.getDefault().getPath(imagePath);
		MatrixToImageWriter.writeToPath(matrix, IMAGE_FORMAT, path);
		
		/*If you want to return byte array of QR code then use below code*/
		/*ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
		MatrixToImageWriter.writeToStream(matrix, IMAGE_FORMAT, pngOutputStream);
	    byte[] data = pngOutputStream.toByteArray(); 
	    return data;*/
	}
}

7 thoughts on “Java – Generate QR Code using zxing | Code Factory”

  1. Simply wish to say your article is as astounding. The clarity in your post is simply cool and i can assume you are an expert on this subject. Fine with your permission let me to grab your RSS feed to keep updated with forthcoming post. Thanks a million and please keep up the enjoyable work.

    Liked by 1 person

  2. I intended to write you the very small word to say thanks again for the amazing techniques you have contributed on this website. This has been really pretty open-handed of people like you to deliver without restraint exactly what a number of people could possibly have made available for an e-book to get some cash for themselves, chiefly given that you could possibly have tried it in case you wanted. These techniques as well acted to become a good way to understand that many people have the identical eagerness similar to my personal own to figure out lots more with respect to this condition. I believe there are numerous more pleasurable times up front for those who check out your site.

    Liked by 1 person

  3. Hello There. I discovered your weblog using msn. That is an extremely well written article. I’ll make sure to bookmark it and return to learn more of your helpful information. Thanks for the post. I will definitely return.

    Liked by 1 person

Leave a comment