How to generate qr code or barcode in java
In this tutorial, I'll show you how to create a QR code or barcode. How can you utilize or save the generated file?
In this tutorial, we will generate barcodes or QR codes using the zxing java package. zxing is an open-source, multi-format 1D/2D barcode image processing toolkit written in Java with portability to other programming languages.
So, first and foremost, make a maven or Gradle project and follow this.
Add these dependencies to your project.
Now create a class and add this code.
Here I have added some default values for the barcode. The first member is BarcodeFormat. you can add many formats Find more formats here. The second and third is the height width of the image and the last one is for image format.
Now, Add generator() in this class and add this code.
You will see there are 2 new classes:
And it's time to combine it all together.
Here I am Getting the object of the generator and generating to ByteArrayOutputStream and saving it to the file. It will save an image to the current directory with the name barcode.jpg.
But we can do a lot more here. if we want to generate the barcode of CODE_128 add this code before generating.
Many times we want an image as base64 we can easily convert like this.
Output:
<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>
implementation 'com.google.zxing:core:3.4.1' implementation 'com.google.zxing:javase:3.4.1'
Now create a class and add this code.
import com.google.zxing.BarcodeFormat; public class QRGenerator { private BarcodeFormat barcodeFormat = BarcodeFormat.QR_CODE; private int height = 500; private int width = 500; private String imageFormat = "jpg"; }
Here I have added some default values for the barcode. The first member is BarcodeFormat. you can add many formats Find more formats here. The second and third is the height width of the image and the last one is for image format.
Now, Add generator() in this class and add this code.
private ByteArrayOutputStream generate(String text) throws WriterException, IOException { var encoder = new MultiFormatWriter(); var matrix = encoder.encode( new String(text.getBytes(StandardCharsets.UTF_8)), barcodeFormat, width, height ); ByteArrayOutputStream bos = new ByteArrayOutputStream(); MatrixToImageWriter.writeToStream(matrix,imageFormat,bos); return bos; }
You will see there are 2 new classes:
- MultiFormatWriter:- MultiFormatWriter class belongs to zxing core repo. encode method of MultiFormatWriter is responsible to convert text to BitMatrix.
-
MatrixToImageWriter:- this class belongs to zxing JavaSE repo. With the help
of this, we converted BitMatrix to stream. I am returning
ByteArrayOutputStream for more use. we will see later. We can also directly
save to file like this.
MatrixToImageWriter.writeToPath(matrix,"jpg",Path.of("img.jpg"));
And it's time to combine it all together.
public static void main(String[] args) throws WriterException, IOException { var generator = new QRGenerator(); var stream = generator.generate("skx coding"); Files.write(Path.of("barcode.jpg"),stream.toByteArray()); }
Here I am Getting the object of the generator and generating to ByteArrayOutputStream and saving it to the file. It will save an image to the current directory with the name barcode.jpg.
But we can do a lot more here. if we want to generate the barcode of CODE_128 add this code before generating.
generator.barcodeFormat = BarcodeFormat.CODE_128; generator.height = 100; generator.width = 300;
Many times we want an image as base64 we can easily convert like this.
String imgBase64 =
Base64.getEncoder().encodeToString(stream.toByteArray());
Full Code
import com.google.zxing.BarcodeFormat; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.Base64; public class QRGenerator { private BarcodeFormat barcodeFormat = BarcodeFormat.QR_CODE; private int height = 500; private int width = 500; private String imageFormat = "jpg"; private ByteArrayOutputStream generate(String text) throws WriterException, IOException { var encoder = new MultiFormatWriter(); var matrix = encoder.encode( new String(text.getBytes(StandardCharsets.UTF_8)), barcodeFormat, width, height ); ByteArrayOutputStream bos = new ByteArrayOutputStream(); MatrixToImageWriter.writeToStream(matrix,imageFormat,bos); return bos; } public static void main(String[] args) throws WriterException, IOException { var generator = new QRGenerator(); var stream = generator.generate("https://skxcoding.blogspot.com"); Files.write(Path.of("barcode.jpg"),stream.toByteArray()); String imgBase64 = Base64.getEncoder().encodeToString(stream.toByteArray()); } }
Output:
That's more for today.
happy coding 😊!
Comments
Post a Comment