Java二维码生成器 – zxing示例

今天我们将来研究Java二维码生成程序。如果你对科技和小工具很熟悉,那么你一定知道二维码。现如今,你几乎无处不见它——在博客、网站甚至一些公共场所。在移动应用中,它非常流行,你可以使用二维码扫描器应用程序扫描二维码,它会显示文本或者将你重定向到网页,如果它是一个URL的话。最近我遇到了这个东西,发现它非常有趣。如果你想了解有关二维码的信息,你可以在维基百科的二维码页面找到很多有用的信息。

Java 二维码生成器

当我在许多网站上发现了二维码图像时,我开始寻找Java二维码生成器。我研究了一些开源API,并发现zxing是最简单且最好用的。如果您想要生成一个二维码图像,我们只需要其核心库。只需将以下依赖项添加到您的Maven项目中即可。

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

如果你想通过命令行读取QR图像,那么我们需要使用它的JavaSE库。你可以添加以下依赖项。

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

用zxing的示例生成二维码图像。

这是一个您可以使用zxing API创建QR码图像的程序。GenerateQRCode.java

package com.Olivia.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.scdev.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);
	}

}

这是这个程序创建的二维码图片文件。您可以使用您的手机二维码扫描器应用程序来测试。它应该指向JournalDev主页的URL。

使用ZXing示例来读取二维码

如果您没有移动应用程序来进行测试,不用担心。您可以通过命令行使用zxing API读取QR码。下面是用于读取QR码图像文件的命令。请注意,zxing所依赖的附加jars在classpath中。

$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
zxing read qr code image

您可以从我们的GitHub存储库下载QR码生成器和阅读器的maven项目。

发表回复 0

Your email address will not be published. Required fields are marked *