springboot生成二维码到海报模板上
QRCodeController
package com.ruoyi.web.controller.app;
import com.google.zxing.WriterException;
import com.ruoyi.app.domain.Opportunity;
import com.ruoyi.app.tool.QRCodeGenerator;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/app/qrcCode")
@Api(tags = "推广海报接口")
public class QRCodeController {
@Autowired
private QRCodeGenerator qrCodeGenerator;
@ApiOperation(value = "获取推广海报", notes = "获取推广海报")
@GetMapping("/qrImage")
public void generateQRCode(HttpServletResponse response, @RequestParam Map<String, String> params) throws Exception {
String openid = params.get("openid");
String xh = params.get("xh");
if (openid == null || openid.isEmpty()) {
throw new Exception("openid不能为空");
}
if (xh == null || xh.isEmpty()) {
xh = "1";
}
// 改为自己的链接
String url = "http://*******.cn/app/index/?parentId=" + openid;
byte[] qrCodeBytes = qrCodeGenerator.generateQRCode(url, 290, 290, xh);
response.setContentType(MediaType.IMAGE_PNG_VALUE);
response.setHeader("Content-Disposition", "inline; filename=qrCode.png");
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "no-cache");
response.setContentLength(qrCodeBytes.length);
try (OutputStream out = response.getOutputStream()) {
out.write(qrCodeBytes);
}
}
}
QRCodeGenerator
package com.ruoyi.app.tool;
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 org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Component
public class QRCodeGenerator {
public byte[] generateQRCode(String url, int qrCodeWidth, int qrCodeHeight, String xh) throws WriterException, IOException {
// 生成二维码
QRCodeWriter qrCodeWriter = new QRCodeWriter();
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 设置容错级别为高
BitMatrix bitMatrix = qrCodeWriter.encode(url, BarcodeFormat.QR_CODE, qrCodeWidth, qrCodeHeight, hints);
BufferedImage qrCodeImage = bitMatrixToImage(bitMatrix);
// 加载背景图片
ClassPathResource backgroundImageResource = new ClassPathResource("static/" + xh + ".jpg");
BufferedImage backgroundImage = ImageIO.read(backgroundImageResource.getInputStream());
// 创建包含背景图片的画布
int combinedImageWidth = backgroundImage.getWidth();
int combinedImageHeight = backgroundImage.getHeight();
BufferedImage combinedImage = new BufferedImage(combinedImageWidth, combinedImageHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D combinedGraphics = combinedImage.createGraphics();
combinedGraphics.drawImage(backgroundImage, 0, 0, null);
// 将二维码放在背景图的左下角
int qrCodeX = 930; // 二维码在背景图片中的X坐标
int qrCodeY = combinedImageHeight - qrCodeHeight - 20; // 二维码在背景图片中的Y坐标
// 添加红色边框
int borderWidth = 2; // 边框宽度
Color borderColor = Color.decode("#B11E4C"); // 边框颜色
combinedGraphics.setColor(new Color(borderColor.getRed(), borderColor.getGreen(), borderColor.getBlue(), 0)); // 设置填充颜色为透明色
combinedGraphics.fillRoundRect(qrCodeX - borderWidth, qrCodeY - borderWidth, qrCodeWidth + 2 * borderWidth, qrCodeHeight + 2 * borderWidth, 5, 5);
combinedGraphics.setColor(borderColor); // 设置边框颜色
combinedGraphics.drawRoundRect(qrCodeX - borderWidth, qrCodeY - borderWidth, qrCodeWidth + 2 * borderWidth, qrCodeHeight + 2 * borderWidth, 5, 5); // 绘制边框
combinedGraphics.drawImage(qrCodeImage, qrCodeX, qrCodeY, null);
// 将合成的图片转为字节数组
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(combinedImage, "png", byteArrayOutputStream);
return byteArrayOutputStream.toByteArray();
}
private BufferedImage bitMatrixToImage(BitMatrix bitMatrix) {
int width = bitMatrix.getWidth();
int height = bitMatrix.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, bitMatrix.get(x, y) ? Color.BLACK.getRGB() : Color.WHITE.getRGB());
}
}
return image;
}
}
海报模板存放(不带二维码的图片):
把地址放到白名单中,无需token可以访问,然后就得到: