要在 Java 中根据 URL 生成二维码并在二维码中间添加 logo,可以使用 ZXing 库来生成二维码,并使用图像处理库(如 Java 的 BufferedImage)来添加 logo。以下是详细的步骤和示例代码:
步骤
生成二维码:使用 ZXing 库生成二维码。
加载 logo 图片:加载需要添加到二维码中间的 logo 图片。
合并图片:将 logo 图片合并到二维码图片中。
示例代码
首先,确保你已经添加了 ZXing 库的依赖。如果你使用 Maven,可以在 pom.xml 文件中添加以下依赖:
<dependencies>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.4.1</version>
</dependency>
</dependencies>
接下来是生成二维码并添加 logo 的完整示例代码:
package com.ruoyi.common.utils.file;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
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;
/**
* @author biypm
* @version 1.0
* @title
* @date 2024/10/5/周六 10:24
*/
public class QRCodeUtils {
public static void main(String[] args) {
String url = "https://www.baidu.com";
int width = 300;
int height = 300;
String format = "png";
String logoPath = System.getProperty("user.dir") + "src/assets/logo/logo.jpg"; // 你的 logo 图片路径
try {
BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, width, height, getHints());
BufferedImage qrCodeImage = matrixToImage(bitMatrix);
BufferedImage logoImage = ImageIO.read(new File(logoPath));
BufferedImage finalImage = addLogoToQRCode(qrCodeImage, logoImage);
ImageIO.write(finalImage, format, new File("qrcode_with_logo.png"));
} catch (WriterException | IOException e) {
e.printStackTrace();
}
}
private static Map<EncodeHintType, ErrorCorrectionLevel> getHints() {
Map<EncodeHintType, ErrorCorrectionLevel> hints = new HashMap<>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
return hints;
}
private static BufferedImage matrixToImage(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) ? Color.BLACK.getRGB() : Color.WHITE.getRGB());
}
}
return image;
}
private static BufferedImage addLogoToQRCode(BufferedImage qrCodeImage, BufferedImage logoImage) {
if (qrCodeImage == null || logoImage == null) {
throw new IllegalArgumentException("qrCodeImage and logoImage must not be null");
}
int qrWidth = qrCodeImage.getWidth();
int qrHeight = qrCodeImage.getHeight();
// 减小Logo大小
int newLogoWidth = qrWidth / 8;
int newLogoHeight = qrHeight / 8;
BufferedImage resizedLogo = new BufferedImage(newLogoWidth, newLogoHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = resizedLogo.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(logoImage, 0, 0, newLogoWidth, newLogoHeight, null);
g2.dispose();
// 创建最终的图像
BufferedImage finalImage = new BufferedImage(qrWidth, qrHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = finalImage.createGraphics();
g.drawImage(qrCodeImage, 0, 0, null);
// 计算Logo在二维码中的位置
int x = (qrWidth - newLogoWidth) / 2;
int y = (qrHeight - newLogoHeight) / 2;
// 绘制Logo到二维码中心
g.drawImage(resizedLogo, x, y, null);
// 设置透明度
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.3f)); // 降低透明度
// 绘制圆角矩形边框
g.setColor(Color.WHITE);
g.setStroke(new BasicStroke(3f)); // 减小边框宽度
g.drawRoundRect(x, y, newLogoWidth, newLogoHeight, 15, 15); // 减小圆角半径
g.dispose();
return finalImage;
}
}