Bläddra i källkod

斑马打印测试

zhuhao 2 år sedan
förälder
incheckning
a25e542807

+ 2 - 3
imcs-admin-boot/imcs-business-biz/src/main/java/com/github/zuihou/business/operationManagementCenter/service/impl/TaskNodeServiceImpl.java

@@ -2457,12 +2457,11 @@ public class TaskNodeServiceImpl extends SuperServiceImpl<TaskNodeMapper, TaskNo
 
     @Override
     public R printCode(Map<String, Object> map) {
-        ZplPrinter.init();
-        ZplPrinter.resetZpl();
+        ZplPrinter p = new ZplPrinter("\\192.168.11.247\\ZDesigner ZT210-200dpi ZPL");
         String content = map.containsKey("uniqueCode")? map.get("uniqueCode").toString() : "";
         if(StringUtil.isEmpty(content))
             return R.fail("编码为空");
-        return ZplPrinter.printer("", content.substring(0, 20))? R.success("打印成功"):R.fail("打印失败");
+        return ZplPrinter.printBarcode(p,content.substring(0, 19))? R.success("打印成功"):R.fail("打印失败");
     }
 
     private List<TaskNode> getFanManNodes(TaskNode taskNode){

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

@@ -1,104 +1,33 @@
 package com.github.zuihou.business.util;
 
-import com.github.zuihou.common.util.StringUtil;
-import lombok.Data;
-import lombok.extern.slf4j.Slf4j;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.stereotype.Component;
-
+import javax.print.*;
+import javax.print.attribute.standard.PrinterName;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 
-import javax.print.Doc;
-import javax.print.DocFlavor;
-import javax.print.DocPrintJob;
-import javax.print.PrintException;
-import javax.print.PrintService;
-import javax.print.PrintServiceLookup;
-import javax.print.SimpleDoc;
-import javax.print.attribute.standard.PrinterName;
-
 public class ZplPrinter {
-    private static Logger logger = LoggerFactory.getLogger(ZplPrinter.class);
-
-    //@Value("${zuihou.print.printCodePath}")
-    private static String printerURI;//打印机完整路径
-    private static PrintService printService;//打印机服务
-    private static byte[] dotFont;
-    private static String begin = "^XA";   //标签格式以^XA开始
-    private static String end = "^XZ";     //标签格式以^XZ结束
-    private static String content = "";
-
-    /**
-     * 斑马打印功能
-     * @param serviceUrl==》打印机服务地址
-     * @param materialCode ==》物料编码
-     * @return
-     */
-    public static boolean printer(String serviceUrl, String materialCode) {
-        ZplPrinter p = new ZplPrinter();
-        if(!StringUtil.isEmpty(serviceUrl)){
-            printerURI = serviceUrl;
-        }
-
-        //下边的条码
-        String bar2 = materialCode;//20位
-        String bar2Paper = "^FO380,600^BY3,3.0,100^BCN,,Y,N,N^FD${data}^FS";//条码样式模板
-        p.setBarcode(bar2, bar2Paper);
-
-        p.setText("云箭集团", 380, 260, 60, 60, 30, 2, 2, 24);
-        p.setText("物料编码", 380, 485, 56, 56, 30, 2, 2, 24);
-        p.setChar(materialCode, 500, 560, 40, 40);
-
-        String zpl2 = p.getZpl();
-        return p.print(zpl2);
-    }
-
-    public static void main(String[] args) {
-        //ZplPrinter p = new ZplPrinter("\\\\192.168.11.247\\ZDesigner ZT210-200dpi ZPL");
-        ZplPrinter p = new ZplPrinter();
-        printerURI = "\\\\192.168.11.247\\ZDesigner ZT210-200dpi ZPL";
-        //1.打印单个条码
-//        String bar0 = "1234567890";//条码内容
-//        String bar0Zpl = "^FO110,110^BY6,3.0,280^BCN,,Y,N,N^FD${data}^FS";//条码样式模板
-//        p.setBarcode(bar0, bar0Zpl);
-//        String zpl = p.getZpl();
-//        System.out.println(zpl);
-//        boolean result1 = p.print(zpl);//打印
-//
-//        p.resetZpl();//注意要清除上次的打印信息
-//        //2.打印中、英、数字、条码混合
-//        //左边的条码
-//        String bar1 = "07";
-//        p.setChar(bar1, 190, 130, 60, 60);
-//        String bar1Zpl = "^FO100,200^BY8,3.0,240^BCR,,N,N,N^FD${data}^FS";//条码样式模板
-//        p.setBarcode(bar1, bar1Zpl);
-        p.resetZpl();//注意要清除上次的打印信息
-        //下边的条码
-        String bar2 = "00000999990018822969";//20位
-        String bar2Paper = "^FO380,600^BY3,3.0,100^BCN,,Y,N,N^FD${data}^FS";//条码样式模板
-        p.setBarcode(bar2, bar2Paper);
-
-        p.setText("云箭集团", 380, 260, 60, 60, 30, 2, 2, 24);
-
-        p.setText("品类:前格框", 380, 485, 56, 56, 30, 2, 2, 24);
-        p.setChar("78787878788", 500, 560, 40, 40);
-
-        String zpl2 = p.getZpl();
-        //System.out.println(zpl2);
-        boolean result2 = p.print(zpl2);
-    }
+    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 String content = "";        //打印内容
+    private String message = "";        //打印的结果信息
 
     /**
-     * 构造方法     *
+     * 构造方法
+     *
+     * @param printerURI 打印机路径
      */
-    public static void init() {
+    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()) {
@@ -112,20 +41,10 @@ public class ZplPrinter {
                 e.printStackTrace();
             }
         } else {
-            System.out.println("ts24.lib文件不存在");
+            System.out.println("C://ts24.lib文件不存在");
         }
-
-        printerURI = "\\\\192.168.11.247\\ZDesigner ZT210-200dpi ZPL";
-        //1.打印单个条码
-        String bar0 = "1234567890";//条码内容
-        String bar0Zpl = "^FO110,110^BY6,3.0,280^BCN,,Y,N,N^FD${data}^FS";//条码样式模板
-        ZplPrinter.setBarcode(bar0, bar0Zpl);
-        ZplPrinter.setText("云箭集团", 380, 260, 60, 60, 30, 2, 2, 24);
-
-        ZplPrinter.setText("品类:前格框", 380, 485, 56, 56, 30, 2, 2, 24);
-        ZplPrinter.setChar("78787878788", 500, 560, 40, 40);
-
         //初始化打印机
+        //AppContext.getAppContext().put(PrintServiceLookup.class.getDeclaredClasses()[0], null);//刷新打印机列表
         PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
         if (services != null && services.length > 0) {
             for (PrintService service : services) {
@@ -148,7 +67,6 @@ public class ZplPrinter {
             System.out.println("找到打印机:[" + printerURI + "]");
             System.out.println("打印机名称:[" + printService.getAttribute(PrinterName.class).getValue() + "]");
         }
-
     }
 
     /**
@@ -157,7 +75,7 @@ public class ZplPrinter {
      * @param barcode 条码字符
      * @param zpl     条码样式模板
      */
-    public static void setBarcode(String barcode, String zpl) {
+    public void setBarcode(String barcode, String zpl) {
         content += zpl.replace("${data}", barcode);
     }
 
@@ -174,26 +92,36 @@ public class ZplPrinter {
      * @param my  中文y轴字体图形放大倍率。范围1-10,默认1
      * @param ms  中文字体间距。24是个比较合适的值。
      */
-    public static void setText(String str, int x, int y, int eh, int ew, int es, int mx, int my, int ms) {
+    public void setText(String str, int x, int y, int eh, int ew, int es, int mx, float my, int ms) {
         byte[] ch = str2bytes(str);
         for (int off = 0; off < ch.length; ) {
-            if (((int) ch[off] & 0x00ff) >= 0xA0) {
-                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);
-                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;
-                    begin += String.format("%02X%02X%02X\n", qcode, wcode, qcode1);
+            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);
+                    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;
+                        begin += String.format("%02X%02X%02X\n", qcode, wcode, qcode1);
+                    }
+                    x = x + ms * mx;
+                    off = off + 2;
+                } catch (Exception e) {
+                    e.printStackTrace();
+                    //替换成X号
+                    setChar("X", x, y, eh, ew);
+                    x = x + es;//注意间距更改为英文字符间距
+                    off = off + 2;
                 }
-                x = x + ms * mx;
-                off = off + 2;
-            } else if (((int) ch[off] & 0x00FF) < 0xA0) {
+            } else if (((int) ch[off] & 0x00FF) < 0xA0) {//英文字符
                 setChar(String.format("%c", ch[off]), x, y, eh, ew);
                 x = x + es;
                 off++;
@@ -207,15 +135,16 @@ public class ZplPrinter {
      * @param str 英文字符串
      * @param x   x坐标
      * @param y   y坐标
-     * @param h   高度
+     * @param h   高度  H
      * @param w   宽度
      */
-    public static void setChar(String str, int x, int y, int h, int w) {
-        content += "^FO" + x + "," + y + "^A0," + h + "," + w + "^FD" + str + "^FS";
+    public void setChar(String str, double x, double y, int h, int w) {
+        content += "^FO" + x + "," + y + "^AQN," + h + "," + w + "^FD" + str + "^FS";
     }
 
+
     /**
-     * 英文字符(包含数字)顺时针旋转90度
+     * 英文字符(包含数字)顺时针旋转180度
      *
      * @param str 英文字符串
      * @param x   x坐标
@@ -223,8 +152,8 @@ public class ZplPrinter {
      * @param h   高度
      * @param w   宽度
      */
-    public static void setCharR(String str, int x, int y, int h, int w) {
-        content += "^FO" + x + "," + y + "^A0R," + h + "," + w + "^FD" + str + "^FS";
+    public void setCharR(String str, int x, int y, int h, int w) {
+        content += "^FO" + x + "," + y + "^A0I," + h + "," + w + "^FD" + str + "^FS";
     }
 
     /**
@@ -232,15 +161,15 @@ public class ZplPrinter {
      *
      * @return
      */
-    public static String getZpl() {
+    public String getZpl() {
         return begin + content + end;
     }
 
     /**
      * 重置ZPL指令,当需要打印多张纸的时候需要调用。
      */
-    public static void resetZpl() {
-        begin = "^XA";
+    public void resetZpl() {
+        begin = "^XA" + darkness + width;
         end = "^XZ";
         content = "";
     }
@@ -250,9 +179,10 @@ public class ZplPrinter {
      *
      * @param zpl 完整的ZPL
      */
-    public static boolean print(String zpl) {
+    public boolean print(String zpl) {
         if (printService == null) {
-            System.out.println("打印出错:没有找到打印机:[" + printerURI + "]");
+            message = "打印出错:没有找到打印机[" + printerURI + "]";
+            System.out.println("打印出错:没有找到打印机[" + printerURI + "]");
             return false;
         }
         DocPrintJob job = printService.createPrintJob();
@@ -261,21 +191,28 @@ public class ZplPrinter {
         Doc doc = new SimpleDoc(by, flavor, null);
         try {
             job.print(doc, null);
+            message = "已打印";
             System.out.println("已打印");
             return true;
         } catch (PrintException e) {
+            message = "打印出错:" + e.getMessage();
+            System.out.println("打印出错:" + e.getMessage());
             e.printStackTrace();
             return false;
         }
     }
 
+    public String getMessage() {
+        return message;
+    }
+
     /**
      * 字符串转byte[]
      *
      * @param s
      * @return
      */
-    private static byte[] str2bytes(String s) {
+    private byte[] str2bytes(String s) {
         if (null == s || "".equals(s)) {
             return null;
         }
@@ -287,4 +224,24 @@ public class ZplPrinter {
         }
         return abytes;
     }
-}
+
+    //^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);
+
+        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("Designer ZT210-200dpi ZPL");
+        p.printBarcode(p, "D21066000101");
+
+    }
+}