日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ZPL语言完成条形码的打印

發布時間:2024/5/14 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)。

?

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

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

總結

以上是生活随笔為你收集整理的ZPL语言完成条形码的打印的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。