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

本文主要是介绍Java调用Zebra条码打印机打印条码、中英文数字条码混合标签,可自由控制格式和排版,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近公司要做一个条码标签打印的软件,故特此研究了一下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. }  


这篇关于Java调用Zebra条码打印机打印条码、中英文数字条码混合标签,可自由控制格式和排版的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/334405

相关文章

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

Java.lang.InterruptedException被中止异常的原因及解决方案

《Java.lang.InterruptedException被中止异常的原因及解决方案》Java.lang.InterruptedException是线程被中断时抛出的异常,用于协作停止执行,常见于... 目录报错问题报错原因解决方法Java.lang.InterruptedException 是 Jav