1.依赖
<!--二维码-->
<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.代码
package com.example.test_demo.or_code;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import lombok.Builder;
import lombok.Data;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
public class QRCodeGenerator {
public static void main(String[] args) {
String url = "http://www.sgbox.ai/?invate=07549892";
try {
createORCode build = createORCode.builder()
.url(url)
.width(350)
.height(350)
.sourcePath("C:\\Users\\lenovo\\Desktop\\宣传图.png")
.writing("恭喜233333333")
.writingPathX(20)
.writingPathY(20)
.targetPathX(50)
.targetPathY(50)
.fontType("SimSun")
.fontStyle(Font.BOLD)
.fontSize(20)
.build();
byte[] imageBytes = generateQRCodeImage(build);
System.out.println("二维码生成并合成成功!");
} catch (WriterException | IOException e) {
System.err.println("二维码生成失败:" + e.getMessage());
}
}
/**
* 生成二维码并合成图片,返回字节数组
* @return 组合图 byte数组
*/
public static byte[] generateQRCodeImage(createORCode orCode)
throws WriterException, IOException {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix bitMatrix = qrCodeWriter.encode(orCode.getUrl(), BarcodeFormat.QR_CODE, orCode.getWidth(), orCode.getHeight());
BufferedImage qrImage = makeQRCodeTransparent(bitMatrix, orCode.getWidth(), orCode.getHeight());
return toBufferedImage(orCode.getSourcePath(), qrImage, orCode.getWriting(), orCode.getTargetPathX(), orCode.targetPathY, orCode.fontType, orCode.fontStyle, orCode.getFontSize(), orCode.getWritingPathX(), orCode.getWritingPathY());
}
/**
* 将二维码的白色背景变为透明
*/
private static BufferedImage makeQRCodeTransparent(BitMatrix bitMatrix, int width, int height) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (bitMatrix.get(x, y)) {
image.setRGB(x, y, Color.BLACK.getRGB());
} else {
image.setRGB(x, y, new Color(0, 0, 0, 0).getRGB());
}
}
}
return image;
}
/**
* 合成二维码到背景图片上,并返回合成后的字节数组
*
* @param sourcePath 背景图片路径
* @param qrImage 生成的二维码图片
* @param writing 添加的文字
* @param targetPathX 二维码x轴位置
* @param targetPathY 二维码y轴位置
* @param fontType 字体类型
* @param fontStyle 字体样式
* @param fontSize 字体大小
* @param writingPathX 文字x轴位置
* @param writingPathY 文字y轴位置
* @return 合成后的图片字节数组
*/
public static byte[] toBufferedImage(String sourcePath, BufferedImage qrImage, String writing,
int targetPathX, int targetPathY, String fontType, int fontStyle,
int fontSize, int writingPathX, int writingPathY) {
try {
BufferedImage source = ImageIO.read(new File(sourcePath));
// 在背景图片上绘制二维码
Graphics2D g2d = source.createGraphics();
g2d.drawImage(qrImage, targetPathX, targetPathY, null);
// 添加文字
g2d.setFont(new Font(fontType, fontStyle, fontSize));
g2d.setColor(Color.RED);
g2d.drawString(writing, writingPathX, writingPathY);
g2d.dispose();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(source, "png", baos);
ImageIO.write(source, "png", new File("C:\\Users\\lenovo\\Desktop\\output.jpg"));
baos.flush();
byte[] imageBytes = baos.toByteArray();
baos.close();
return imageBytes;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Data
@Builder
public static class createORCode{
// 网址
private String url;
// 二维码宽度
private int width;
// 二维码高度
private int height;
// 背景图片位置
private String sourcePath;
// 添加的文字
private String writing;
// 二维码x轴位置
private int targetPathX;
// 二维码y轴位置
private int targetPathY;
// 字体类型
private String fontType;
// 字体样式
private int fontStyle;
// 字体大小
private int fontSize;
// 文字x轴位置
private int writingPathX;
// 文字y轴位置
private int writingPathY;
}
}