ZPL語言完成條形碼的打印


近期因為項目的需求,需要使用到打印機來打印業務相關的條形碼和其他信息,由於之前有操作其它打印機的經驗,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)。

 

碼字不易,如果覺得有幫助,一定要給我點贊喲~~

不然信不信我砸了你家燈,半夜偷親你 ( ̄ε  ̄) !!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM