Java QR Code Generator – zxing示例

别兴国
2023-12-01

Today we will look into the Java QR code generator program. If you are tech and gadget savvy, then you must be aware of the QR code. You will find it everywhere these days – in blogs, websites, and even in some public places. This is very popular in mobile apps, where you scan the QR code using a QR Code scanner app and it will show you the text or redirect you to the web page if it’s URL.

今天,我们将研究Java QR代码生成器程序。 如果您精通技术和小工具,则必须注意QR码。 这些天,到处都可以找到它-在博客,网站,甚至在某些公共场所。 这在移动应用程序中非常流行,在移动应用程序中,您可以使用QR Code扫描仪应用程序扫描QR Code,它将显示文本或将您重定向到网页(如果是URL)。

I came across this recently and found it very interesting. If you want to know about QR Code, you can find a lot of useful information at Wikipedia QR Code Page.

我最近遇到了这个问题,发现它非常有趣。 如果您想了解QR Code,可以在Wikipedia QR Code Page上找到很多有用的信息。

Java QR代码生成器 (Java QR code generator)

When I found QR code images on so many websites, I started looking for java QR code generator. I looked into some open source APIs and found zxing to be the simple and best to use.

当我在许多网站上找到QR码图像时,我开始寻找Java QR码生成器。 我研究了一些开源API,发现zxing是最简单且最易于使用的。

If you want to generate a QR code image, then we only need its core library. Just add below dependency to your maven project.

如果您想生成QR码图像,那么我们只需要其核心库。 只需在您的Maven项目中添加以下依赖项即可。

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

If you want to read QR image through the command line, then we need to use it’s JavaSE library. You can add below dependency for it.

如果要通过命令行读取QR图像,则需要使用它的JavaSE库。 您可以为其添加以下依赖项。

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

This will also let you know two extra dependencies required to run from the command line, as shown in the below image. We will have to add these jars into classpath to run the client app to read the QR code image. We will see this in action later on in this tutorial.

这还将使您知道从命令行运行所需的两个额外的依赖关系,如下图所示。 我们将必须将这些jar添加到classpath中,以运行客户端应用程序以读取QR码图像。 我们将在本教程后面的部分中看到这一点。

zxing示例生成QR码图像 (zxing example to generate QR code image)

Here is the program you can use to create QR Code image with zxing API.

这是您可以用来通过zxing API创建QR Code图片的程序。

GenerateQRCode.java

GenerateQRCode.java

package com.journaldev.qrcode.generator;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class GenerateQRCode {

	public static void main(String[] args) throws WriterException, IOException {
		String qrCodeText = "https://www.journaldev.com";
		String filePath = "JD.png";
		int size = 125;
		String fileType = "png";
		File qrFile = new File(filePath);
		createQRImage(qrFile, qrCodeText, size, fileType);
		System.out.println("DONE");
	}

	private static void createQRImage(File qrFile, String qrCodeText, int size, String fileType)
			throws WriterException, IOException {
		// Create the ByteMatrix for the QR-Code that encodes the given String
		Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<>();
		hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
		QRCodeWriter qrCodeWriter = new QRCodeWriter();
		BitMatrix byteMatrix = qrCodeWriter.encode(qrCodeText, BarcodeFormat.QR_CODE, size, size, hintMap);
		// Make the BufferedImage that are to hold the QRCode
		int matrixWidth = byteMatrix.getWidth();
		BufferedImage image = new BufferedImage(matrixWidth, matrixWidth, BufferedImage.TYPE_INT_RGB);
		image.createGraphics();

		Graphics2D graphics = (Graphics2D) image.getGraphics();
		graphics.setColor(Color.WHITE);
		graphics.fillRect(0, 0, matrixWidth, matrixWidth);
		// Paint and save the image using the ByteMatrix
		graphics.setColor(Color.BLACK);

		for (int i = 0; i < matrixWidth; i++) {
			for (int j = 0; j < matrixWidth; j++) {
				if (byteMatrix.get(i, j)) {
					graphics.fillRect(i, j, 1, 1);
				}
			}
		}
		ImageIO.write(image, fileType, qrFile);
	}

}

Here is the QR Code image file created by this program. You can use your mobile QR Code scanner app to test it. It should point to JournalDev Home URL.

这是此程序创建的QR Code图像文件。 您可以使用移动QR码扫描仪应用程序对其进行测试。 它应该指向JournalDev主页URL。

zxing示例读取QR码 (zxing example to read QR code)

If you don’t have a mobile app to test it, don’t worry. You can read QR code with zxing API through the command line.

如果您没有移动应用程序进行测试,请不要担心。 您可以通过命令行使用zxing API读取QR码。

Below is the command to read the QR code image file. Notice the additional jars in the classpath that zxing depends on.

以下是读取QR码图像文件的命令。 请注意zxing依赖的类路径中的其他jar。

$java -cp $HOME/.m2/repository/com/google/zxing/javase/3.3.2/javase-3.3.2.jar:.:$HOME/.m2/repository/com/google/zxing/core/3.3.2/core-3.3.2.jar:$HOME/.m2/repository/com/beust/jcommander/1.72/jcommander-1.72.jar:$HOME/.m2/repository/com/github/jai-imageio/jai-imageio-core/1.3.1/jai-imageio-core-1.3.1.jar com.google.zxing.client.j2se.CommandLineRunner JD.png

The below image shows the output produced by this command.

下图显示了此命令产生的输出。

GitHub Repository. GitHub Repository下载QR Code Generator和Reader maven项目。

翻译自: https://www.journaldev.com/470/java-qr-code-generator-zxing-example

 类似资料: