开发中用到了斑马打印机打印标签,这次开发中只是用来打文字,没有生成条形码,所以在别人的基础上我又改动了一下。要参考打条形码的可以看代码中的其它相关方法。我用的是Zebra800,java调用斑马相关型号应该是类似,其它型号没有测试过

1、下载Zebra800驱动及ts24.lib文件

https://download.csdn.net/download/fmwind/11248115

2、工具类

package com.fh.util.print;
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;//import sun.awt.AppContext;
/*** 采用点阵字库ts24.lib* @author Labrador**/
public class ZplPrinter {private String printerURI = null;         //打印机完整路径private PrintService printService = null; //打印机服务private byte[] dotFont;private String darkness = "~SD22"; //Set Darkness设置色带颜色的深度 0-30private String width = "^PW1200";    //Print Width打印宽度0-1500private 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 ZplPrinter(String printerURI){this.printerURI = printerURI;//加载字体File file = new File("D:\\sjhfimage\\lib\\ts24.lib");if(file.exists()){FileInputStream fis;try {fis = new FileInputStream(file);dotFont = new byte[fis.available()];fis.read(dotFont);fis.close();} catch (IOException e) {e.printStackTrace();}}else{System.out.println("C://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 (printerURI.equals(service.getName())) {printService = service;break;}}}if (printService == null) {System.out.println("没有找到打印机:["+printerURI+"]");//循环出所有的打印机if (services != null && services.length > 0) {System.out.println("可用的打印机列表:");for (PrintService service : services) {System.out.println("["+service.getName()+"]");}}}else{System.out.println("找到打印机:["+printerURI+"]");System.out.println("打印机名称:["+printService.getAttribute(PrinterName.class).getValue()+"]");}}/*** 设置条形码* @param barcode 条码字符* @param zpl 条码样式模板*/public void setBarcode(String barcode,String zpl) {content += zpl.replace("${data}", barcode);}/*** 中文字符、英文字符(包含数字)混合* @param str 中文、英文* @param x x坐标* @param y y坐标* @param eh 英文字体高度height* @param ew 英文字体宽度width* @param es 英文字体间距spacing* @param mx 中文x轴字体图形放大倍率。范围1-10,默认1* @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, 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);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;}} else if (((int) ch[off] & 0x00FF) < 0xA0) {//英文字符setChar(String.format("%c", ch[off]), x, y, eh, ew);x = x + es;off++;}}}/*** 英文字符串(包含数字)* @param str 英文字符串* @param x   x坐标* @param y y坐标* @param h 高度* @param w 宽度*/public void setChar(String str, int x, int y, int h, int w) {content += "^FO" + x + "," + y + "^A0," + h + "," + w + "^FD" + str + "^FS";}/*** 英文字符(包含数字)顺时针旋转90度* @param str 英文字符串* @param x x坐标* @param y y坐标* @param h 高度* @param w 宽度*/public void setCharR(String str, int x, int y, int h, int w) {content += "^FO" + x + "," + y + "^A0R," + h + "," + w + "^FD" + str + "^FS";}/*** 获取完整的ZPL* @return*/public String getZpl() {return begin + content + end;}/*** 重置ZPL指令,当需要打印多张纸的时候需要调用。*/public void resetZpl() {begin = "^XA" + darkness + width;end = "^XZ";content = "";}/*** 打印* @param zpl 完整的ZPL*/public boolean print(String zpl){if(printService==null){message = "打印出错:没有找到打印机["+printerURI+"]";System.out.println("打印出错:没有找到打印机["+printerURI+"]");return false;}DocPrintJob job = printService.createPrintJob();byte[] by = zpl.getBytes();DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;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 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;}/*** * @param unit 单位名称* @param orderId 工单号* @param type 类别*/public static boolean printPicking(String unit,String orderId,String type){ZplPrinter p = new ZplPrinter("ZDesigner GT800 (ZPL)");p.setText(unit, 253, 26, 40, 40, 20, 1, 1, 24);p.setChar(orderId, 253, 76, 20, 20);p.setText(type, 253, 120, 53, 53, 20, 1, 1, 24);String zpl = p.getZpl();//System.out.println(zpl);boolean flag=p.print(zpl);return flag;}
}

安装打印机的过程中一定要看好打印机的名称,不然会提示找不到打印机。

3、测试类

package com.fh.util.print;
public class ZplPrinterTest {public static void main(String[] args) {ZplPrinter p = new ZplPrinter("ZDesigner GT800 (ZPL)");
//      ZplPrinter p = new ZplPrinter("\\\\10.10.13.224\\ZDesigner GT800 (ZPL)");//      printBarcode(p);
//      p.resetZpl();//清除
//      printPicking300DPI(p);
//      p.resetZpl();//清除//printPicking203DPI(p);
//      p.resetZpl();//清除printPicking(p);}private static boolean printPicking(ZplPrinter p){        p.setText("国药控股湖南有限公司", 253, 26, 40, 40, 20, 1, 1, 24);p.setChar("CSS0BPKPPR", 253, 66, 20, 20);p.setText("公司自配送 公路", 253, 120, 53, 53, 20, 1, 1, 24);String zpl = p.getZpl();System.out.println(zpl);boolean result = p.print(zpl);return result;}private static boolean printBarcode(ZplPrinter p){//1.打印单个条码String bar0 = "131ZA010101";//条码内容
//      String bar0Zpl = "^FO110,110^BY6,3.0,280^BCN,,Y,N,N^FD${data}^FS";//条码样式模板String bar0Zpl = "^FO100,50^AAN,100,110^BY6,3.0,280^BCN,,N,N,N^FD${data}^FS";//条码样式模板p.setBarcode(bar0, bar0Zpl);p.setChar(bar0,13,26,140,110);p.setText("上", 13, 66, 60, 60, 30, 4, 4, 24);String zpl = p.getZpl();System.out.println(zpl);boolean result = p.print(zpl);//打印return result;}private static boolean printPicking300DPI(ZplPrinter p){//左边的条码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);//下边的条码String bar2 = "123459999900188ABCDE";//20位String bar2Paper = "^FO380,600^BY3,3.0,100^BCN,,Y,N,N^FD${data}^FS";//条码样式模板p.setBarcode(bar2,bar2Paper);p.setText("国药控股湖南有限公司", 380, 40, 60, 60, 30, 2, 2, 24);p.setChar("CSS0BPKPPR", 380, 100, 60, 60);p.setText("09件",940, 80, 60, 60, 30, 2, 2, 24);p.setText("补", 1100, 80, 60, 60, 30, 2, 2, 24);p.setText("公司自配送 公路", 380, 180, 80, 80, 30, 3, 3, 24);p.setChar("03231151",940, 187, 40, 40);p.setChar("2015-10-10",940, 227, 30, 30);p.setText("湖南六谷大药房连锁有限公司", 380, 260, 60, 60, 30, 2, 2, 24);p.setText(":长沙市:开福区:捞刀河镇中岭村258号:", 380, 320, 60, 60, 30, 2, 2, 22);p.setText("多SKU", 800, 485, 60, 60, 30, 2, 2, 24);p.setText("库位:49", 380, 420, 56, 56, 30, 2, 2, 24);p.setText("品类:感冒胶囊", 380, 485, 56, 56, 30, 2, 2, 24);p.setText("批号:", 380, 550, 56, 56, 30, 2, 2, 24);p.setChar("78787878788", 500, 560, 40, 40);String zpl = p.getZpl();System.out.println(zpl);boolean result = p.print(zpl);return result;}private static boolean printPicking203DPI(ZplPrinter p){//左边的条码/** String bar1 = "07"; p.setChar(bar1, 126, 86, 40, 40); String bar1Zpl =* "^FO66,133^BY5,3.0,160^BCR,,N,N,N^FD${data}^FS";//条码样式模板* p.setBarcode(bar1,bar1Zpl); //下边的条码 String bar2 =* "00000999990018822969";//20位 String bar2Paper =* "^FO253,400^BY2,3.0,66^BCN,,Y,N,N^FD${data}^FS";//条码样式模板* p.setBarcode(bar2,bar2Paper);*/p.setText("国药控股湖南有限公司", 253, 26, 40, 40, 20, 1, 1, 24);p.setChar("CSS0BPKPPR", 253, 66, 20, 20);/** p.setText("09件",626, 53, 40, 40, 20, 1, 1, 24); p.setText("补", 733, 53, 40,* 40, 20, 1, 1, 24);*/p.setText("公司自配送 公路", 253, 120, 53, 53, 20, 1, 1, 24);/** p.setChar("03231151",626, 124, 26, 26); p.setChar("2015-10-10",626, 151, 20,* 20);* * p.setText("湖南六谷大药房连锁有限公司", 253, 173, 40, 40, 20, 1, 1, 24);* * p.setText("长沙市开福区捞刀河镇中岭村258号", 253, 213, 30, 30, 20, 1, 1, 22);* * p.setText("多SKU", 533, 323, 40, 40, 20, 1, 1, 24);* * p.setText("库位:49", 253, 280, 37, 37, 20, 1, 1, 24); p.setText("品类:感冒胶囊", 253,* 323, 37, 37, 20, 1, 1, 24);* * p.setText("批号:", 253, 366, 37, 37, 20, 1, 1, 24); p.setChar("78787878788",* 333, 373, 26, 26);*/String zpl = p.getZpl();System.out.println(zpl);boolean result = p.print(zpl);return result;}private static boolean printRestoking(ZplPrinter p){//上方的条码String bar1 = "1434455567773344abcd";String bar1Zpl = "^FO85,70^BY4,3.0,140^BCN,,Y,N,N^FD${data}^FS";//条码样式模板p.setBarcode(bar1,bar1Zpl);//源货位p.setText("来源:131ZA010101", 100, 320, 60, 60, 30, 2, 2, 24);//目标货位p.setText("目的:131ZA010102",640, 320, 60, 60, 30, 2, 2, 24);//货品编号p.setText("货品编号:YF10001", 100, 440, 60, 60, 30, 2, 2, 24);//件装数p.setText("补货数量:200", 640, 440, 60, 60, 30, 2, 2, 24);//品名p.setText("复方一支黄花:", 100, 580, 60, 60, 30, 2, 2, 24);String zpl = p.getZpl();System.out.println(zpl);boolean result = p.print(zpl);return result;}
}

参考:

https://blog.csdn.net/scholar_man/article/details/78784163

Java调用Zebra800条码打印机相关推荐

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

    最近公司要做一个条码标签打印的软件,故特此研究了一下Zebra条码打印机,粗略了解了一下ZPL语言,Download了几个Demo,但始终觉得这些Demo不规范.不全面,问题很多,于是自己抽时间整理了 ...

  2. Java调用TSC条码打印机接口打印条码和二维码

    公司新买了一台TSC条码打印机,型号:TSC TTP-244 PRO,让和现有资产管理系统对接,可以根据系统上的编码直接打印. 研究了几天,终于调试出来了,下边是代码,,目测可用: java后台调试代 ...

  3. java 调用博思得条码打印机

    效果:  打印文字.二维码.图片 准备工作: 1).JNative(JNative.jar.JNativeCpp.dll) 2).博思得dll文件(CDFPSK.dll)和API文档 import o ...

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

    https://blog.csdn.net/itsation/article/details/44857745#commentsedit

  5. JAVA 调用 DLL 文件 TSCLIB.DLL TSC打印机

    这是一段JAVA调用TSC条码打印机的代码:有详细的注释! 1,需要把dll文件拷贝到c:\windows\system32目录下 2,工程里面需要加载JNative.jar包(网上很多下载的) /* ...

  6. Java调用Bartender控制条码打印机

    官方提供的主要是C#支持. 基于java调用bartender二次开发官方给了一份1998年的J#代码,,,完全用不了,,,百度谷歌搜索万能的网友的答案,发现也没有可参考的.. 最后想到了之前用到了一 ...

  7. eclipse java调用c 代码吗_linux下通过eclipse开发用java调用c程序的方法

    linux下通过eclipse开发用java调用c程序的方法: 1.先建立好java工程并建立java文件如下: public class testso {     static {         ...

  8. Java调用C/C++编写的第三方dll动态链接库(zz)

    这里主要用的方法是JNI.在网上查资料时看到很多人说用JNI非常的复杂,不仅要看很多的文档,而且要非常熟悉C/C++编程.恐怕有很多人在看到诸如此类的评论时已经决定绕道用其他方法了.本文将做详细的介绍 ...

  9. 使用JNA,让java调用原生代码

    JNA定义: JNA:java Native Access,是SUN公司开发的基于JNI的框架.JNI使得Java可以调用原生的c或者c++代码. JNA与JNI(Java Native Interf ...

最新文章

  1. 提取nds游戏的音乐
  2. 面试题19:二叉树镜像
  3. 语言中要输出表格_C语言 | 表格输出若干人的信息
  4. Ubuntu下基本的命令总结
  5. mybatis的xml文件 比较详细的学习注意点
  6. MPMoviePlayerController属性,方法,通知整理
  7. 在.Net Core中使用Swagger制作接口文档
  8. spring 官方文档
  9. Atitit  如何让精灵控件运动
  10. M2Det 论文笔记
  11. 复杂网络-无标度网络matlab代码实现
  12. 统计素数并求和python_Python练习题4.2统计素数并求和
  13. .ps格式的文件怎么打开?
  14. RuntimeError: grad can be implicitly created only for scalar outputs
  15. 有关海盗湾墙外世界网站收藏
  16. Springboot配置保存日志文件
  17. 【CSS】笔记3-三大样式、盒子模型、PS、圆角、阴影
  18. [IOS APP]长恨歌-王安忆有声小说
  19. NOIP2010 提高组 复赛 translate 机器翻译
  20. 2022 CCPC 威海 赛后总结

热门文章

  1. 陌上花开,可缓缓归矣——2016年校招总结
  2. modelmapper属性匹配问题分析
  3. 游戏开发中的全栈开发者
  4. 第二章第三节(上)路径决策算法(动态规划)
  5. 戴尔 PowerEdge 14G 加速分布式存储解决方案 zData 提升性能
  6. 权威认证 | 云和恩墨zData Light存储管理软件获H3C兼容认证
  7. python编写程序计算复利-使用Python函数计算复利
  8. 0321 复利计算—贷款
  9. 美团,大众点评,58城市行政区域和商圈数据实现
  10. object_detection源码解析-box_list