需求:制作出多张二维码,并且压缩后下载。下载下来的压缩包解压后二维码能正常扫码
引入二维码制作的jar包
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.3</version>
</dependency>
二维码制作工具类
package com.demo.utils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class ZxingUtils {
/**
* 生成二维码
*
* @param contents 二维码内容
* @param width 图片宽度
* @param height 图片高度
* @param hints 二维码相关参数
* @return BufferedImage对象
* @throws WriterException 编码时出错
* @throws IOException 写入文件出错
*/
public static BufferedImage enQRCode(String contents, int width, int height, Map<EncodeHintType, ?> hints) throws WriterException {
// String uuid = UUID.randomUUID().toString().replace("-", "");
//本地完整路径
// String pathname = path + "/" + uuid + "." + format;
//生成二维码
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
// Path file = new File(pathname).toPath();
//将二维码保存到路径下
// MatrixToImageWriter.writeToPa(bitMatrix, format, file);
// return pathname;
return MatrixToImageWriter.toBufferedImage(bitMatrix);
}
public static BufferedImage enQRCode(String contents, int width, int height) throws WriterException {
//定义二维码参数
Map<