Explorar el Código

斑马打印测试

zhuhao hace 2 años
padre
commit
d935dd92ee

La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 244 - 152
imcs-admin-boot/imcs-business-biz/src/main/java/com/github/zuihou/business/operationManagementCenter/service/impl/TaskNodeServiceImpl.java


+ 125 - 101
imcs-admin-boot/imcs-business-biz/src/main/java/com/github/zuihou/business/util/ZplPrinter.java

@@ -1,5 +1,9 @@
 package com.github.zuihou.business.util;
 
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 import javax.print.*;
 import javax.print.attribute.standard.PrinterName;
 import java.io.File;
@@ -7,77 +11,72 @@ import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 
-public class ZplPrinter {
-    private String printerURI = null;			//打印机完整路径
-    private PrintService printService = null;	//打印机服务
-    private byte[] dotFont;
-    private String darkness = "~SD10";	//Set Darkness设置色带颜色的深度 0-30
-    private String width = "^PW1000";	//Print Width打印宽度0-1500
-    private String length = "^LL800";	//Label Length标签长度0-x(暂无作用)
-    private String begin = "^XA" + darkness + width;	//标签格式以^XA开始
-    private String end = "^XZ";			//标签格式以^XZ结束
-    private static String content = "";		//打印内容
-    private String message = "";		//打印的结果信息
 
+public class ZplPrinter {
+    private static Logger log = LoggerFactory.getLogger(ZplPrinter.class);
+    private String printerURI = null;           //打印机完整路径
+    private PrintService printService = null;   //打印机服务
+    private byte[] font;
+    private String darkness = "~SD22";          //设置色带颜色的深度 0~30
+    private String width = "^PW1200";           //打印宽度 0~1500
+    private String begin = "^XA" + darkness + width;    //标签格式以^XA开始
+    private String end = "^XZ";
+    private String content = "";                //打印内容
+    private String message = "";                //打印的结果信息
 
     /**
      * 构造方法
-     *
-     * @param printerURI 打印机路径
      */
     public ZplPrinter(String printerURI) {
         this.printerURI = printerURI;
         //加载字体
-        //File file = new File("C:\\Users\\gonghf\\Desktop\\ts24.lib");
         String relativelyPath = System.getProperty("user.dir");
         File file = new File(relativelyPath + "//ts24.lib");
         if (file.exists()) {
             FileInputStream fis;
             try {
                 fis = new FileInputStream(file);
-                dotFont = new byte[fis.available()];
-                fis.read(dotFont);
+                font = new byte[fis.available()];
+                fis.read(font);
                 fis.close();
-            } catch (IOException e) {
-                e.printStackTrace();
+            } catch (Exception e) {
+                log.info(e.getMessage());
             }
         } else {
-            System.out.println("ts24.lib文件不存在");
+            log.info("D://lib//ts24.lib 文件不存在");
         }
         //初始化打印机
-        //AppContext.getAppContext().put(PrintServiceLookup.class.getDeclaredClasses()[0], null);//刷新打印机列表
         PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
         if (services != null && services.length > 0) {
             for (PrintService service : services) {
-                if (service.getName().equals(printerURI)) {
+                if (printerURI.equals(service.getName())) {
                     printService = service;
                     break;
                 }
             }
         }
         if (printService == null) {
-            System.out.println("没有找到打印机:[" + printerURI + "]");
-            //循环出所有的打印机
+            log.info("没有找到打印机:[" + printerURI + "]");
             if (services != null && services.length > 0) {
-                System.out.println("可用的打印机列表:");
+                log.info("可用的打印机列表:");
                 for (PrintService service : services) {
-                    System.out.println("[" + service.getName() + "]");
+                    log.info("[" + service.getName() + "]");
                 }
             }
         } else {
-            System.out.println("找到打印机:[" + printerURI + "]");
-            System.out.println("打印机名称:[" + printService.getAttribute(PrinterName.class).getValue() + "]");
+            log.info("找到打印机:[" + printerURI + "]");
+            log.info("打印机名称:[" + printService.getAttribute(PrinterName.class).getValue() + "]");
         }
     }
 
     /**
      * 设置条形码
      *
-     * @param barcode 条码字符
+     * @param barCode 条码字符
      * @param zpl     条码样式模板
      */
-    public void setBarcode(String barcode, String zpl) {
-        content += zpl.replace("${data}", barcode);
+    public void setBarCode(String barCode, String zpl) {
+        content += zpl.replace("${data}", barCode);
     }
 
     /**
@@ -93,24 +92,22 @@ public class ZplPrinter {
      * @param my  中文y轴字体图形放大倍率。范围1-10,默认1
      * @param ms  中文字体间距。24是个比较合适的值。
      */
-    public void setText(String str, int x, int y, int eh, int ew, int es, int mx, float my, int ms) {
+    public void setText(String str, int x, int y, int eh, int ew, int es, int mx, int my, int ms) {
         byte[] ch = str2bytes(str);
         for (int off = 0; off < ch.length; ) {
             if (((int) ch[off] & 0x00ff) >= 0xA0) {//中文字符
                 try {
                     int qcode = ch[off] & 0xff;
                     int wcode = ch[off + 1] & 0xff;
-//                    content += String.format("^FO%d,%d^XG0000%01X%01X,%d,%d^FS\n", x, y, qcode, wcode, mx, my);
-                    content += String.format("^FO%d,%d^XG0000%01X%01X,%d,%f^FS\n", x, y, qcode, wcode, mx, my);
-//                    System.out.println("content===="+content);
+                    content += String.format("^FO%d,%d^XG0000%01X%01X,%d,%d^FS\n", x, y, qcode, wcode, mx, my);
                     begin += String.format("~DG0000%02X%02X,00072,003,\n", qcode, wcode);
                     qcode = (qcode + 128 - 32) & 0x00ff;
                     wcode = (wcode + 128 - 32) & 0x00ff;
                     int offset = ((int) qcode - 16) * 94 * 72 + ((int) wcode - 1) * 72;
                     for (int j = 0; j < 72; j += 3) {
-                        qcode = (int) dotFont[j + offset] & 0x00ff;
-                        wcode = (int) dotFont[j + offset + 1] & 0x00ff;
-                        int qcode1 = (int) dotFont[j + offset + 2] & 0x00ff;
+                        qcode = (int) font[j + offset] & 0x00ff;
+                        wcode = (int) font[j + offset + 1] & 0x00ff;
+                        int qcode1 = (int) font[j + offset + 2] & 0x00ff;
                         begin += String.format("%02X%02X%02X\n", qcode, wcode, qcode1);
                     }
                     x = x + ms * mx;
@@ -136,29 +133,24 @@ public class ZplPrinter {
      * @param str 英文字符串
      * @param x   x坐标
      * @param y   y坐标
-     * @param h   高度  H
+     * @param h   高度
      * @param w   宽度
      */
-    public void setChar(String str, double x, double y, int h, int w) {
-        content += "^FO" + x + "," + y + "^AQN," + h + "," + w + "^FD" + str + "^FS";
+    public void setChar(String str, int x, int y, int h, int w) {
+        content += "^FO" + x + "," + y + "^A0," + h + "," + w + "^FD" + str + "^FS";
     }
 
-
     /**
-     * 英文字符(包含数字)顺时针旋转180度
-     *
-     * @param str 英文字符串
-     * @param x   x坐标
-     * @param y   y坐标
-     * @param h   高度
-     * @param w   宽度
+     * 重置ZPL指令,多张标签同时打印时需要调用
      */
-    public void setCharR(String str, int x, int y, int h, int w) {
-        content += "^FO" + x + "," + y + "^A0I," + h + "," + w + "^FD" + str + "^FS";
+    public void resetZpl() {
+        begin = "^XA" + darkness + width;
+        end = "^XZ";
+        content = "";
     }
 
     /**
-     * 获取完整的ZPL
+     * 获取完整的ZPL指令
      *
      * @return
      */
@@ -167,12 +159,22 @@ public class ZplPrinter {
     }
 
     /**
-     * 重置ZPL指令,当需要打印多张纸的时候需要调用。
+     * 字符串转换为byte[]
+     *
+     * @param s
+     * @return
      */
-    public void resetZpl() {
-        begin = "^XA" + darkness + width;
-        end = "^XZ";
-        content = "";
+    private byte[] str2bytes(String s) {
+        if (null == s || "".equals(s)) {
+            return null;
+        }
+        byte[] abytes = null;
+        try {
+            abytes = s.getBytes("gb2312");
+        } catch (UnsupportedEncodingException ex) {
+            ex.printStackTrace();
+        }
+        return abytes;
     }
 
     /**
@@ -182,8 +184,7 @@ public class ZplPrinter {
      */
     public boolean print(String zpl) {
         if (printService == null) {
-            message = "打印出错:没有找到打印机[" + printerURI + "]";
-            System.out.println("打印出错:没有找到打印机[" + printerURI + "]");
+            log.info("打印出错:没有找到打印机[" + printerURI + "]");
             return false;
         }
         DocPrintJob job = printService.createPrintJob();
@@ -192,60 +193,83 @@ public class ZplPrinter {
         Doc doc = new SimpleDoc(by, flavor, null);
         try {
             job.print(doc, null);
-            message = "已打印";
-            System.out.println("已打印");
+            log.info("已打印");
             return true;
         } catch (PrintException e) {
-            message = "打印出错:" + e.getMessage();
-            System.out.println("打印出错:" + e.getMessage());
+            log.info("打印出错:" + e.getMessage());
             e.printStackTrace();
             return false;
         }
     }
 
-    public String getMessage() {
-        return message;
-    }
-
-    /**
-     * 字符串转byte[]
-     *
-     * @param s
-     * @return
-     */
-    private byte[] str2bytes(String s) {
-        if (null == s || "".equals(s)) {
-            return null;
-        }
-        byte[] abytes = null;
-        try {
-            abytes = s.getBytes("gb2312");
-        } catch (UnsupportedEncodingException ex) {
-            ex.printStackTrace();
+    public static boolean printBarcode(ZplPrinter printer, String uniqueCode) {
+        if (StringUtils.isNotEmpty(uniqueCode)) {
+            String[] uniqueCodeArray = uniqueCode.split("&");
+            if (uniqueCodeArray.length > 4) {
+                printer.setChar("Order: " + uniqueCodeArray[0] + uniqueCodeArray[1], 40, 20, 30, 30);
+                printer.setChar("Task: " + uniqueCodeArray[2], 40, 70, 30, 30);
+                printer.setChar("SN0: " + uniqueCodeArray[3], 40, 120, 30, 30);
+                printer.setChar("SN: " + uniqueCodeArray[4], 40, 170, 30, 30);
+                printer.setText("生产日期: 2022-11-29", 40, 220, 30, 30, 20, 1, 1, 22);
+                //		printer.setText("生产日期:", 40, 220, 56, 56, 30, 2, 2, 24);
+                //		printer.setChar("2019", 120, 230, 40, 40);
+                //条码
+                //		String data = "9Y203CCNW9A019396";//条码内容
+                //		String dataZpl = "^FO260,250^BY2,11.0,10,40^BCC,80,N^FD${data}^FS";//条码样式模板
+                //		printer.setBarcode(data, dataZpl);
+                //二维码
+                String ewm = "9Y203CCNW9A019396";//条码内容
+                String ewmZpl = "^FO430,50^BQ,2,6^CI26^FH^FDQA^FD${data}^FS";//条码样式模板
+                printer.setBarCode(ewm, ewmZpl);
+                printer.resetZpl();
+                printer.setText("批号:", 20, 550, 56, 56, 30, 2, 2, 24);
+                printer.setChar("78787878788", 200, 560, 40, 40);
+                //下边的条码
+                String bar2 = "00000999990018822969";//20位
+                String bar2Paper = "^FO260,250^BY2,3.0,66^BCN,,Y,N,N^FD${data}^FS";//条码样式模板
+                printer.setBarCode(bar2, bar2Paper);
+                String zpl = printer.getZpl();
+                return printer.print(zpl);
+            }
         }
-        return abytes;
-    }
+        return false;
 
-    //^B3N,Y,Y  code39条形码
-    public static boolean printBarcode(ZplPrinter p, String uniqueCode) {
-        //1.打印单个条码
-        String bar0 = uniqueCode;//条码内容
-        String str = "云箭集团";
-        String bar0Zpl = "^FO190,90^BY1.4,3,80^BCN,60,N,N,N^FD${data}^FS";//条码样式模板
-        p.setBarcode(bar0, bar0Zpl);
-        p.setCharR(bar0,200,60,25,25);
-        content += "^CI26^SEE:GB18030.DAT^CWJ,E:MSUNG24^CI28^FO160,20^A1I,10,10^FD"+str+"^FS";
-        content += "^FO500,90^BY1.4,3,80^BCN,60,N,N,N^FD"+bar0+"^FS^FO510,60^A0I,25,25^FD"+bar0+"^FS^FT680,20^A1I,10,10^FD"+str+"^FS";
-
-        String zpl = p.getZpl();
-        System.out.println(zpl);
-        boolean result = p.print(zpl);//打印
-        return result;
     }
 
     public static void main(String[] args) {
-        ZplPrinter p = new ZplPrinter("\\\\192.168.11.247\\ZDesigner ZT210-200dpi ZPL");
-        p.printBarcode(p, "D21066000101");
-
+        ZplPrinter printer = new ZplPrinter("HP LaserJet 1020");
+        printBarcode(printer, "K&DD_202221390963&TASK_20222139490279&kT_DH001&KT001&000002");
+//        ZplPrinter printer = new ZplPrinter("HP LaserJet 1020");
+//        /**
+//         * 文本
+//         */
+//        printer.setChar("SN: 9Y203CCNW9A19396", 40, 20, 30, 30);
+//        printer.setChar("MAC: 74EE2AEB1A52", 40, 70, 30, 30);
+//        printer.setChar("DID: 9FCNY21EZJ460B190916", 40, 120, 30, 30);
+//        printer.setChar("CMIIT: 2016DP9142", 40, 170, 30, 30);
+////		printer.setText("生产日期:2019", 40, 220 ,30, 30, 20, 1 , 1, 22);
+////		printer.setText("生产日期:", 40, 220, 56, 56, 30, 2, 2, 24);
+////		printer.setChar("2019", 120, 230, 40, 40);
+//        //条码
+////		String data = "9Y203CCNW9A019396";//条码内容
+////		String dataZpl = "^FO260,250^BY2,11.0,10,40^BCC,80,N^FD${data}^FS";//条码样式模板
+////		printer.setBarcode(data, dataZpl);
+//        //二维码
+//        String ewm = "9Y203CCNW9A019396";//条码内容
+//        String ewmZpl = "^FO430,50^BQ,2,6^CI26^FH^FDQA^FD${data}^FS";//条码样式模板
+//        printer.setBarCode(ewm, ewmZpl);
+//        printer.resetZpl();
+//        printer.setText("批号:", 20, 550, 56, 56, 30, 2, 2, 24);
+//        printer.setChar("78787878788", 200, 560, 40, 40);
+////		printer.setText("批号:", 20, 220, 56, 56, 30, 1, 1, 24);
+////		printer.setChar("78787878788", 120, 230, 40, 40);
+//        //下边的条码
+//        String bar2 = "00000999990018822969";//20位
+//        String bar2Paper = "^FO260,250^BY2,3.0,66^BCN,,Y,N,N^FD${data}^FS";//条码样式模板
+//        printer.setBarCode(bar2, bar2Paper);
+//        String zpl = printer.getZpl();
+////		System.out.println(zpl);
+//        printer.print(zpl);
     }
 }
+

Algunos archivos no se mostraron porque demasiados archivos cambiaron en este cambio