近期因?yàn)轫?xiàng)目的需求,需要使用到打印機(jī)來打印業(yè)務(wù)相關(guān)的條形碼和其他信息,由于之前有操作其它打印機(jī)的經(jīng)驗(yàn)赏僧,Leader就安排我來做這個(gè)了(湊哦巴席,這能說我是懵逼的么)历涝。于是就開始了我的探索之旅啦,不對漾唉,是踩坑之旅荧库,總的來說還是蠻順利的,這里就稍微總結(jié)一下經(jīng)驗(yàn)赵刑。
ZPL(Zebra Programming Language)是斑馬公司自主設(shè)計(jì)的語言(斑馬公司的業(yè)務(wù)主要是制作斑馬條形碼打印機(jī))分衫。如今大部分條碼打印機(jī)都是能夠識別ZPL指令的,我們能夠用ZPL指令編寫一個(gè)模板般此,然后將自己主動生成的條形碼值(字符串)依照一定格式格式化成新的字符串蚪战。然后將這些內(nèi)容傳入打印機(jī)就可以。
下面是ZPL語言的含義:
^XA——開始標(biāo)簽格式
^LH0,0——打印的原點(diǎn)位置
^F0203,203——文本開始位置
^ADN,30,30——字體類型與大小
^FDExampleString——打印正文字符串铐懊,F(xiàn)D后為打印的內(nèi)容
^FS ——無特殊含義邀桑,一般用在一段指令段的結(jié)尾
^XZ ——結(jié)束標(biāo)簽格式
^BY2.0,3.0——條碼線條的粗細(xì)
^B7N,5,3,,,N ——二維碼的長寬比
^BCN,120,Y,N,N,A——條形碼的高度
了解上面的這些指令之后就可以寫一個(gè)完整的指令,來打印條形碼科乎。
^XA^LH10,10^FO90,60^ADN,20,10^BY2.0,3.0^BCN,120,Y,N,N,A^FDL000001^FS^XZ
除了上面的指令之外壁畸,當(dāng)然還需要指令的發(fā)出者——后臺代碼,這里我是用Android(Java代碼)實(shí)現(xiàn)的喜喂,下面貼出代碼瓤摧,希望能給有需要的人一些參考竿裂。
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){ //新開線程中執(zhí)行打印操作 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"); //和打印機(jī)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); } } /** * 發(fā)送打印指令到打印機(jī) * @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()); } }}
調(diào)用打印機(jī)打印條形碼:PrinterHelper.printStr("L000001");
注意:1,打印機(jī)必須要和發(fā)指令的設(shè)備(比如手機(jī)照弥,掃描機(jī))聯(lián)網(wǎng)腻异,可以通過wifi或者藍(lán)牙,建議使用藍(lán)牙这揣,因?yàn)楸容^穩(wěn)定悔常。
2,使用上面代碼記得導(dǎo)入相關(guān)的jar包(在build.gradle里加入api files('libs/ZSDK_ANDROID_API.jar')的dependency)给赞。
碼字不易机打,如果覺得有幫助,一定要給我點(diǎn)贊喲~~
不然信不信我砸了你家燈片迅,半夜偷親你 ( ̄ε  ̄) !!!