springboot 批量生成二维码
时间: 2023-09-25 09:14:58 浏览: 182
java生成二维码的demo(基于springboot)
可以使用Google开源的Zxing库来生成二维码,以下是基于Spring Boot的批量生成二维码的示例代码:
1. 引入依赖
在pom.xml中添加以下依赖:
```xml
<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>
```
2. 编写生成二维码的工具类
```java
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class QrCodeUtils {
private static final int WIDTH = 300; // 二维码宽度
private static final int HEIGHT = 300; // 二维码高度
private static final String FORMAT = "png"; // 二维码格式
private static final String CHARSET = "UTF-8"; // 字符集
private static final int MARGIN = 1; // 边距
/**
* 生成二维码
*
* @param content 二维码内容
* @param file 二维码文件
* @throws Exception
*/
public static void generateQRCode(String content, File file) throws Exception {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, WIDTH, HEIGHT,
getDecodeHintType());
BufferedImage image = toBufferedImage(bitMatrix);
ImageIO.write(image, FORMAT, file);
}
/**
* 批量生成二维码
*
* @param contents 二维码内容列表
* @param dir 二维码目录
* @throws Exception
*/
public static void batchGenerateQRCode(List<String> contents, File dir) throws Exception {
if (!dir.exists()) {
dir.mkdirs();
}
for (int i = 0; i < contents.size(); i++) {
String content = contents.get(i);
String fileName = "qrcode_" + (i + 1) + "." + FORMAT;
File file = new File(dir, fileName);
generateQRCode(content, file);
}
}
/**
* 获取编码提示类型
*
* @return
*/
private static EncodeHintType getDecodeHintType() {
EncodeHintType encodeHintType = EncodeHintType.ERROR_CORRECTION;
ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.H;
return encodeHintType;
}
/**
* BitMatrix转BufferedImage
*
* @param matrix
* @return
*/
private static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
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, matrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
}
}
return image;
}
}
```
3. 调用工具类批量生成二维码
```java
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class QrCodeTest {
public static void main(String[] args) throws Exception {
List<String> contents = new ArrayList<>();
contents.add("http://www.baidu.com");
contents.add("http://www.google.com");
contents.add("http://www.github.com");
File dir = new File("D:/qrcode");
QrCodeUtils.batchGenerateQRCode(contents, dir);
}
}
```
以上代码会在D:/qrcode目录下生成三个二维码文件,内容分别为http://www.baidu.com、http://www.google.com、http://www.github.com。
阅读全文