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("二维码/条形码生成失败");
}

View File

@@ -10,6 +10,8 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
/**
* 挂号controller
@@ -39,10 +41,15 @@ public class RegisterController {
* 挂号,通过文件路径
*/
@PostMapping("/print")
public Result<LexMarkResultDTO> registerByFilePath(@RequestBody @Validated ReceiptPrintRequest request){
String testData = "{\"hospitalName\":\"澜沧县中医医院\",\"registeTerminalName\":\"中国农业银行自助终端\",\"registeType\":\"自助挂号\",\"name\":\"刘博雅\",\"gender\":\"\",\"age\":28,\"birthDate\":\"1996-06-31\",\"cardNumber\":\"6221**********0731\",\"outpatientNumber\":\"2501150038\",\"department\":\"普外科门诊\",\"visitLevel\":\"普通号\",\"doctor\":\"普通门诊\",\"sequence\":\"1\",\"registerDate\":\"2025-01-15\",\"totalFee\":4.00,\"paymentMethod\":\"微信扫码支付\",\"orderNumber\":\"\",\"transactionNumber\":\"2025011513090412092794szztzzj\",\"qrCodeBase64_2base64Type_1_250_250\":\"maby this is a Base64 code data if has\",\"terminalNumber\":\"12092794\",\"printTime\":\"2025-01-15 13:10:08\"}";
public Result<LexMarkResultDTO> registerByFilePath(@RequestBody @Validated ReceiptPrintRequest request) throws UnsupportedEncodingException {
// String testData = "{\"hospitalName\":\"澜沧县中医医院\",\"registeTerminalName\":\"中国农业银行自助终端\",\"registeType\":\"自助挂号\",\"name\":\"刘博雅\",\"gender\":\"男\",\"age\":28,\"birthDate\":\"1996-06-31\",\"cardNumber\":\"6221**********0731\",\"outpatientNumber\":\"2501150038\",\"department\":\"普外科门诊\",\"visitLevel\":\"普通号\",\"doctor\":\"普通门诊\",\"sequence\":\"1\",\"registerDate\":\"2025-01-15\",\"totalFee\":4.00,\"paymentMethod\":\"微信扫码支付\",\"orderNumber\":\"\",\"transactionNumber\":\"2025011513090412092794szztzzj\",\"qrCodeBase64_2base64Type_1_250_250\":\"maby this is a Base64 code data if has\",\"terminalNumber\":\"12092794\",\"printTime\":\"2025-01-15 13:10:08\"}";
// String testData = "{\"hospitalName\":\"澜沧县中医医院\",\"registeTerminalName\":\"中国农业银行自助终端\",\"registeType\":\"自助挂号\",\"name\":\"刘博雅\",\"gender\":\"男\",\"age\":28,\"birthDate\":\"1996-06-31\",\"cardNumber\":\"6221**********0731\",\"outpatientNumber\":\"2501150038\",\"department\":\"普外科门诊\",\"visitLevel\":\"普通号\",\"doctor\":\"普通门诊\",\"sequence\":\"1\",\"registerDate\":\"2025-01-15\",\"totalFee\":4.00,\"paymentMethod\":\"微信扫码支付\",\"orderNumber\":\"\",\"transactionNumber\":\"2025011513090412092794szztzzj\",\"qrCodeBase64_2base64Type_2_500_160\":\"maby this is a Base64 code data if has\",\"terminalNumber\":\"12092794\",\"printTime\":\"2025-01-15 13:10:08\"}";
return Result.ok((LexMarkResultDTO)printService.printImage(JSONObject.parseObject(testData), request.getTemplateName(), request.getWidth(), request.getHeight(), request.getFileDir()));
return Result.ok((LexMarkResultDTO)printService.printImage(
JSONObject.parseObject(URLDecoder.decode(request.getTemplateFillData(), "UTF-8")),
request.getTemplateName(),
request.getWidth(),
request.getHeight(),
request.getFileDir()));
}
/**

View File

@@ -53,17 +53,15 @@ public class RegisterServiceImpl implements PrintService {
param.put("alignment", 0); // 对齐方式
param.put("offsetX", 0); // x
param.put("offsetY", 0); // y
param.put("resolution", 1); //
param.put("resolution", 8); //
param.put("mediaCtrl", 1);
param.put("fields", "LOGO=" + filePath);
lexMarkDTO.setParam(param.toJSONString());
LexMarkResultDTO<LexMarkResultDTO.Param> paramLexMarkResultDTO = thirdService.callDevice(lexMarkDTO, LexMarkResultDTO.Param.class);
// 切纸
//this.thirdService.cutPaper("ReceiptPrinter", "ControlMedia", 4);
File file = new File(filePath.toString());
// 检查文件是否存在
if (file.exists()) {
if (file.exists() ) {
// 尝试删除文件
if (file.delete()) {
log.info("文件删除成功: " + filePath);