近期因为项目的需求,需要使用到打印机来打印业务相关的条形码和其他信息,由于之前有操作其它打印机的经验,Leader就安排我来做这个了(凑哦,这能说我是懵逼的么)。于是就开始了我的探索之旅啦,不对,是踩坑之旅,总的来说还是蛮顺利的,这里就稍微总结一下经验。
ZPL(Zebra Programming Language)是斑马公司自主设计的语言(斑马公司的业务主要是制作斑马条形码打印机)。如今大部分条码打印机都是能够识别ZPL指令的,我们能够用ZPL指令编写一个模板,然后将自己主动生成的条形码值(字符串)依照一定格式格式化成新的字符串。然后将这些内容传入打印机就可以。
下面是ZPL语言的含义:
^XA——开始标签格式
^LH0,0——打印的原点位置
^F0203,203——文本开始位置
^ADN,30,30——字体类型与大小
^FDExampleString——打印正文字符串,FD后为打印的内容
^FS ——无特殊含义,一般用在一段指令段的结尾
^XZ ——结束标签格式
^BY2.0,3.0——条码线条的粗细
^B7N,5,3,,,N ——二维码的长宽比
^BCN,120,Y,N,N,A——条形码的高度
了解上面的这些指令之后就可以写一个完整的指令,来打印条形码。
^XA^LH10,10^FO90,60^ADN,20,10^BY2.0,3.0^BCN,120,Y,N,N,A^FDL000001^FS^XZ
打印结果
除了上面的指令之外,当然还需要指令的发出者——后台代码,这里我是用Android(Java代码)实现的,下面贴出代码,希望能给有需要的人一些参考。
import com.tao.admin.loglib.Logger;
import com.zebra.sdk.comm.BluetoothConnection;
import com.zebra.sdk.comm.Connection;
import com.zebra.sdk.comm.ConnectionException;
import com.zebra.sdk.comm.TcpConnection;
import com.zebra.sdk.printer.PrinterLanguage;
import com.zebra.sdk.printer.ZebraPrinter;
import com.zebra.sdk.printer.ZebraPrinterFactory;
import com.zebra.sdk.printer.ZebraPrinterLanguageUnknownException;
public class PrinterHelper {
private static ZebraPrinter printer;
private static Connection printerConnection;
public static void printStr(final String printStr){
//新开线程中执行打印操作
new Thread(new Runnable() {
@Override
public void run() {
printer = connect();
if (printer != null) {
sendLabel(printer,printerConnection,printStr);
} else {
disconnect(printerConnection);
}
} }).start();
}
public static ZebraPrinter connect() {
printerConnection = null;
try {
int port = Integer.parseInt("9100");
//和打印机1对1匹配
printerConnection = new TcpConnection("10.240.161.228", port);
} catch (NumberFormatException e) {
Logger.e("Printer Error 1", e.getMessage());
return null;
}
try {
printerConnection.open();
} catch (ConnectionException e) {
Logger.e("Printer Error 2-1", e.getMessage());
PrinterHelper.disconnect(printerConnection);
}
ZebraPrinter printer = null;
if (printerConnection.isConnected()) {
try {
printer = ZebraPrinterFactory.getInstance(printerConnection);
PrinterLanguage pl = printer.getPrinterControlLanguage();
} catch (ConnectionException e) {
Logger.e("Printer Error 2-2", e.getMessage());
printer = null;
PrinterHelper.disconnect(printerConnection);
} catch (ZebraPrinterLanguageUnknownException e) {
Logger.e("Printer Error 3", e.getMessage());
printer = null;
PrinterHelper.disconnect(printerConnection);
}
}
return printer;
}
private static void sendLabel(ZebraPrinter printer,Connection printerConnection,String printStr) {
try {
byte[] configLabel = getConfigLabel(printer,printerConnection,printStr);
printerConnection.write(configLabel);
if (printerConnection instanceof BluetoothConnection) {
String friendlyName = ((BluetoothConnection) printerConnection).getFriendlyName();
}
} catch (ConnectionException e) {
Logger.e("Printer Error 2-3", e.getMessage());
} finally {
disconnect(printerConnection);
}
}
/**
* 发送打印指令到打印机
* @return
*/
private static byte[] getConfigLabel(ZebraPrinter printer,Connection printerConnection,String printStr) {
PrinterLanguage printerLanguage = printer.getPrinterControlLanguage();
byte[] configLabel = null;
if (printerLanguage == PrinterLanguage.ZPL) {
Logger.e("Print Language","ZPL");
configLabel = ("^XA^LH10,10^FO90,60^ADN,20,10^BY2.0,3.0^BCN,120,Y,N,N,A^FD"+printStr+"^FS^XZ").getBytes();
} else if (printerLanguage == PrinterLanguage.CPCL) {
Logger.e("Print Language","CPCL");
String cpclConfigLabel = "! 0 200 200 406 1\r\n" + "ON-FEED IGNORE\r\n" + "BOX 20 20 380 380 8\r\n" + "T 0 6 137 177 TEST\r\n" + "PRINT\r\n"; configLabel = cpclConfigLabel.getBytes();
}
return configLabel;
}
public static void disconnect(Connection printerConnection) {
try {
if (printerConnection != null) {
printerConnection.close();
}
} catch (ConnectionException e) {
Logger.e("Printer Error 2-4", e.getMessage());
}
}
}
调用打印机打印条形码:PrinterHelper.printStr("L000001");
注意:1,打印机必须要和发指令的设备(比如手机,扫描机)联网,可以通过wifi或者蓝牙,建议使用蓝牙,因为比较稳定。
2,使用上面代码记得导入相关的jar包(在build.gradle里加入api files('libs/ZSDK_ANDROID_API.jar')的dependency)。
码字不易,如果觉得有帮助,一定要给我点赞哟~~
不然信不信我砸了你家灯,半夜偷亲你 ( ̄ε  ̄) !!!