fix:修改模板渲染

This commit is contained in:
2025-03-19 12:33:35 +08:00
parent da5c10b632
commit df17c286fe
4 changed files with 49 additions and 47 deletions

View File

@@ -145,8 +145,8 @@ public class TemplateUtils {
String[] split = key.split("_");
if (split.length > 1 && split[1].equals("2base64Type")) {
int type = split.length > 2 ? Integer.parseInt(split[2]) : 1;
int width = split.length > 3 ? Integer.parseInt(split[3]) : 100;
int height = split.length > 4 ? Integer.parseInt(split[4]) : 100;
int width = split.length > 2 ? Integer.parseInt(split[3]) : 100;
int height = split.length > 3 ? Integer.parseInt(split[4]) : 100;
// 如果是图片类型,需要进行base64转换
String base64 = this.generateQRCode(type, String.valueOf(data.get(key)), width, height);
context.setVariable(split[0], "data:image/jpeg;base64," + base64);
@@ -161,33 +161,30 @@ public class TemplateUtils {
}
/**
* 根据内容生成二维码
* @param content 转换内容
*/
private String generateQRCode(int type, String content, int width, int height) {
try {
BufferedImage qrImage = null;
BitMatrix bitMatrix = null;
if (type == 1) {
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 设置字符编码为 UTF-8
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 设置纠错级别
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 设置字符编码为 UTF-8
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 设置纠错级别
// 设置外边距为 0 以去除白边
hints.put(EncodeHintType.MARGIN, 0);
if (type == 1) {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
qrImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
}else if (type == 2) {
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
} else if (type == 2) {
Code128Writer barcodeWriter = new Code128Writer();
bitMatrix = barcodeWriter.encode(content, BarcodeFormat.CODE_128, width, height, hints);
qrImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
}
if ( qrImage == null ){
throw new RRException("图片渲染失败");
}
qrImage.createGraphics();
Graphics2D graphics = (Graphics2D) qrImage.getGraphics();
@@ -206,7 +203,7 @@ public class TemplateUtils {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(qrImage, "png", baos);
return Base64.getEncoder().encodeToString(baos.toByteArray());
}catch (Exception e){
} catch (Exception e) {
log.error("二维码/条形码生成失败", e);
throw new RRException("二维码/条形码生成失败");
}