斑马打印机-RFID打印机 ZT411R最全攻略,从0上手 通过【JAVA生成ZPL指令+ts24.lib+ip:端口】的方式调用打印,无须安装驱动
前言:本文如標(biāo)題所言,主要分享如何通過(guò)JAVA調(diào)用斑馬的RFID進(jìn)行打印
1.準(zhǔn)備
? ? ? ? 首先得準(zhǔn)備好你的RFID打印機(jī)、本文實(shí)例用得是斑馬ZT411 RFID版。根據(jù)打印機(jī)操作手冊(cè)完成打印機(jī)得安裝,其中包括標(biāo)簽和色帶安裝、以及打印機(jī)得配網(wǎng)(推薦配置固定IP)。
下載?斑馬打印機(jī)連接工具,方便配置打印機(jī),以及測(cè)試ZPL指令
? ? ? ? 下載后根據(jù)打印機(jī)具體型號(hào)選擇安裝相應(yīng)的驅(qū)動(dòng)方便測(cè)試(說(shuō)明:java中連接打印機(jī)是不需要安裝驅(qū)動(dòng)的)
下載?ZebraDesigner for Developers 3 條碼標(biāo)簽編輯軟件
**下載**?ts24.lib斑馬打印機(jī)點(diǎn)陣字體-Java文檔類資源-CSDN下載,無(wú)需積分 將下載完的ts24.lib文件放在好記住的位置后文代碼中會(huì)使用該文件的地址。?
下載?斑馬打印機(jī)ZPL中文指令手冊(cè)-Java文檔類資源-CSDN下載?無(wú)需積分
2.開(kāi)始打印
1.打印工具類
import com.szzl.cms.stock.domain.HcBind; import com.szzl.common.utils.DateUtils; import com.szzl.common.utils.StringUtils; import com.szzl.system.service.ISysConfigService; import com.zebra.sdk.comm.*; import com.zebra.sdk.printer.PrinterStatus; import com.zebra.sdk.printer.ZebraPrinter; import com.zebra.sdk.printer.ZebraPrinterFactory; import com.zebra.sdk.printer.ZebraPrinterLanguageUnknownException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import java.io.*; import java.util.HashMap; import java.util.Map;/*** <p>RFID打印機(jī)測(cè)試</p>** @author : LIn* @date : 2022-06-17 11:14**/ @Service public class ZplPrinter {@Autowiredprivate ISysConfigService configService;private byte[] dotFont;/*** 標(biāo)簽?zāi)0?/public static String printModel(HcBind hcBind) {ZplPrinter p = new ZplPrinter();StringBuilder begin = new StringBuilder();StringBuilder content = new StringBuilder();// 耗材名稱PrintDataBackDTO name = p.getText("名稱:" + hcBind.getName(), 54, 70);begin.append(name.getBegin());content.append(name.getContent());// 耗材規(guī)格PrintDataBackDTO spec = p.getText("規(guī)格:" + hcBind.getSpec(), 54, 120);begin.append(spec.getBegin());content.append(spec.getContent());// 廠家PrintDataBackDTO manufacturer = p.getText("廠家:" + hcBind.getManufacturer(), 54, 170);begin.append(manufacturer.getBegin());content.append(manufacturer.getContent());// 批次PrintDataBackDTO batchNo = p.getText("批次:" + hcBind.getBatchNo(), 54, 220);begin.append(batchNo.getBegin());content.append(batchNo.getContent());// 效期PrintDataBackDTO effectDate = p.getText("效期:" + DateUtils.parseDateToStr("yyyy-MM-dd",hcBind.getEffectDate()), 54, 270);begin.append(effectDate.getBegin());content.append(effectDate.getContent());//返回EPC在這里設(shè)置 (),,到^FS前可填入?yún)?shù)名稱 "^RS8 ^RFR,H,0,8,E^FN1^FS^HV1,,EPC:^FS" ^RS8,,,1表示這里的1表示只讀一個(gè)標(biāo)簽content.append("^RS8,,,1 ^RFR,H,0,8,E^FN1^FS^HV1,,^FS");// 條形碼content.append("^BY2,3,104^FT791,297^BCN,,Y,N^FH\\^FD>;").append(hcBind.getUniqueCode()).append("^FS");///二維碼content.append("^FT1015,313^BQN,2,5^FH\\^FDLA,").append(hcBind.getUniqueCode()).append("^FS");///EPC打印出來(lái)content.append("^FPH,6^FT54,320^AQN,28,9^FN2^FS^FN2^RFR,H,2,16,E^FS");//打印數(shù)量content.append("^PQ1,0,1,Y");return begin.toString() + content.toString() + "^XZ";}/*** 打印并獲取Epc*/public Map<String,Object> printGetEpc(HcBind hcBind) throws ConnectionException {String sendStr = printModel(hcBind);String printerIp = configService.selectConfigByKey("rfid.printer.ip");Connection connection = ConnectionBuilder.build("TCP_MULTI:" + printerIp + ":9100:9200");Map<String,Object> map = new HashMap<String, Object>();try {connection.open();//獲得tid指令//String date="^XA^RS8^RFR,H,0,12,E^FN1^FS^HV1,,TID:^FS^XZ";byte[] sendAndWaitForResponse = connection.sendAndWaitForResponse(sendStr.getBytes(),8000,8000,"" );StringBuilder epcBuilder = new StringBuilder();for (byte b : sendAndWaitForResponse) {char c = (char) b;String str = String.valueOf(c);epcBuilder.append(str);}String epc = epcBuilder.toString();//判斷EPC是否為空 并且長(zhǎng)度是否為24 這里根據(jù)實(shí)際標(biāo)簽信息決定if(StringUtils.isNotEmpty(epc) && epc.length() == 24){map.put("dealState", "success");map.put("epc", epc);}else{map.put("dealState", "error");map.put("dealMsg","獲取標(biāo)簽RFID失敗,請(qǐng)檢查打印機(jī)打印機(jī)狀態(tài),并完成RFID校準(zhǔn)");}} catch (ConnectionException e) {map.put("dealState", "error");map.put("dealMsg","打印出現(xiàn)異常,請(qǐng)聯(lián)系管理員");} finally {connection.close();}return map;}/*** 根據(jù)ip獲得打印機(jī)狀態(tài)* @return* @throws ConnectionException*/public Map<String,Object> getPrinterStatus() throws ConnectionException { // String printerIp = configService.selectConfigByKey("rfid.printer.ip");//打印機(jī)連接信息Connection connection = new TcpConnection("192.168.36.95", TcpConnection.DEFAULT_ZPL_TCP_PORT);//返回打印機(jī)狀態(tài)用Map<String,Object> map = new HashMap<String, Object>();map.put("dealState", "success");try {//打開(kāi)與打印機(jī)的鏈接connection.open();//獲得打印機(jī)工廠ZebraPrinter zPrinter = ZebraPrinterFactory.getInstance(connection);// 獲得打印機(jī)狀態(tài)/**{"isHeadCold":false, 是否過(guò)冷"isHeadOpen":false, 是否打開(kāi)"isHeadTooHot":false, 是否過(guò)熱"isPaperOut":false, 是否缺紙"isPartialFormatInProgress":false, 是否正在進(jìn)行格式化"isPaused":true, 暫停"isReadyToPrint":false, 準(zhǔn)備好打印"isReceiveBufferFull":false, 接收緩沖區(qū)滿了"isRibbonOut":false, 帶出"labelLengthInDots":1956, 用點(diǎn)標(biāo)注長(zhǎng)度"labelsRemainingInBatch":0, 批號(hào)仍在分批"numberOfFormatsInReceiveBuffer":0,接收緩沖區(qū)的格式數(shù)"printMode":"TEAR_OFF"} 打印模式** **/PrinterStatus currentStatus = zPrinter.getCurrentStatus();map.put("currentStatus", currentStatus);connection.close();} catch (ConnectionException | ZebraPrinterLanguageUnknownException e) {map.put("dealState", "error");map.put("dealMsg", e);} finally {//關(guān)閉打印機(jī)連接connection.close();}return map;}/*** 構(gòu)造方法**/public ZplPrinter() {File file = new File("C://ts24.lib");//這里放自己的ts24.lib位置FileInputStream fis;try {fis = new FileInputStream(file);dotFont = new byte[fis.available()];fis.read(dotFont);fis.close();} catch (IOException e) {e.printStackTrace();}}/*** 打印內(nèi)容** @param str 內(nèi)容* @param x x坐標(biāo)* @param y y坐標(biāo)*/private PrintDataBackDTO getText(String str, int x, int y) {PrintDataBackDTO result = new PrintDataBackDTO();StringBuilder begin = new StringBuilder();begin.append("^XA~SD25^PW1146^PR1,1");StringBuilder content = new StringBuilder();byte[] ch = str2bytes(str);for (int off = 0; off < ch.length; ) {if (((int) ch[off] & 0x00ff) >= 0xA0) {try {//中文字符int qcode = ch[off] & 0xff;int wcode = ch[off + 1] & 0xff;content.append(String.format("^FO%d,%d^XG0000%01X%01X,%d,%d^FS\n", x, y, qcode, wcode, 1, 1));begin.append(String.format("~DG0000%02X%02X,00072,003,\n", qcode, wcode));qcode = (qcode + 128 - 32) & 0x00ff;wcode = (wcode + 128 - 32) & 0x00ff;int offset = (qcode - 16) * 94 * 72 + (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.append(String.format("%02X%02X%02X\n", qcode, wcode, qcode1));}x = x + 24;off = off + 2;} catch (Exception e) {//替換成X號(hào)content.append(getChar("X", x, y, 30, 20));x = x + 15;off = off + 2;}} else if (((int) ch[off] & 0x00FF) < 0xA0) {content.append(getChar(String.format("%c", ch[off]), x, y, 30, 20));x = x + 15;off++;}}result.setBegin(begin);result.setContent(content);return result;}/*** 英文字符串(包含數(shù)字)** @param str 英文字符串* @param x x坐標(biāo)* @param y y坐標(biāo)* @param h 高度* @param w 寬度*/private String getChar(String str, int x, int y, int h, int w) {return "^FO" + x + "," + y + "^A0," + h + "," + w + "^FD" + str + "^FS";}/*** 字符串轉(zhuǎn)byte[]*/private byte[] str2bytes(String s) {if (null == s || "".equals(s)) {return null;}byte[] abytes = null;try {abytes = s.getBytes("gb2312");} catch (UnsupportedEncodingException ex) {System.out.println("字符串轉(zhuǎn)byte出錯(cuò)");}return abytes;} } //調(diào)用示例 Map<String, Object> printResult = zplPrinter.printGetEpc(hcBind);2.打印對(duì)象根據(jù)需求自定義printModel方法
3.使用ZebraDesigner 3 for Developers生成.prn文件,用記事本打開(kāi),拷貝后 打開(kāi)Zebra Setup Utilities
?打開(kāi)后拷入ZPL指令
?點(diǎn)擊 【發(fā)送到打印機(jī)】按鈕 即可測(cè)試打印
4.調(diào)好的位置信息根據(jù)需求放入printModel方法中
總結(jié)
以上是生活随笔為你收集整理的斑马打印机-RFID打印机 ZT411R最全攻略,从0上手 通过【JAVA生成ZPL指令+ts24.lib+ip:端口】的方式调用打印,无须安装驱动的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 设计模式学习笔记-装饰模式
- 下一篇: c语言里 如何取得线程的lpparame