Przeglądaj źródła

fix: 修复二维码打印问题并优化标签布局
问题修复:
- 修复 qrcode() 函数不工作的问题,改用 sendcommand() 发送原始 TSPL 命令
- 添加 TSCLIB.sendcommand() 接口,支持发送原始 TSPL 指令
- 解决二维码静默区超出标签范围导致多打空白标签的问题
布局优化:
- 二维码位置从 (80, 120) 调整到 (70, 145),上移避免静默区超出
- 二维码 cellWidth 从 3 增大到 7,提高扫码识别率
- 批次号字体从 20 增大到 24 并加粗,提高可读性
- 微调所有文本元素位置,优化整体布局对齐

技术细节:
- 二维码使用原始 TSPL 命令格式:QRCODE x,y,eccLevel,cellWidth,mode,rotation,"data"
- 添加调试日志输出二维码参数和 TSPL 命令
- QR 码静默区(Quiet Zone)约 4 个模块宽度,需预留足够底部空间

wangxichen 1 dzień temu
rodzic
commit
c109217027

+ 17 - 17
config/label-config.json

@@ -11,12 +11,12 @@
       "id": "qrcode",
       "type": "qrcode",
       "label": "二维码",
-      "x": 80,
-      "y": 120,
+      "x": 70,
+      "y": 145,
       "width": 100,
       "height": 100,
       "eccLevel": "L",
-      "cellWidth": "3",
+      "cellWidth": "7",
       "text": "+KB60IS3031T811001201",
       "dataSource": "sn"
     },
@@ -24,8 +24,8 @@
       "id": "company-line1",
       "type": "text",
       "label": "公司名-第1行",
-      "x": 77,
-      "y": 81,
+      "x": 115,
+      "y": 75,
       "fontSize": 26,
       "fontFamily": "SimSun",
       "fontStyle": 1,
@@ -36,8 +36,8 @@
       "id": "company-line2",
       "type": "text",
       "label": "公司名-第2行",
-      "x": 22,
-      "y": 115,
+      "x": 55,
+      "y": 112,
       "fontSize": 26,
       "fontFamily": "SimSun",
       "fontStyle": 1,
@@ -48,8 +48,8 @@
       "id": "product-line1",
       "type": "text",
       "label": "产品名",
-      "x": 277,
-      "y": 81,
+      "x": 304,
+      "y": 75,
       "fontSize": 28,
       "fontFamily": "SimSun",
       "fontStyle": 1,
@@ -60,8 +60,8 @@
       "id": "product-line2",
       "type": "text",
       "label": "产品型号",
-      "x": 300,
-      "y": 115,
+      "x": 335,
+      "y": 114,
       "fontSize": 24,
       "fontFamily": "Arial",
       "fontStyle": 0,
@@ -72,8 +72,8 @@
       "id": "date-serial",
       "type": "text",
       "label": "日期+流水号",
-      "x": 276,
-      "y": 200,
+      "x": 298,
+      "y": 204,
       "fontSize": 24,
       "fontFamily": "Arial",
       "fontStyle": 0,
@@ -84,11 +84,11 @@
       "id": "batch-number",
       "type": "text",
       "label": "批次号",
-      "x": 288,
-      "y": 255,
-      "fontSize": 20,
+      "x": 291,
+      "y": 275,
+      "fontSize": 24,
       "fontFamily": "Arial",
-      "fontStyle": 0,
+      "fontStyle": 1,
       "text": "+KB60IS3031T811001201",
       "dataSource": "sn"
     }

+ 48 - 0
src/com/mes/print/test/TestBarcode.java

@@ -0,0 +1,48 @@
+package com.mes.print.test;
+
+import com.mes.print.tsc.TSCLIB;
+
+/**
+ * 测试条形码打印
+ */
+public class TestBarcode {
+    public static void main(String[] args) {
+        TSCLIB tsc = TSCLIB.INSTANCE;
+
+        try {
+            // 1. 打开打印机
+            System.out.println("打开打印机...");
+            int result = tsc.openport("TSC TTP-244 PRO");
+            if (result != 1) {
+                System.err.println("打开打印机失败");
+                return;
+            }
+
+            // 2. 设置标签参数
+            System.out.println("设置标签参数...");
+            tsc.setup("76.2", "41.1", "4", "10", "0", "3", "0");
+
+            // 3. 清除缓冲区
+            tsc.clearbuffer();
+
+            // 4. 绘制条形码(CODE128)
+            System.out.println("绘制条形码...");
+            System.out.println("  位置: (50, 100)");
+            System.out.println("  内容: KB60IS3031T81");
+
+            tsc.barcode128("50", "100", "80", "1", "0", "2", "KB60IS3031T81");
+
+            // 5. 打印
+            System.out.println("开始打印...");
+            tsc.printlabel("1", "1");
+
+            // 6. 关闭打印机
+            tsc.closeport();
+
+            System.out.println("完成!");
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}

+ 50 - 0
src/com/mes/print/test/TestQRCode.java

@@ -0,0 +1,50 @@
+package com.mes.print.test;
+
+import com.mes.print.tsc.TSCLIB;
+
+/**
+ * 测试二维码打印
+ */
+public class TestQRCode {
+    public static void main(String[] args) {
+        TSCLIB tsc = TSCLIB.INSTANCE;
+
+        try {
+            // 1. 打开打印机
+            System.out.println("打开打印机...");
+            int result = tsc.openport("TSC TTP-244 PRO");
+            if (result != 1) {
+                System.err.println("打开打印机失败");
+                return;
+            }
+
+            // 2. 设置标签参数
+            System.out.println("设置标签参数...");
+            tsc.setup("76.2", "41.1", "4", "10", "0", "3", "0");
+
+            // 3. 清除缓冲区
+            tsc.clearbuffer();
+
+            // 4. 绘制二维码(简单测试)
+            System.out.println("绘制二维码...");
+            System.out.println("  位置: (50, 100)");
+            System.out.println("  纠错: L");
+            System.out.println("  单元: 5");
+            System.out.println("  内容: TEST123");
+
+            tsc.qrcode("50", "100", "L", "5", "A", "0", "TEST123");
+
+            // 5. 打印
+            System.out.println("开始打印...");
+            tsc.printlabel("1", "1");
+
+            // 6. 关闭打印机
+            tsc.closeport();
+
+            System.out.println("完成!");
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}

+ 48 - 0
src/com/mes/print/test/TestQRCodeRaw.java

@@ -0,0 +1,48 @@
+package com.mes.print.test;
+
+import com.mes.print.tsc.TSCLIB;
+
+/**
+ * 测试用原始 TSPL 命令打印二维码
+ */
+public class TestQRCodeRaw {
+    public static void main(String[] args) {
+        TSCLIB tsc = TSCLIB.INSTANCE;
+
+        try {
+            // 1. 打开打印机
+            System.out.println("打开打印机...");
+            int result = tsc.openport("TSC TTP-244 PRO");
+            if (result != 1) {
+                System.err.println("打开打印机失败");
+                return;
+            }
+
+            // 2. 设置标签参数
+            System.out.println("设置标签参数...");
+            tsc.setup("76.2", "41.1", "4", "10", "0", "3", "0");
+
+            // 3. 清除缓冲区
+            tsc.clearbuffer();
+
+            // 4. 用 sendcommand 发送原始 TSPL 二维码命令
+            System.out.println("发送原始 TSPL 二维码命令...");
+            String qrCommand = "QRCODE 50,100,L,5,A,0,\"TEST123\"";
+            System.out.println("  命令: " + qrCommand);
+
+            tsc.sendcommand(qrCommand);
+
+            // 5. 打印
+            System.out.println("开始打印...");
+            tsc.printlabel("1", "1");
+
+            // 6. 关闭打印机
+            tsc.closeport();
+
+            System.out.println("完成!");
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}

+ 6 - 0
src/com/mes/print/tsc/TSCLIB.java

@@ -106,4 +106,10 @@ public interface TSCLIB extends Library {
      */
     int qrcode(String x, String y, String eccLevel, String cellWidth,
                String mode, String rotation, String code);
+
+    /**
+     * 发送原始命令到打印机
+     * @param command TSPL 命令字符串
+     */
+    int sendcommand(String command);
 }

+ 8 - 4
src/com/mes/print/tsc/TscPrinter.java

@@ -113,16 +113,20 @@ public class TscPrinter {
                 System.out.println("[DEBUG] 二维码位置: (" + element.x + ", " + element.y + ")");
                 System.out.println("[DEBUG] 二维码尺寸: " + element.width + "x" + element.height);
 
-                // 绘制二维码
-                tsc.qrcode(
-                    String.valueOf(element.x),
-                    String.valueOf(element.y),
+                // 使用原始 TSPL 命令绘制二维码(qrcode() 函数不工作)
+                // 格式: QRCODE x,y,eccLevel,cellWidth,mode,rotation,"data"
+                String qrCommand = String.format(
+                    "QRCODE %d,%d,%s,%s,%s,%s,\"%s\"",
+                    element.x,
+                    element.y,
                     element.eccLevel,
                     element.cellWidth,
                     "A",  // mode
                     "0",  // rotation
                     qrData
                 );
+                System.out.println("[DEBUG] TSPL 命令: " + qrCommand);
+                tsc.sendcommand(qrCommand);
             }
         }
     }