Java调用Zebra条码打印机打印条码、中英文数字条码混合标签,可自由控制格式和排版

来源:互联网 发布:python 爬虫 金融数据 编辑:程序博客网 时间:2024/04/28 09:18

最近公司要做一个条码标签打印的软件,故特此研究了一下Zebra条码打印机,粗略了解了一下ZPL语言,Download了几个Demo,但始终觉得这些Demo不规范、不全面,问题很多,于是自己抽时间整理了一下。代码注释我写得比较清楚,无需太多解释,一看便知。如果要对元素格式进行修改,需要对zpl语言有所了解。

功能要点:

1.Java调用打印机

2.打印单个条码

3.打印中文字符

4.打印中文、英文、数字、条码混合标签

打印效果:




[java] view plain copy
  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.IOException;  
  4. import java.io.UnsupportedEncodingException;  
  5.   
  6. import javax.print.Doc;  
  7. import javax.print.DocFlavor;  
  8. import javax.print.DocPrintJob;  
  9. import javax.print.PrintException;  
  10. import javax.print.PrintService;  
  11. import javax.print.PrintServiceLookup;  
  12. import javax.print.SimpleDoc;  
  13. import javax.print.attribute.standard.PrinterName;  
  14.   
  15. public class ZplPrinter {  
  16.     private String printerURI = null;//打印机完整路径  
  17.     private PrintService printService = null;//打印机服务  
  18.     private byte[] dotFont;  
  19.     private String begin = "^XA";   //标签格式以^XA开始  
  20.     private String end = "^XZ";     //标签格式以^XZ结束  
  21.     private String content = "";  
  22.   
  23.     public static void main(String[] args) {  
  24.         ZplPrinter p = new ZplPrinter("\\\\192.168.0.12\\ZDesigner 105SLPlus-300dpi ZPL");  
  25.         //1.打印单个条码  
  26.         String bar0 = "1234567890";//条码内容  
  27.         String bar0Zpl = "^FO110,110^BY6,3.0,280^BCN,,Y,N,N^FD${data}^FS";//条码样式模板  
  28.         p.setBarcode(bar0, bar0Zpl);  
  29.         String zpl = p.getZpl();  
  30.         System.out.println(zpl);  
  31.         boolean result1 = p.print(zpl);//打印  
  32.           
  33.         p.resetZpl();//注意要清除上次的打印信息  
  34.         //2.打印中、英、数字、条码混合  
  35.         //左边的条码  
  36.         String bar1 = "07";  
  37.         p.setChar(bar1, 1901306060);  
  38.         String bar1Zpl = "^FO100,200^BY8,3.0,240^BCR,,N,N,N^FD${data}^FS";//条码样式模板  
  39.         p.setBarcode(bar1,bar1Zpl);  
  40.         //下边的条码  
  41.         String bar2 = "00000999990018822969";//20位  
  42.         String bar2Paper = "^FO380,600^BY3,3.0,100^BCN,,Y,N,N^FD${data}^FS";//条码样式模板  
  43.         p.setBarcode(bar2,bar2Paper);  
  44.           
  45.         p.setText("国药控股湖南有限公司"380406060302224);  
  46.         p.setChar("CSS0BPKPPR"3801006060);  
  47.           
  48.         p.setText("09件",940806060302224);  
  49.         p.setText("补"1100806060302224);  
  50.           
  51.         p.setText("公司自配送 公路"3801808080303324);  
  52.         p.setChar("03231151",9401874040);  
  53.         p.setChar("2015-10-10",9402273030);  
  54.           
  55.         p.setText("湖南六谷大药房连锁有限公司"3802606060302224);  
  56.           
  57.         p.setText("长沙市开福区捞刀河镇中岭村258号"3803206060302222);  
  58.           
  59.         p.setText("多SKU"8004856060302224);  
  60.           
  61.         p.setText("库位:49"3804205656302224);  
  62.         p.setText("品类:感冒胶囊"3804855656302224);  
  63.           
  64.         p.setText("批号:"3805505656302224);  
  65.         p.setChar("78787878788"5005604040);  
  66.           
  67.         String zpl2 = p.getZpl();  
  68.         System.out.println(zpl2);  
  69.         boolean result2 = p.print(zpl2);  
  70.     }  
  71.   
  72.     /** 
  73.      * 构造方法 
  74.      * @param printerURI 打印机路径 
  75.      */  
  76.     public ZplPrinter(String printerURI){  
  77.         this.printerURI = printerURI;  
  78.         //加载字体  
  79.         File file = new File("C://ts24.lib");  
  80.         if(file.exists()){  
  81.             FileInputStream fis;  
  82.             try {  
  83.                 fis = new FileInputStream(file);  
  84.                 dotFont = new byte[fis.available()];  
  85.                 fis.read(dotFont);  
  86.                 fis.close();  
  87.             } catch (IOException e) {  
  88.                 e.printStackTrace();  
  89.             }  
  90.         }else{  
  91.             System.out.println("C://ts24.lib文件不存在");  
  92.         }  
  93.         //初始化打印机  
  94.         PrintService[] services = PrintServiceLookup.lookupPrintServices(null,null);  
  95.         if (services != null && services.length > 0) {  
  96.             for (PrintService service : services) {  
  97.                 if (printerURI.equals(service.getName())) {  
  98.                     printService = service;  
  99.                     break;  
  100.                 }  
  101.             }  
  102.         }  
  103.         if (printService == null) {  
  104.             System.out.println("没有找到打印机:["+printerURI+"]");  
  105.             //循环出所有的打印机  
  106.             if (services != null && services.length > 0) {  
  107.                 System.out.println("可用的打印机列表:");  
  108.                 for (PrintService service : services) {  
  109.                     System.out.println("["+service.getName()+"]");  
  110.                 }  
  111.             }  
  112.         }else{  
  113.             System.out.println("找到打印机:["+printerURI+"]");  
  114.             System.out.println("打印机名称:["+printService.getAttribute(PrinterName.class).getValue()+"]");  
  115.         }  
  116.     }  
  117.     /** 
  118.      * 设置条形码 
  119.      * @param barcode 条码字符 
  120.      * @param zpl 条码样式模板 
  121.      */  
  122.     public void setBarcode(String barcode,String zpl) {  
  123.         content += zpl.replace("${data}", barcode);  
  124.     }  
  125.   
  126.     /** 
  127.      * 中文字符、英文字符(包含数字)混合 
  128.      * @param str 中文、英文 
  129.      * @param x x坐标 
  130.      * @param y y坐标 
  131.      * @param eh 英文字体高度height 
  132.      * @param ew 英文字体宽度width 
  133.      * @param es 英文字体间距spacing 
  134.      * @param mx 中文x轴字体图形放大倍率。范围1-10,默认1 
  135.      * @param my 中文y轴字体图形放大倍率。范围1-10,默认1 
  136.      * @param ms 中文字体间距。24是个比较合适的值。 
  137.      */  
  138.     public void setText(String str, int x, int y, int eh, int ew, int es, int mx, int my, int ms) {  
  139.         byte[] ch = str2bytes(str);  
  140.         for (int off = 0; off < ch.length;) {  
  141.             if (((int) ch[off] & 0x00ff) >= 0xA0) {  
  142.                 int qcode = ch[off] & 0xff;  
  143.                 int wcode = ch[off + 1] & 0xff;  
  144.                 content += String.format("^FO%d,%d^XG0000%01X%01X,%d,%d^FS\n", x, y, qcode, wcode, mx, my);  
  145.                 begin += String.format("~DG0000%02X%02X,00072,003,\n", qcode, wcode);  
  146.                 qcode = (qcode + 128 - 32) & 0x00ff;  
  147.                 wcode = (wcode + 128 - 32) & 0x00ff;  
  148.                 int offset = ((int) qcode - 16) * 94 * 72 + ((int) wcode - 1) * 72;  
  149.                 for (int j = 0; j < 72; j += 3) {  
  150.                     qcode = (int) dotFont[j + offset] & 0x00ff;  
  151.                     wcode = (int) dotFont[j + offset + 1] & 0x00ff;  
  152.                     int qcode1 = (int) dotFont[j + offset + 2] & 0x00ff;  
  153.                     begin += String.format("%02X%02X%02X\n", qcode, wcode, qcode1);  
  154.                 }  
  155.                 x = x + ms * mx;  
  156.                 off = off + 2;  
  157.             } else if (((int) ch[off] & 0x00FF) < 0xA0) {  
  158.                 setChar(String.format("%c", ch[off]), x, y, eh, ew);  
  159.                 x = x + es;  
  160.                 off++;  
  161.             }  
  162.         }  
  163.     }  
  164.     /** 
  165.      * 英文字符串(包含数字) 
  166.      * @param str 英文字符串 
  167.      * @param x x坐标 
  168.      * @param y y坐标 
  169.      * @param h 高度 
  170.      * @param w 宽度 
  171.      */  
  172.     public void setChar(String str, int x, int y, int h, int w) {  
  173.         content += "^FO" + x + "," + y + "^A0," + h + "," + w + "^FD" + str + "^FS";  
  174.     }  
  175.     /** 
  176.      * 英文字符(包含数字)顺时针旋转90度 
  177.      * @param str 英文字符串 
  178.      * @param x x坐标 
  179.      * @param y y坐标 
  180.      * @param h 高度 
  181.      * @param w 宽度 
  182.      */  
  183.     public void setCharR(String str, int x, int y, int h, int w) {  
  184.         content += "^FO" + x + "," + y + "^A0R," + h + "," + w + "^FD" + str + "^FS";  
  185.     }  
  186.     /** 
  187.      * 获取完整的ZPL 
  188.      * @return 
  189.      */  
  190.     public String getZpl() {  
  191.         return begin + content + end;  
  192.     }  
  193.     /** 
  194.      * 重置ZPL指令,当需要打印多张纸的时候需要调用。 
  195.      */  
  196.     public void resetZpl() {  
  197.         begin = "^XA";  
  198.         end = "^XZ";  
  199.         content = "";  
  200.     }  
  201.     /** 
  202.      * 打印 
  203.      * @param zpl 完整的ZPL 
  204.      */  
  205.     public boolean print(String zpl){  
  206.         if(printService==null){  
  207.             System.out.println("打印出错:没有找到打印机:["+printerURI+"]");  
  208.             return false;  
  209.         }  
  210.         DocPrintJob job = printService.createPrintJob();  
  211.         byte[] by = zpl.getBytes();  
  212.         DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;  
  213.         Doc doc = new SimpleDoc(by, flavor, null);  
  214.         try {  
  215.             job.print(doc, null);  
  216.             System.out.println("已打印");  
  217.             return true;  
  218.         } catch (PrintException e) {  
  219.             e.printStackTrace();  
  220.             return false;  
  221.         }  
  222.     }  
  223.     /** 
  224.      * 字符串转byte[] 
  225.      * @param s 
  226.      * @return 
  227.      */  
  228.     private byte[] str2bytes(String s) {  
  229.         if (null == s || "".equals(s)) {  
  230.             return null;  
  231.         }  
  232.         byte[] abytes = null;  
  233.         try {  
  234.             abytes = s.getBytes("gb2312");  
  235.         } catch (UnsupportedEncodingException ex) {  
  236.             ex.printStackTrace();  
  237.         }  
  238.         return abytes;  
  239.     }  
  240. }  


阅读全文
0 0
原创粉丝点击