引入依赖包
<!-- 二维码 ZXing -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.5.1</version> <!-- 请使用最新版本 -->
</dependency>
ZXing,全名为"Zebra Crossing",是一个开源的Java库,用于二维码的生成和解析。可以用于生成QR码以及解析包括QR码在内的多种二维码格式,除了QR码,ZXing还支持解析其他一维码和二维码
生成二维码
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* 生成QR码图片
*
* @param data 要存储在QR码中的数据,可以是文本、URL等
* @param width QR码的宽度(像素)
* @param height QR码的高度(像素)
*/
public static BufferedImage createImage(String data, int width, int height) {
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 设置字符编码
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 错误纠正级别
hints.put(EncodeHintType.MARGIN, 1); // 二维码边距
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix bitMatrix = null;
try {
bitMatrix = writer.encode(data, BarcodeFormat.QR_CODE, width, height, hints);
} catch (WriterException e) {
throw new RuntimeException(e);
}
// 创建BufferedImage对象来表示QR码
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? Color.BLACK.getRGB() : Color.WHITE.getRGB());
}
}
return image;
}
输出二维码图片
http请求输出
在平时开发中,大多是都是前端请求接口,返回二维码图片,这时需要使用HttpServletResponse的输出流返回给前端
/**
* 响应接口输出二维码
* @param data 要存储在QR码中的数据
* @param response
* @throws Exception
*/
public static void encode(String data, HttpServletResponse response)
throws Exception {
BufferedImage image = createImage(data,200,200);
ImageIO.write(image, "png", response.getOutputStream());
}
控制层提供接口
@GetMapping("qrCode")
public void qrcode(HttpServletResponse response) throws Exception {
String data = "**********";
QRcodeTools.encode(data, response);
}
本地输出
在本地电脑上生成一个二维码图片
/**
* 本地输出二维码
* @param data
* @param filePath
* @throws Exception
*/
public static void localEncode(String data,String filePath) {
BufferedImage image = createImage(data,200,200);
File qrCodeFile = new File(filePath);
try {
ImageIO.write(image, "png", qrCodeFile);
} catch (IOException e) {
System.out.println("写入失败");
}
}
public static void main(String[] args) {
String data = "https://www.baidu.com/";// 内容
String filePath1 = "C:\\Users\\HouPru\\Desktop\\QR码.png";// QR码存放路径
localEncode(data,filePath1);
}
条形码
/**
* 生成条形码
* @param data 要存储在条形码中的数据,可以是商品条形码等
* @param width 条形码的宽度(像素)
* @param height 条形码的高度(像素)
*/
public static BufferedImage createImageBar(String data, int width, int height) {
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 设置字符编码
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix bitMatrix = null;
try {
bitMatrix = writer.encode(data, BarcodeFormat.CODE_128, width, height, hints);
} catch (WriterException e) {
throw new RuntimeException(e);
}
// 创建BufferedImage对象来表示条形码
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? 0 : 0xFFFFFF); // 生成黑色条和白色背景的条形码
}
}
return image;
}