Java QR 码生成器 - 中兴范例

今天我们将看看JavaQR代码生成器程序. 如果你是技术和工具熟练,那么你必须知道QR代码. 你会发现它在这些日子 - 在博客,网站,甚至在一些公共场所. 这在移动应用程序中非常受欢迎,你扫描QR代码使用QR代码扫描应用程序,它会向你显示文本或重定向你到网页,如果它是URL. 我最近遇到这个,并发现它非常有趣。如果你想了解QR代码,你可以找到很多有用的信息在维基百科(QR代码页面)(https://en.wikipedia.org/wiki/QR_code)。

Java QR代码生成器

当我在许多网站上找到QR代码图像时,我开始寻找JavaQR代码生成器。我检查了一些开源API,并发现zxing是最简单和最好使用的。

1<dependency>
2    <groupId>com.google.zxing</groupId>
3    <artifactId>core</artifactId>
4    <version>3.3.2</version>
5</dependency>

如果你想通过命令行阅读QR图像,那么我们需要使用它的JavaSE库。

1<dependency>
2    <groupId>com.google.zxing</groupId>
3    <artifactId>javase</artifactId>
4    <version>3.3.2</version>
5</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 maven dependencies

zxing 示例生成 QR 代码图像

以下是您可以使用的程序来创建 QR 代码图像与 zxing API. GenerateQRCode.java

 1package com.journaldev.qrcode.generator;
 2
 3import java.awt.Color;
 4import java.awt.Graphics2D;
 5import java.awt.image.BufferedImage;
 6import java.io.File;
 7import java.io.IOException;
 8import java.util.Hashtable;
 9
10import javax.imageio.ImageIO;
11
12import com.google.zxing.BarcodeFormat;
13import com.google.zxing.EncodeHintType;
14import com.google.zxing.WriterException;
15import com.google.zxing.common.BitMatrix;
16import com.google.zxing.qrcode.QRCodeWriter;
17import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
18
19public class GenerateQRCode {
20
21    public static void main(String[] args) throws WriterException, IOException {
22    	String qrCodeText = "https://www.journaldev.com";
23    	String filePath = "JD.png";
24    	int size = 125;
25    	String fileType = "png";
26    	File qrFile = new File(filePath);
27    	createQRImage(qrFile, qrCodeText, size, fileType);
28    	System.out.println("DONE");
29    }
30
31    private static void createQRImage(File qrFile, String qrCodeText, int size, String fileType)
32    		throws WriterException, IOException {
33    	// Create the ByteMatrix for the QR-Code that encodes the given String
34    	Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<>();
35    	hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
36    	QRCodeWriter qrCodeWriter = new QRCodeWriter();
37    	BitMatrix byteMatrix = qrCodeWriter.encode(qrCodeText, BarcodeFormat.QR_CODE, size, size, hintMap);
38    	// Make the BufferedImage that are to hold the QRCode
39    	int matrixWidth = byteMatrix.getWidth();
40    	BufferedImage image = new BufferedImage(matrixWidth, matrixWidth, BufferedImage.TYPE_INT_RGB);
41    	image.createGraphics();
42
43    	Graphics2D graphics = (Graphics2D) image.getGraphics();
44    	graphics.setColor(Color.WHITE);
45    	graphics.fillRect(0, 0, matrixWidth, matrixWidth);
46    	// Paint and save the image using the ByteMatrix
47    	graphics.setColor(Color.BLACK);
48
49    	for (int i = 0; i < matrixWidth; i++) {
50    		for (int j = 0; j < matrixWidth; j++) {
51    			if (byteMatrix.get(i, j)) {
52    				graphics.fillRect(i, j, 1, 1);
53    			}
54    		}
55    	}
56    	ImageIO.write(image, fileType, qrFile);
57    }
58
59}

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. java qr code generator, zxing example

zxing 示例阅读 QR 代码

如果您没有移动应用程序来测试它,不要担心. 您可以通过命令行通过zxing API读取QR代码。 下面是读取QR代码图像文件的命令。

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

您可以从我们的 GitHub 存储库下载 QR Code Generator and Reader maven 项目。

Published At
Categories with 技术
Tagged with
comments powered by Disqus