本文目录导读:

我来介绍几种Java生成二维码的常用实现方法:
使用ZXing库(推荐)
添加Maven依赖
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.5.1</version>
</dependency>
基本二维码生成代码
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.client.j2se.MatrixToImageWriter;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
public class QRCodeGenerator {
/**
* 生成二维码到文件
*/
public static void generateQRCodeToFile(String content, String filePath,
int width, int height) throws Exception {
// 设置二维码参数
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 = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
// 写入文件
File outputFile = new File(filePath);
MatrixToImageWriter.writeToPath(bitMatrix, "png", outputFile.toPath());
}
/**
* 生成二维码到输出流
*/
public static void generateQRCodeToStream(String content, OutputStream outputStream,
int width, int height) throws Exception {
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 = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
MatrixToImageWriter.writeToStream(bitMatrix, "png", outputStream);
}
/**
* 生成二维码为BufferedImage
*/
public static BufferedImage generateQRCodeImage(String content, int width, int height)
throws Exception {
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 = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
return MatrixToImageWriter.toBufferedImage(bitMatrix);
}
}
带Logo的二维码
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
public class QRCodeWithLogoGenerator {
/**
* 生成带Logo的二维码
*/
public static BufferedImage generateQRCodeWithLogo(String content, String logoPath,
int width, int height) throws Exception {
// 先生成普通二维码
BufferedImage qrImage = QRCodeGenerator.generateQRCodeImage(content, width, height);
// 读取Logo图片
BufferedImage logoImage;
if (logoPath.startsWith("http")) {
logoImage = ImageIO.read(new URL(logoPath));
} else {
logoImage = ImageIO.read(new File(logoPath));
}
// 计算Logo大小(二维码的20%)
int logoSize = Math.min(width, height) / 5;
int logoX = (width - logoSize) / 2;
int logoY = (height - logoSize) / 2;
// 创建画板
Graphics2D graphics = qrImage.createGraphics();
// 绘制Logo背景(白色圆角矩形)
graphics.setComposite(AlphaComposite.SrcOver);
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setColor(Color.WHITE);
graphics.fillRoundRect(logoX - 2, logoY - 2, logoSize + 4, logoSize + 4, 15, 15);
// 绘制Logo
graphics.drawImage(logoImage, logoX, logoY, logoSize, logoSize, null);
// 绘制Logo边框
graphics.setColor(Color.LIGHT_GRAY);
graphics.drawRoundRect(logoX - 2, logoY - 2, logoSize + 4, logoSize + 4, 15, 15);
graphics.dispose();
qrImage.flush();
return qrImage;
}
}
使用qrcode库
添加依赖
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>qrcode</artifactId>
<version>3.5.1</version>
</dependency>
简单实现
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class SimpleQRCodeGenerator {
public static void main(String[] args) {
try {
String content = "https://www.example.com";
String filePath = "qrcode.png";
// 生成二维码
QRCodeGenerator.generateQRCodeToFile(content, filePath, 300, 300);
System.out.println("二维码生成成功:" + filePath);
// 生成带Logo的二维码
BufferedImage qrWithLogo = QRCodeWithLogoGenerator.generateQRCodeWithLogo(
content, "logo.png", 300, 300
);
ImageIO.write(qrWithLogo, "png", new File("qrcode_with_logo.png"));
System.out.println("带Logo的二维码生成成功");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Web接口生成二维码
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
@RestController
public class QRCodeController {
@GetMapping("/generateQRCode")
public void generateQRCode(@RequestParam String content,
@RequestParam(defaultValue = "300") int width,
@RequestParam(defaultValue = "300") int height,
HttpServletResponse response) {
try {
response.setContentType("image/png");
QRCodeGenerator.generateQRCodeToStream(content, response.getOutputStream(),
width, height);
} catch (Exception e) {
e.printStackTrace();
}
}
}
二维码转Base64(用于前端展示)
import java.util.Base64;
import javax.imageio.ImageIO;
import java.io.ByteArrayOutputStream;
public class QRCodeToBase64 {
public static String generateQRCodeBase64(String content, int width, int height)
throws Exception {
BufferedImage qrImage = QRCodeGenerator.generateQRCodeImage(content, width, height);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(qrImage, "png", baos);
byte[] imageBytes = baos.toByteArray();
return Base64.getEncoder().encodeToString(imageBytes);
}
}
使用示例
public class Main {
public static void main(String[] args) {
try {
// 1. 基本二维码
QRCodeGenerator.generateQRCodeToFile(
"https://www.example.com",
"output/basic_qrcode.png",
300, 300
);
// 2. 带Logo二维码
BufferedImage logoQR = QRCodeWithLogoGenerator.generateQRCodeWithLogo(
"https://www.example.com",
"logo.png",
300, 300
);
ImageIO.write(logoQR, "png", new File("output/logo_qrcode.png"));
// 3. 生成Base64
String base64 = QRCodeToBase64.generateQRCodeBase64(
"Hello World", 200, 200
);
System.out.println("Base64: " + base64);
} catch (Exception e) {
e.printStackTrace();
}
}
}
注意事项
- 字符编码:一定要设置UTF-8编码,避免中文乱码
- 纠错等级:建议使用H级(最高),可以包含更多信息但二维码会更复杂
- 大小设置:根据实际应用场景设置合适的尺寸
- Logo大小:Logo过大会影响二维码识别,建议不超过二维码的20%
- 性能优化:频繁生成二维码时可以考虑缓存机制
这些代码可以直接复制使用,根据你的具体需求选择合适的实现方式。
版权声明:足球直播
足球直播
足球直播
足球直播
足球直播
中超直播
中超直播
中超直播
中超直播
中超直播
英超直播
英超直播
英超直播
英超直播
英超直播
德甲直播
德甲直播
德甲直播
德甲直播
德甲直播
西甲直播
西甲直播
西甲直播
西甲直播
西甲直播
意甲直播
意甲直播
意甲直播
意甲直播
意甲直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
足球直播
足球直播
足球直播
足球直播
足球直播
中超直播
中超直播
中超直播
中超直播
中超直播
英超直播
英超直播
英超直播
英超直播
英超直播
德甲直播
德甲直播
德甲直播
德甲直播
德甲直播
西甲直播
西甲直播
西甲直播
西甲直播
西甲直播
意甲直播
意甲直播
意甲直播
意甲直播
意甲直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
足球直播
足球直播
足球直播
足球直播
足球直播
中超直播
中超直播
中超直播
中超直播
中超直播
英超直播
英超直播
英超直播
英超直播
英超直播
德甲直播
德甲直播
德甲直播
德甲直播
德甲直播
西甲直播
西甲直播
西甲直播
西甲直播
西甲直播
意甲直播
意甲直播
意甲直播
意甲直播
意甲直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播