今天我们将研究Java QR 码生成器程序。如果您精通技术和小工具,那么您一定知道二维码。如今,您随处都可以找到它 - 在博客、网站,甚至在一些公共场所。这在移动应用程序中非常流行,您可以使用 QR 码扫描仪应用程序扫描 QR 码,它会显示文本或将您重定向到网页(如果是 URL)。我最近发现了这个并发现它非常有趣。如果你想了解二维码,你可以在维基百科上找到很多有用的信息二维码页.
当我在这么多网站上找到QR码图像时,我开始寻找java QR码生成器。我研究了一些开源 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>
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.
这是您可以使用 zxing API 创建 QR 码图像的程序。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.
如果您没有移动应用程序来测试它,请不要担心。您可以通过命令行使用 zxing API 读取二维码。以下是读取二维码图像文件的命令。请注意 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.
您可以从我们的网站下载 QR 码生成器和阅读器 maven 项目GitHub 存储库.