Compare commits

..

2 Commits

1 changed files with 61 additions and 14 deletions

View File

@ -15,7 +15,10 @@ import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils; import org.thymeleaf.util.StringUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.IOException;
/** /**
* 挂号服务打印 * 挂号服务打印
@ -31,8 +34,14 @@ public class RegisterServiceImpl implements PrintService {
@Resource @Resource
private ThirdService thirdService; private ThirdService thirdService;
private static final int MAX_HEIGHT = 1000;
private static final int FIXED_WIDTH = 690;
@Override @Override
public LexMarkResultDTO<LexMarkResultDTO.Param> printImage(JSONObject data, String template, int width, int height, String saveDir) { public LexMarkResultDTO<LexMarkResultDTO.Param> printImage(JSONObject data, String template, int width, int height, String saveDir) {
if ( width > 690 || height <= 0){
width = 690;
}
// 获取模板 // 获取模板
String templateName = ReceiptTemplateEnum.getTemplateName(template); String templateName = ReceiptTemplateEnum.getTemplateName(template);
@ -45,34 +54,72 @@ public class RegisterServiceImpl implements PrintService {
throw new RRException("模板渲染错误"); throw new RRException("模板渲染错误");
} }
String[] deletePathList = new String[(int) Math.ceil((double) height / MAX_HEIGHT) + 1];
deletePathList[0] = filePath.toString();
String[] filePathList = new String[(int) Math.ceil((double) height / MAX_HEIGHT)];
// 对图片进行分块处理当前台式打印机最大参数配置 宽度690高度1200
if ( height > 1200){
try {
// 读取输入图片
BufferedImage originalImage = ImageIO.read(new File(filePath.toString()));
// 计算切割的块数
int numPieces = (int) Math.ceil((double) height / MAX_HEIGHT);
// 循环切割图片并保存每一块
for (int i = 0; i < numPieces; i++) {
int startY = i * MAX_HEIGHT;
int pieceHeight = Math.min(MAX_HEIGHT, height - startY);
// 创建一个新的 BufferedImage 对象用于存储切割的部分
BufferedImage piece = new BufferedImage(FIXED_WIDTH, pieceHeight, BufferedImage.TYPE_INT_RGB);
piece.getGraphics().drawImage(originalImage, 0, 0, FIXED_WIDTH, pieceHeight, 0, startY, FIXED_WIDTH, startY + pieceHeight, null);
// 保存切割的图片块
File outputFile = new File(saveDir + "/output_" + (i + 1) + ".jpg");
filePathList[i] = "LOGO" + (i + 1) + "=" + saveDir + "/output_" + (i + 1) + ".jpg";
deletePathList[i + 1] = saveDir + "/output_" + (i + 1) + ".jpg";
ImageIO.write(piece, "jpg", outputFile);
}
} catch (IOException e) {
log.error("");
e.printStackTrace();
}
}else {
filePathList[0] = "LOGO" + filePath.toString();
}
LexMarkDTO lexMarkDTO = new LexMarkDTO(); LexMarkDTO lexMarkDTO = new LexMarkDTO();
lexMarkDTO.setActionName("PrintForm"); lexMarkDTO.setActionName("PrintForm");
lexMarkDTO.setCallID(19256); lexMarkDTO.setCallID(19256);
lexMarkDTO.setDevName("ReceiptPrinter"); lexMarkDTO.setDevName("ReceiptPrinter");
JSONObject param = new JSONObject(); JSONObject param = new JSONObject();
param.put("TimeOut", 30000); param.put("TimeOut", 30000);
param.put("formName", "ReceiptForm"); // 表单名从 param.put("formName", "GWIReceiptForm_example"); // 表单名从
param.put("mediaName", "Blank"); // 媒介类型 param.put("mediaName", "GWIReceiptMedia_example"); // 媒介类型
param.put("alignment", 0); // 对齐方式 param.put("alignment", 0); // 对齐方式
param.put("offsetX", 0); // x param.put("offsetX", 0); // x
param.put("offsetY", 0); // y param.put("offsetY", 0); // y
param.put("resolution", 8); // param.put("resolution", 1); // 媒介类型当前为打印缓冲区 + 弹出 + 剪纸
param.put("mediaCtrl", 1); param.put("mediaCtrl", 8); // 1\2\4\6\8打印的清晰度
param.put("fields", "LOGO=" + filePath); param.put("fields", StringUtils.join(filePathList, "&"));
lexMarkDTO.setParam(param.toJSONString()); lexMarkDTO.setParam(param.toJSONString());
LexMarkResultDTO<LexMarkResultDTO.Param> paramLexMarkResultDTO = thirdService.callDevice(lexMarkDTO, LexMarkResultDTO.Param.class); LexMarkResultDTO<LexMarkResultDTO.Param> paramLexMarkResultDTO = thirdService.callDevice(lexMarkDTO, LexMarkResultDTO.Param.class);
File file = new File(filePath.toString()); for (String path : deletePathList) {
File file = new File(path);
// 检查文件是否存在 // 检查文件是否存在
if (file.exists() ) { if (file.exists() ) {
// 尝试删除文件 // 尝试删除文件
if (file.delete()) { if (file.delete()) {
log.info("文件删除成功: " + filePath); log.info("文件删除成功: " + path);
} else { } else {
log.info("文件删除失败: " + filePath); log.info("文件删除失败: " + path);
} }
} else { } else {
log.info("文件不存在: " + filePath); log.info("文件不存在: " + path);
}
} }
// this.thirdService.close("ReceiptPrinter"); // this.thirdService.close("ReceiptPrinter");