Java二维码
二维码工具
- ZXing (Zebra Crossing):Google 出品,经典老牌,稳定可靠。
- QrCode (来自 com.google.zxing.qrcode):轻量级,适合快速开发。
- QRCodeUtils (封装工具类):自己撸一点封装,方便复用。
1. 引入 Maven 依赖
com.google.zxing
core
3.5.2
com.google.zxing
javase
3.5.2
2. 封装一个二维码工具类
package com.moyu.qr;
import com.google.zxing.*;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
/**
* 天天摸鱼的 Java 工程师出品
* 通用二维码生成工具类
*/
publicclass QrCodeUtils {
/**
* 生成普通二维码并保存为图片文件
*
* @param content 二维码内容
* @param filePath 保存路径(绝对路径)
* @param width 宽度
* @param height 高度
* @throws Exception
*/
public static void generateSimpleQrCode(String content, String filePath, int width, int height) throws Exception {
// 编码参数设置
Map hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 编码类型
hints.put(EncodeHintType.MARGIN, 1); // 边距
BitMatrix bitMatrix = new MultiFormatWriter().encode(
content,
BarcodeFormat.QR_CODE,
width,
height,
hints
);
Path path = new File(filePath).toPath();
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
}
/**
* 生成带 Logo 的二维码
*
* @param content 二维码内容
* @param logoPath logo 图片路径
* @param outputPath 输出二维码路径
*/
public static void generateQrCodeWithLogo(String content, String logoPath, String outputPath) throws Exception {
int width = 300;
int height = 300;
Map hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.MARGIN, 1);
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 容错率高,避免 logo 遮挡
BitMatrix bitMatrix = new MultiFormatWriter().encode(
content, BarcodeFormat.QR_CODE, width, height, hints
);
// 生成二维码图像
BufferedImage qrImage = MatrixToImageWriter.toBufferedImage(bitMatrix, new MatrixToImageConfig());
// 加载 logo 图片
BufferedImage logo = ImageIO.read(new File(logoPath));
int logoWidth = qrImage.getWidth() / 5;
int logoHeight = qrImage.getHeight() / 5;
// 计算 logo 放置位置(居中)
int x = (qrImage.getWidth() - logoWidth) / 2;
int y = (qrImage.getHeight() - logoHeight) / 2;
// 合并图片
Graphics2D g = qrImage.createGraphics();
g.drawImage(logo, x, y, logoWidth, logoHeight, null);
g.dispose();
ImageIO.write(qrImage, "PNG", new File(outputPath));
}
}
3. 实战调用:生成一个带参数的推广码
public class Main {
public static void main(String[] args) throws Exception {
String content = "https://yourdomain.com/register?ref=userid_123456";
String savePath = "D:/qrcode/promo.png";
QrCodeUtils.generateSimpleQrCode(content, savePath, 300, 300);
System.out.println("推广二维码生成成功,快去扫码看看!");
}
}
4. 带 Logo 的品牌码
public class LogoDemo {
public static void main(String[] args) throws Exception {
String content = "https://yourapp.com/share?id=abc123";
String logo = "D:/logo/logo.png";
String output = "D:/qrcode/share_with_logo.png";
QrCodeUtils.generateQrCodeWithLogo(content, logo, output);
System.out.println("带 Logo 的二维码已生成!");
}
}

浙公网安备 33010602011771号