From 46097c08251cdc0207dbb8b4317a925a5c8b3001 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=9F=B3=E5=A4=B4=E4=BA=BA?= <3076767823@qq.com>
Date: Mon, 17 Mar 2025 10:28:17 +0800
Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E5=A2=9E=E5=8A=A0=E5=B0=8F?=
=?UTF-8?q?=E7=A5=A8=E6=89=93=E5=8D=B0=E6=9C=BA=E6=8E=A5=E5=8F=A3=E4=BB=A5?=
=?UTF-8?q?=E5=8F=8A=E8=8E=B7=E5=8F=96=E7=8A=B6=E6=80=81=E6=8E=A5=E5=8F=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 5 ++
.../autoReplyPrint/base/BaseImagePrint.java | 74 ++++++++++++++++++-
.../controller/ReceiptPrintController.java | 39 +++++++---
.../controller/TemplateController.java | 8 +-
.../request/ReceiptPrintRequest.java | 6 +-
.../service/ImagePrintService.java | 6 ++
.../impl/COMImagePrintServiceImpl.java | 6 ++
.../impl/TCPImagePrintServiceImpl.java | 6 ++
.../service/impl/TemplateService.java | 50 +++++++++----
.../impl/USBImagePrintServiceImpl.java | 6 ++
.../autoReplyPrint/utils/TestFunction.java | 5 +-
11 files changed, 173 insertions(+), 38 deletions(-)
diff --git a/pom.xml b/pom.xml
index 041c123..6f2f48e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -97,6 +97,11 @@
core
3.4.1
+
+ com.google.zxing
+ javase
+ 3.4.1
+
diff --git a/src/main/java/com/dpkj/modules/autoReplyPrint/base/BaseImagePrint.java b/src/main/java/com/dpkj/modules/autoReplyPrint/base/BaseImagePrint.java
index ea9a96e..1c40298 100644
--- a/src/main/java/com/dpkj/modules/autoReplyPrint/base/BaseImagePrint.java
+++ b/src/main/java/com/dpkj/modules/autoReplyPrint/base/BaseImagePrint.java
@@ -3,9 +3,16 @@ package com.dpkj.modules.autoReplyPrint.base;
import com.dpkj.common.exception.RRException;
import com.dpkj.modules.autoReplyPrint.utils.AutoReplyPrint;
import com.dpkj.modules.autoReplyPrint.utils.ImageUtils;
+import com.dpkj.modules.autoReplyPrint.utils.TestFunction;
+import com.dpkj.modules.autoReplyPrint.vo.PrinterStatus;
import com.sun.jna.Pointer;
+import com.sun.jna.ptr.LongByReference;
+import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
/**
* 图片打印基础实现
@@ -14,6 +21,7 @@ import org.springframework.web.multipart.MultipartFile;
* @version 1.0
* @since 2025-01-17 14:37:23
*/
+@Slf4j
public abstract class BaseImagePrint {
/**
@@ -27,6 +35,7 @@ public abstract class BaseImagePrint {
public void printFromPath(String devName, int dstw, int dsth, String pszFile, int binaryzation_method, int compression_method) {
Pointer handle = getHandle(devName);
try {
+ System.out.println(devName + pszFile);
// 开始打印图片
boolean printTag = AutoReplyPrint.INSTANCE.CP_Pos_PrintRasterImageFromFile(handle, dstw, dsth, pszFile, binaryzation_method, compression_method);
if( !printTag ){
@@ -40,7 +49,10 @@ public abstract class BaseImagePrint {
throw new RRException("图片裁剪失败,请自行裁剪");
}
}catch (Exception e){
- e.printStackTrace();
+ log.error("{}, {}", this, e.getMessage(), e);
+ if ( e instanceof RRException) {
+ throw new RRException(((RRException) e).getCode(), e.getMessage());
+ }
throw new RRException(e);
}finally {
if ( handle != null) AutoReplyPrint.INSTANCE.CP_Port_Close(handle);
@@ -85,5 +97,65 @@ public abstract class BaseImagePrint {
}
+ public PrinterStatus getStatus(String devName){
+ Pointer handle = getHandle(devName);
+ try {
+ // 获取状态前对打印机的错误进行清除、同时清除缓存
+ boolean cleanTag = AutoReplyPrint.INSTANCE.CP_Printer_ClearPrinterError(handle);
+ boolean cacheTag = AutoReplyPrint.INSTANCE.CP_Printer_ClearPrinterBuffer(handle);
+ if ( !cleanTag){
+ throw new RRException(501, "打印机错误清除失败");
+ }
+
+ // 获取打印机状态,因为目前暂时没有办法获取更多的状态,直接获取打印机的状态,如果打印机的状态时非正常的,那么直接抛出打印机错误
+ byte[] statusResult = new byte[100];
+ LongByReference printer_error_status = new LongByReference();
+ LongByReference printer_info_status = new LongByReference();
+ LongByReference timestamp_ms_printer_status = new LongByReference();
+ boolean statusTag = AutoReplyPrint.INSTANCE.CP_Printer_GetPrinterStatusInfo(handle,printer_error_status, printer_info_status, timestamp_ms_printer_status);
+ if ( !statusTag){
+ throw new RRException(501, "打印机发生错误");
+ }
+
+ AutoReplyPrint.CP_PrinterStatus status = new AutoReplyPrint.CP_PrinterStatus(
+ printer_error_status.getValue(), printer_info_status.getValue());
+ String errorMessage = "";
+ if (status.ERROR_OCCURED()) {
+ if (status.ERROR_CUTTER())
+ throw new RRException(500, "ERROR_CUTTER");
+ if (status.ERROR_FLASH())
+ throw new RRException(500, "ERROR_FLASH");
+ if (status.ERROR_NOPAPER())
+ throw new RRException(500, "ERROR_NOPAPER");
+ if (status.ERROR_VOLTAGE())
+ throw new RRException(500, "ERROR_VOLTAGE");
+ if (status.ERROR_MARKER())
+ throw new RRException(500, "ERROR_MARKER");
+ if (status.ERROR_ENGINE())
+ throw new RRException(500, "ERROR_ENGINE");
+ if (status.ERROR_OVERHEAT())
+ throw new RRException(500, "ERROR_OVERHEAT");
+ if (status.ERROR_COVERUP())
+ throw new RRException(500, "ERROR_COVERUP");
+ if (status.ERROR_MOTOR())
+ throw new RRException(500, "ERROR_MOTOR");
+ }
+
+ Date dt_printer_status = new Date(timestamp_ms_printer_status.getValue());
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ String str_printer_error_status = simpleDateFormat.format(dt_printer_status) + String.format(" Printer Error Status: 0x%04X\r\n", printer_error_status.getValue() & 0xffff);
+ String str_printer_info_status = simpleDateFormat.format(dt_printer_status) + String.format(" Printer Info Status: 0x%04X\r\n", printer_info_status.getValue() & 0xffff);
+
+ return new PrinterStatus();
+ }catch (Exception e){
+ e.printStackTrace();
+ if ( e instanceof RRException){
+ throw new RRException(((RRException) e).getCode(), e.getMessage());
+ }
+ throw new RRException(e);
+ }finally {
+ if ( handle != null) AutoReplyPrint.INSTANCE.CP_Port_Close(handle);
+ }
+ }
}
diff --git a/src/main/java/com/dpkj/modules/autoReplyPrint/controller/ReceiptPrintController.java b/src/main/java/com/dpkj/modules/autoReplyPrint/controller/ReceiptPrintController.java
index 9bbd335..66b279f 100644
--- a/src/main/java/com/dpkj/modules/autoReplyPrint/controller/ReceiptPrintController.java
+++ b/src/main/java/com/dpkj/modules/autoReplyPrint/controller/ReceiptPrintController.java
@@ -6,9 +6,12 @@ import com.dpkj.common.vo.Result;
import com.dpkj.modules.autoReplyPrint.request.ReceiptPrintRequest;
import com.dpkj.modules.autoReplyPrint.service.ImagePrintService;
import com.dpkj.modules.autoReplyPrint.service.impl.TemplateService;
+import com.dpkj.modules.autoReplyPrint.vo.PrinterStatus;
+import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;
+import java.io.File;
import javax.annotation.Resource;
@@ -19,6 +22,7 @@ import javax.annotation.Resource;
* @version 1.0
* @since 2025-03-10 9:29:22
*/
+@Slf4j
@RestController
@RequestMapping("/receipt")
public class ReceiptPrintController {
@@ -32,31 +36,44 @@ public class ReceiptPrintController {
@PostMapping("/print")
private Result print(@Validated @RequestBody ReceiptPrintRequest request){
String devName = "VID:0x0FE6,PID:0x811E"; // 采用默认的devName,不进行入参传值了
+ // 进行模板填充
+ 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\"}";
StringBuilder filePath = new StringBuilder(request.getFileDir());
- //
// 校验是否选中了模板,如果没选中模板的话则不需要另外生成了
if ( !StringUtils.isEmpty(request.getTemplateName()) && !StringUtils.isEmpty(request.getFileDir())){
- String templateFillData = request.getTemplateFillData();
- if ( !StringUtils.isEmpty(templateFillData)){
- byte[] image = templateService.generateReceiptImage(
- JSONObject.parseObject(request.getTemplateFillData()),
- request.getTemplateName(),
- request.getWidth(),
- request.getHeight(),
- filePath);
- }
+ byte[] image = templateService.generateReceiptImage(JSONObject.parseObject(testData), request.getTemplateName(), request.getWidth(), request.getHeight(), filePath);
}else {
throw new RRException("模板渲染错误");
}
-
usbImagePrintService.imagePrintFromPath(devName,
request.getWidth(),
request.getHeight(),
filePath.toString(),
1,
0);
+
+ // 删除图片
+ File file = new File(filePath.toString());
+ // 检查文件是否存在
+ if (file.exists() ) {
+ // 尝试删除文件
+ if (file.delete()) {
+ log.info("文件删除成功: " + filePath);
+ } else {
+ log.info("文件删除失败: " + filePath);
+ }
+ } else {
+ log.info("文件不存在: " + filePath);
+ }
+
return Result.ok();
}
+ @PostMapping("/getStatus")
+ public Result print(){
+ String devName = "VID:0x0FE6,PID:0x811E"; // 采用默认的devName,不进行入参传值了
+ return Result.ok(this.usbImagePrintService.getStatus(devName));
+ }
+
}
diff --git a/src/main/java/com/dpkj/modules/autoReplyPrint/controller/TemplateController.java b/src/main/java/com/dpkj/modules/autoReplyPrint/controller/TemplateController.java
index 85c8470..22283c7 100644
--- a/src/main/java/com/dpkj/modules/autoReplyPrint/controller/TemplateController.java
+++ b/src/main/java/com/dpkj/modules/autoReplyPrint/controller/TemplateController.java
@@ -28,8 +28,8 @@ public class TemplateController {
@RequestParam Integer height,
@PathVariable String templateName,
@RequestParam(defaultValue = "E:\\images") String saveDir) {
- StringBuilder fileDir = new StringBuilder(saveDir);
- this.templateService.generateReceiptImage(JSONObject.parseObject(jsonData), templateName, width, height, fileDir);
+ StringBuilder filePath = new StringBuilder();
+ this.templateService.generateReceiptImage(JSONObject.parseObject(jsonData), templateName, width, height, filePath);
return Result.ok("模板生成成功");
}
@@ -45,8 +45,8 @@ public class TemplateController {
"
\n" +
"