From ac183aa4734e160e32897d1bf77e9f68c5e31bb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=B3=E5=A4=B4=E4=BA=BA?= <3076767823@qq.com> Date: Tue, 25 Mar 2025 15:24:44 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E4=BF=AE=E5=A4=8D=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E5=A4=AA=E9=95=BF=E6=89=93=E5=8D=B0=E4=B8=8D=E5=87=BA?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=8C=E9=9C=80=E8=A6=81=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=8F=B0=E5=BC=8F=E6=9C=BA=E7=9A=84GWI=5FReceipt=5Fform.form?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/RegisterServiceImpl.java | 75 +++++++++++++++---- 1 file changed, 61 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/dpkj/modules/print/service/impl/RegisterServiceImpl.java b/src/main/java/com/dpkj/modules/print/service/impl/RegisterServiceImpl.java index 6f61c32..016cc86 100644 --- a/src/main/java/com/dpkj/modules/print/service/impl/RegisterServiceImpl.java +++ b/src/main/java/com/dpkj/modules/print/service/impl/RegisterServiceImpl.java @@ -15,7 +15,10 @@ import org.springframework.stereotype.Service; import org.thymeleaf.util.StringUtils; import javax.annotation.Resource; +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; import java.io.File; +import java.io.IOException; /** * 挂号服务打印 @@ -31,8 +34,14 @@ public class RegisterServiceImpl implements PrintService { @Resource private ThirdService thirdService; + private static final int MAX_HEIGHT = 1000; + private static final int FIXED_WIDTH = 690; + @Override public LexMarkResultDTO printImage(JSONObject data, String template, int width, int height, String saveDir) { + if ( width > 690 || height <= 0){ + width = 690; + } // 获取模板 String templateName = ReceiptTemplateEnum.getTemplateName(template); @@ -45,34 +54,72 @@ public class RegisterServiceImpl implements PrintService { 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.setActionName("PrintForm"); lexMarkDTO.setCallID(19256); lexMarkDTO.setDevName("ReceiptPrinter"); JSONObject param = new JSONObject(); param.put("TimeOut", 30000); - param.put("formName", "ReceiptForm"); // 表单名从 - param.put("mediaName", "Blank"); // 媒介类型 + param.put("formName", "GWIReceiptForm_example"); // 表单名从 + param.put("mediaName", "GWIReceiptMedia_example"); // 媒介类型 param.put("alignment", 0); // 对齐方式 param.put("offsetX", 0); // x param.put("offsetY", 0); // y - param.put("resolution", 8); // - param.put("mediaCtrl", 1); - param.put("fields", "LOGO=" + filePath); + param.put("resolution", 1); // 媒介类型,当前为:打印缓冲区 + 弹出 + 剪纸 + param.put("mediaCtrl", 8); // 1\2\4\6\8打印的清晰度 + param.put("fields", StringUtils.join(filePathList, "&")); lexMarkDTO.setParam(param.toJSONString()); LexMarkResultDTO paramLexMarkResultDTO = thirdService.callDevice(lexMarkDTO, LexMarkResultDTO.Param.class); - File file = new File(filePath.toString()); - // 检查文件是否存在 - if (file.exists() ) { - // 尝试删除文件 - if (file.delete()) { - log.info("文件删除成功: " + filePath); + for (String path : deletePathList) { + File file = new File(path); + // 检查文件是否存在 + if (file.exists() ) { + // 尝试删除文件 + if (file.delete()) { + log.info("文件删除成功: " + path); + } else { + log.info("文件删除失败: " + path); + } } else { - log.info("文件删除失败: " + filePath); + log.info("文件不存在: " + path); } - } else { - log.info("文件不存在: " + filePath); } // this.thirdService.close("ReceiptPrinter");