博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java QR Code Generator – zxing示例
阅读量:2530 次
发布时间:2019-05-11

本文共 5167 字,大约阅读时间需要 17 分钟。

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,可以在Wikipedia 找到很多有用的信息。

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 to be the simple and best to use.

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

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项目中添加以下依赖项即可。

com.google.zxing
core
3.3.2

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库。 您可以为其添加以下依赖项。

com.google.zxing
javase
3.3.2

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
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.

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

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

翻译自:

转载地址:http://koozd.baihongyu.com/

你可能感兴趣的文章
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_43、SpringBoot2.x异步任务实战(核心知识)...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_01课程简介
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第11节 Logback日志框架介绍和SpringBoot整合实战_45、SpringBoot2.x日志讲解和Logback配置实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_02技术选型
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_汇总
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_01传统架构演进到分布式架构
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_02 微服务核心基础讲解
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_04微服务下电商项目基础模块设计...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-01 什么是微服务的注册中心
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-03CAP原理、常见面试题
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-04 SpringCloud微服务核心组件Eureka介绍和闭源后影响...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-05 服务注册和发现Eureka Server搭建实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-06 服务注册和发现之Eureka Client搭建商品服务实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-07 Eureka服务注册中心配置控制台问题处理...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-01 常用的服务间调用方式讲解
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-02 微服务调用方式之ribbon实战 订单调用商品服务...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-03 高级篇幅之Ribbon负载均衡源码分析实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-06 Feign核心源码解读和服务调用方式ribbon和Feign选择...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-05 微服务调用方式之feign 实战 订单调用商品服务...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-02 Netflix开源组件断路器
查看>>