一、效果图

二、代码
- 使用的是zxing所以需要2个依赖,下面是pom.xml的配置:
<properties>
<zxing.version>3.3.1</zxing.version>
<zxing.javase.version>3.1.0</zxing.javase.version>
</properties>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>${zxing.version}</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>${zxing.javase.version}</version>
</dependency>
- 完整代码ZXingBackGroundUtils.java:
package com.zzq.test.zxing;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
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.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class ZXingBackGroundUtils {
private static final int QRCOLOR = 0xFF000000;
private static final int BGWHITE = 0xFFFFFFFF;
private static final int WIDTH = 200;
private static final int HEIGHT = 200;
private static Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() {
private static final long serialVersionUID = 1L;
{
put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
put(EncodeHintType.CHARACTER_SET, "utf-8");
put(EncodeHintType.MARGIN, 0);
}
};
public static void drawLogoQRCode(File logoFile, File codeFile, String qrUrl) {
try {
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
BitMatrix bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
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, bm.get(x, y) ? QRCOLOR : BGWHITE);
}
}
int width = image.getWidth();
int height = image.getHeight();
if (Objects.nonNull(logoFile) && logoFile.exists()) {
Graphics2D g = image.createGraphics();
BufferedImage logo = ImageIO.read(logoFile);
g.drawImage(logo, width * 2 / 5, height * 2 / 5, width * 2 / 10, height * 2 / 10, null);
g.dispose();
logo.flush();
}
image.flush();
ImageIO.write(image, "png", codeFile);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
File logoFile = new File("C://Users//Administrator//Desktop//2.jpg");
File QrCodeFile = new File("C://Users//Administrator//Desktop//test.jpg");
String url = "https://www.baidu.com/";
drawLogoQRCode(logoFile, QrCodeFile, url);
mergeImage("C://Users//Administrator//Desktop//1.jpg", "C://Users//Administrator//Desktop//test.jpg", "C://Users//Administrator//Desktop//test2.jpg","63", "163");
}
public static void mergeImage(String bigPath, String smallPath, String newFilePath,String x, String y) throws IOException {
try {
BufferedImage small;
BufferedImage big;
if (bigPath.contains("http://") || bigPath.contains("https://")) {
URL url = new URL(bigPath);
big = ImageIO.read(url);
} else {
big = ImageIO.read(new File(bigPath));
}
if (smallPath.contains("http://") || smallPath.contains("https://")) {
URL url = new URL(smallPath);
small = ImageIO.read(url);
} else {
small = ImageIO.read(new File(smallPath));
}
Graphics2D g = big.createGraphics();
float fx = Float.parseFloat(x);
float fy = Float.parseFloat(y);
int x_i = (int) fx;
int y_i = (int) fy;
g.drawImage(small, x_i, y_i, small.getWidth(), small.getHeight(), null);
g.dispose();
ImageIO.write(big, "png", new File(newFilePath));
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 主要2个方法:
- 1、drawLogoQRCode生成二维码
- 2、mergeImage给二维码嵌套背景合并图片
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现