java向Zebra打印机发送zpl指令打印条码

kingschan 2016-12-06 09:05:20 阅读 2058 评论 0 喜欢 0
zpl 

最近在做扫描枪的项目,需要用java控制斑马打印机自动打印条码,我研究了一下Zebra条码打印机,粗略了解了一下ZPL语言并实现了一个demo供大家参考

java代码

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.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.standard.MediaPrintableArea;
public class ZplPrinter {
private String printerURI = null;//打印机完整路径
private PrintService printService = null;//打印机服务
private byte[] dotFont;
private String begin = "^XA"; //标签格式以^XA开始
private String end = "^XZ"; //标签格式以^XZ结束
private static String content = "";
public static void main(String[] args) throws IOException {
ZplPrinter p = new ZplPrinter("\\\\192.168.168.126\\ZDesigner ZT410-300dpi ZPL1");
p.resetZpl();
String content_str="##|200050|25100400001|100|20161019|201101-03|820005016101900393##";
//F0 x坐标,y坐标
String qrcode_t ="^FO120,60^BQ,2,10^FDQA,${data}^FS";
p.setBarcode(content_str,qrcode_t);
p.setText("1.9L草菇老抽250ML", 550, 60, 60, 60, 30, 2, 2, 20);
p.setText("特级金标生抽 6箱", 550, 120, 60, 60, 30, 2, 2, 20);
content+="^FO560,60^ABN,20,20^FD820000314^FS";
content+="^FO660,60^ABN,20,20^FD010800559^FS";
p.setChar("820000314010800559", 560, 60, 30, 30);
p.setChar("820000314010800559", 550, 190, 45, 45);
p.setChar("2013322113", 550, 240, 50, 50);
p.setText("二维码备注", 550, 290, 40, 40, 30, 2, 2, 24);
content+="^PQ5";//打印5张
String zpl2 = p.getZpl();
boolean result2 = p.print(zpl2);
}
/**
* 构造方法
* @param printerURI 打印机路径
*/
public ZplPrinter(String printerURI){
this.printerURI = printerURI;
//加载字体
File file = new File("C://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文件不存在");
}
printService = PrintServiceLookup.lookupDefaultPrintService();
}
/**
* 设置条形码
* @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) {
String string =filterSpecialChar(str);//过滤特殊字符
byte[] ch = str2bytes(string);
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);
}
x = x + ms * mx;
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";
end = "^XZ";
content = "";
}
/**
* 打印
* @param zpl 完整的ZPL
*/
public boolean print(String zpl){
if(printService==null){
System.out.println("打印出错:没有找到打印机:["+printerURI+"]");
return false;
}
DocPrintJob job = printService.createPrintJob();
byte[] by = zpl.getBytes();
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
DocAttributeSet das = new HashDocAttributeSet();
das.add(new MediaPrintableArea(0, 0, 1, 45, MediaPrintableArea.MM));
Doc doc = new SimpleDoc(by, flavor, das);
try {
job.print(doc, null);
System.out.println("已打印");
return true;
} catch (PrintException e) {
e.printStackTrace();
return false;
}
}
/**
* 字符串转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 content
* @return
*/
public static String filterSpecialChar(String content){
StringBuffer sb = new StringBuffer();
String[] array=content.split("");
for (String str : array) {
if (str.matches("\\w|[\u4e00-\u9fa5]|\\*|\\.")) {
sb.append(str);
}
}
return sb.toString();
}
}

效果如下:

java斑马打印机打钱条码
java斑马打印机打钱条码

需要注意点如下

  • 用指令位置要慢慢高很麻烦
  • 要用到ts24.lib 转义库
  • 要在电脑上添加默认打印机(把斑马设置为默认打印机)
  • 学习zpl指令就可以让打印机打出你想要的内容

把斑马设置为默认打印机

指令转义库:

ts24.lib

zpl指令手册

支持@用户名回复

×  

:bowtie: :smile: :laughing: :blush: :smiley: :relaxed: :smirk: :heart_eyes: :kissing_heart: :kissing_closed_eyes: :flushed: :relieved: :satisfied: :grin: :wink: :stuck_out_tongue_winking_eye: :stuck_out_tongue_closed_eyes: :grinning: :kissing: :kissing_smiling_eyes: :stuck_out_tongue: :sleeping: :worried: :frowning: :anguished: :open_mouth: :grimacing: :confused: :hushed: :expressionless: :unamused: :sweat_smile: :sweat: :weary: :pensive: :disappointed: :confounded: :fearful: :cold_sweat: :persevere: :cry: :sob: :joy: :astonished: :scream: :neckbeard: :tired_face: :angry: :rage: :triumph: :sleepy: :yum: :mask: :sunglasses: :dizzy_face: :imp: :smiling_imp: :neutral_face: :no_mouth: :innocent: :alien: :yellow_heart: :blue_heart: :purple_heart: :heart: :green_heart: :broken_heart: :heartbeat: :heartpulse: :two_hearts: :revolving_hearts: :cupid: :sparkling_heart: :sparkles: :star: :star2: :dizzy: :boom: :collision: :anger: :exclamation: :question: :grey_exclamation: :grey_question: :zzz: :dash: :sweat_drops: :notes: :musical_note: :fire: :hankey: :poop: :shit: :+1: :thumbsup: :-1: :thumbsdown: :ok_hand: :punch: :facepunch: :fist: :v: :wave: :hand: :open_hands: :point_up: :point_down: :point_left: :point_right: :raised_hands: :pray: :point_up_2: :clap: :muscle: :metal: :walking: :runner: :running: :couple: :family: :two_men_holding_hands: :two_women_holding_hands: :dancer: :dancers: :ok_woman: :no_good: :information_desk_person: :raised_hand: :bride_with_veil: :person_with_pouting_face: :person_frowning: :bow: :couplekiss: :couple_with_heart: :massage: :haircut: :nail_care: :boy: :girl: :woman: :man: :baby: :older_woman: :older_man: :person_with_blond_hair: :man_with_gua_pi_mao: :man_with_turban: :construction_worker: :cop: :angel: :princess: :smiley_cat: :smile_cat: :heart_eyes_cat: :kissing_cat: :smirk_cat: :scream_cat: :crying_cat_face: :joy_cat: :pouting_cat: :japanese_ogre: :japanese_goblin: :see_no_evil: :hear_no_evil: :speak_no_evil: :guardsman: :skull: :feet: :lips: :kiss: :droplet: :ear: :eyes: :nose: :tongue: :love_letter: :bust_in_silhouette: :busts_in_silhouette: :speech_balloon: :thought_balloon:

共0条评论

文章导航

  1. java代码
  2. 效果如下:
  3. 需要注意点如下
  4. 指令转义库:

相似文章

  • 顶部
  • 关灯
  • 留言
  • 点赞