小票打印就是向打印設備發送控制打印格式的指令集,而這些打印格式需要去查詢對應打印機的API文檔,這里我把常用的api給封裝了一下
PrintFormatUtils.Java
/** * 打印格式 * Created by john on 17-3-23. */public class PrintFormatUtils { // 對齊方式 public static final int ALIGN_LEFT = 0; // 靠左 public static final int ALIGN_CENTER = 1; // 居中 public static final int ALIGN_RIGHT = 2; // 靠右 //字體大小 public static final int FONT_NORMAL = 0; // 正常 public static final int FONT_MIDDLE = 1; // 中等 public static final int FONT_BIG = 2; // 大 //加粗模式 public static final int FONT_BOLD = 0; // 字體加粗 public static final int FONT_BOLD_CANCEL = 1; // 取消加粗 /** * 打印二維碼 * @param qrCode * @return */ public static String getQrCodeCmd(String qrCode) { byte[] data; int store_len = qrCode.length() + 3; byte store_pL = (byte) (store_len % 256); byte store_pH = (byte) (store_len / 256); // QR Code: Select the model // Hex 1D 28 6B 04 00 31 41 n1(x32) n2(x00) - size of model // set n1 [49 x31, model 1] [50 x32, model 2] [51 x33, micro qr code] // https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=140 byte[] modelQR = {(byte)0x1d, (byte)0x28, (byte)0x6b, (byte)0x04, (byte)0x00, (byte)0x31, (byte)0x41, (byte)0x32, (byte)0x00}; // QR Code: Set the size of module // Hex 1D 28 6B 03 00 31 43 n // n depends on the printer // https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=141 byte[] sizeQR = {(byte)0x1d, (byte)0x28, (byte)0x6b, (byte)0x03, (byte)0x00, (byte)0x31, (byte)0x43, (byte)0x08}; // Hex 1D 28 6B 03 00 31 45 n // Set n for error correction [48 x30 -> 7%] [49 x31-> 15%] [50 x32 -> 25%] [51 x33 -> 30%] // https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=142 byte[] errorQR = {(byte)0x1d, (byte)0x28, (byte)0x6b, (byte)0x03, (byte)0x00, (byte)0x31, (byte)0x45, (byte)0x31}; // QR Code: Store the data in the symbol storage area // Hex 1D 28 6B pL pH 31 50 30 d1...dk // https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=143 // 1D 28 6B pL pH cn(49->x31) fn(80->x50) m(48->x30) d1…dk byte[] storeQR = {(byte)0x1d, (byte)0x28, (byte)0x6b, store_pL, store_pH, (byte)0x31, (byte)0x50, (byte)0x30}; // QR Code: Print the symbol data in the symbol storage area // Hex 1D 28 6B 03 00 31 51 m // https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=144 byte[] printQR = {(byte)0x1d, (byte)0x28, (byte)0x6b, (byte)0x03, (byte)0x00, (byte)0x31, (byte)0x51, (byte)0x30}; data = byteMerger(modelQR, sizeQR); data = byteMerger(data, errorQR); data = byteMerger(data, storeQR); data = byteMerger(data, qrCode.getBytes()); data = byteMerger(data, printQR); return new String(data); } /** * 打印條碼 * @param barcode * @return */ public static String getBarcodeCmd(String barcode) { // 打印 Code-128 條碼時需要使用字符集前綴 // "{A" 表示大寫字母 // "{B" 表示所有字母,數字,符號 // "{C" 表示數字,可以表示 00 - 99 的范圍 byte[] data; String btEncode; if (barcode.length() < 18) { // 字符長度小于15的時候直接輸出字符串 btEncode = "{B" + barcode; } else { // 否則做一點優化 int startPos = 0; btEncode = "{B"; for (int i = 0; i < barcode.length(); i++) { char curChar = barcode.charAt(i); if (curChar < 48 || curChar > 57 || i == (barcode.length() - 1)) { // 如果是非數字或者是最后一個字符 if (i - startPos >= 10) { if (startPos == 0) { btEncode = ""; } btEncode += "{C"; boolean isFirst = true; int numCode = 0; for (int j = startPos; j < i; j++) { if (isFirst) { // 處理第一位 numCode = (barcode.charAt(j) - 48) * 10; isFirst = false; } else { // 處理第二位 numCode += (barcode.charAt(j) - 48); btEncode += (char) numCode; isFirst = true; } } btEncode += "{B"; if (!isFirst) { startPos = i - 1; } else { startPos = i; } } for (int k = startPos; k <= i; k++) { btEncode += barcode.charAt(k); } startPos = i + 1; } } } // 設置 HRI 的位置,02 表示下方 byte[] hriPosition = {(byte) 0x1d, (byte) 0x48, (byte) 0x02}; // 最后一個參數表示寬度 取值范圍 1-6 如果條碼超長則無法打印 byte[] width = {(byte) 0x1d, (byte) 0x77, (byte) 0x02}; byte[] height = {(byte) 0x1d, (byte) 0x68, (byte) 0xfe}; // 最后兩個參數 73 : CODE 128 || 編碼的長度 byte[] barcodeType = {(byte) 0x1d, (byte) 0x6b, (byte) 73, (byte) btEncode.length()}; byte[] print = {(byte) 10, (byte) 0}; data = PrintFormatUtils.byteMerger(hriPosition, width); data = PrintFormatUtils.byteMerger(data, height); data = PrintFormatUtils.byteMerger(data, barcodeType); data = PrintFormatUtils.byteMerger(data, btEncode.getBytes()); data = PrintFormatUtils.byteMerger(data, print); return new String(data); } /** * 切紙 * @return */ public static String getCutPaperCmd() { // 走紙并切紙,最后一個參數控制走紙的長度 byte[] data = {(byte) 0x1d, (byte) 0x56, (byte) 0x42, (byte) 0x15}; return new String(data); } /** * 對齊方式 * @param alignMode * @return */ public static String getAlignCmd(int alignMode) { byte[] data = {(byte) 0x1b, (byte) 0x61, (byte) 0x0}; if (alignMode == ALIGN_LEFT) { data[2] = (byte) 0x00; } else if (alignMode == ALIGN_CENTER) { data[2] = (byte) 0x01; } else if (alignMode == ALIGN_RIGHT) { data[2] = (byte) 0x02; } return new String(data); } /** * 字體大小 * @param fontSize * @return */ public static String getFontSizeCmd(int fontSize) { byte[] data = {(byte) 0x1d, (byte) 0x21, (byte) 0x0}; if (fontSize == FONT_NORMAL) { data[2] = (byte) 0x00; } else if (fontSize == FONT_MIDDLE) { data[2] = (byte) 0x01; } else if (fontSize == FONT_BIG) { data[2] = (byte) 0x11; } return new String(data); } /** * 加粗模式 * @param fontBold * @return */ public static String getFontBoldCmd(int fontBold) { byte[] data = {(byte) 0x1b, (byte) 0x45, (byte) 0x0}; if (fontBold == FONT_BOLD) { data[2] = (byte) 0x01; } else if (fontBold == FONT_BOLD_CANCEL) { data[2] = (byte) 0x00; } return new String(data); } /** * 打開錢箱 * @return */ public static String getOpenDrawerCmd() { byte[] data = new byte[4]; data[0] = 0x10; data[1] = 0x14; data[2] = 0x00; data[3] = 0x00; return new String(data); } /** * 字符串轉字節數組 * @param str * @return */ public static byte[] stringToBytes(String str) { byte[] data = null; try { byte[] strBytes = str.getBytes("utf-8"); data = (new String(strBytes, "utf-8")).getBytes("gbk"); } catch (UnsupportedEncodingException exception) { exception.printStackTrace(); } return data; } /** * 字節數組合并 * @param bytesA * @param bytesB * @return */ public static byte[] byteMerger(byte[] bytesA, byte[] bytesB) { byte[] bytes = new byte[bytesA.length + bytesB.length]; System.arraycopy(bytesA, 0, bytes, 0, bytesA.length); System.arraycopy(bytesB, 0, bytes, bytesA.length, bytesB.length); return bytes; }}
有了打印格式,還要對具體的打印小票設置打印模板,主要就是利用上面的打印格式工具類,進行字符或字符串拼接,設置文字間空格的長度,以及使用換行符換行等。
有些小票打印的內容有可能是通用的,比如底部結束語亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
精品国内亚洲在观看18黄| 国产精品美女久久久久av超清| 欧美亚洲免费电影| 国产精品亚洲一区二区三区| 国产一区欧美二区三区| 97婷婷涩涩精品一区| 精品国产一区二区三区在线观看| 久久久精品久久久久| 国产精品中文字幕久久久| 国产日韩欧美影视| 欧美美最猛性xxxxxx| 羞羞色国产精品| 亚洲qvod图片区电影| 日本欧美精品在线| 亚洲人成电影在线观看天堂色| 国产精品 欧美在线| 亚洲福利视频在线| 久久久精品日本| 国产精品美乳一区二区免费| 欧美成人四级hd版| 成人免费直播live| 大伊人狠狠躁夜夜躁av一区| 欧美精品video| 精品国产一区二区在线| 欧美xxxwww| 国产自产女人91一区在线观看| 第一福利永久视频精品| 国产91精品在线播放| 亚洲男人av在线| 国产一区玩具在线观看| 久久综合国产精品台湾中文娱乐网| 欧美午夜精品在线| 国产精品美女主播在线观看纯欲| 亚洲福利视频在线| 91夜夜未满十八勿入爽爽影院| 欧美理论电影在线观看| 日韩在线视频导航| 91精品国产乱码久久久久久蜜臀| 久久99精品久久久久久噜噜| 欧美成人午夜免费视在线看片| 久久久噜噜噜久久久| 中文字幕亚洲二区| 亚洲欧美日韩天堂一区二区| 亚洲欧美日韩精品久久| 欧美日韩在线一区| 日韩精品在线免费播放| 欧洲美女免费图片一区| 亚洲电影天堂av| 91福利视频在线观看| 日韩电视剧免费观看网站| 精品久久久久久久久久久| 97国产精品人人爽人人做| 精品国产视频在线| 91色视频在线导航| 久久在线免费视频| 青青精品视频播放| 国产欧美日韩视频| 97欧美精品一区二区三区| 欧美综合激情网| 久久精品电影网| 欧美xxxx18性欧美| 久久亚洲精品中文字幕冲田杏梨| 国内精品久久久久久久| 亚洲欧美变态国产另类| 亚洲男人天堂古典| 精品av在线播放| 日韩在线视频一区| 日韩欧美主播在线| 欧美日韩视频在线| 2024亚洲男人天堂| xvideos成人免费中文版| 欧美一乱一性一交一视频| 成人激情视频小说免费下载| 日韩视频在线免费观看| 久久亚洲精品国产亚洲老地址| 国产精品日韩久久久久| 国产日韩精品视频| 久久天天躁日日躁| 青青草原成人在线视频| 亚洲一区制服诱惑| 97精品国产97久久久久久免费| 亚洲a∨日韩av高清在线观看| 日本精品一区二区三区在线播放视频| 日韩精品在线视频观看| 久久手机精品视频| 欧美成人国产va精品日本一级| 国产在线观看精品一区二区三区| 亚洲18私人小影院| 国产精品va在线播放我和闺蜜| 欧美性资源免费| 国内免费精品永久在线视频| 亚洲欧美激情另类校园| 黑人巨大精品欧美一区二区三区| 美女啪啪无遮挡免费久久网站| 精品国产精品自拍| 欧美噜噜久久久xxx| 久久影院模特热| 久久综合久久八八| 欧美日韩国产二区| www.美女亚洲精品| 欧美激情在线观看| 日本不卡视频在线播放| 成人免费高清完整版在线观看| 中文字幕欧美精品日韩中文字幕| 国内成人精品一区| 欧美在线视频在线播放完整版免费观看| 狠狠躁天天躁日日躁欧美| 亚洲永久免费观看| 日韩中文字幕在线观看| 日韩大陆欧美高清视频区| 亚洲精品福利视频| 久久久国产精品视频| 欧美视频中文在线看| 亚洲精品成人久久电影| 欧美丝袜一区二区三区| 欧美精品国产精品日韩精品| 亚洲人成绝费网站色www| 中文字幕日韩有码| 日韩中文字幕视频在线| 最近2019好看的中文字幕免费| 成人做爰www免费看视频网站| 日日摸夜夜添一区| 亚洲综合一区二区不卡| 久久久久久亚洲精品| 丝袜美腿亚洲一区二区| 久久久久久久久久久成人| 欧美精品一本久久男人的天堂| 国产精品第一视频| 亚洲国产精品字幕| 亚洲free性xxxx护士白浆| 亚洲日韩中文字幕在线播放| 国产成人鲁鲁免费视频a| 热久久视久久精品18亚洲精品| 欧美理论电影在线播放| 一区二区三区无码高清视频| 欧美成人精品影院| 日韩精品福利在线| 国产精品美女久久久免费| 久久久女女女女999久久| 青青青国产精品一区二区| 日韩视频免费看| 亚洲欧洲国产伦综合| 日韩av中文字幕在线| 欧美日韩国产色视频| 国产91精品不卡视频| 国产一区二区三区免费视频| 国产精品美女主播在线观看纯欲| 欧美专区国产专区| 欧美在线一级va免费观看| 国产精品日日摸夜夜添夜夜av| 91在线播放国产| www欧美xxxx| 久久精彩免费视频| 精品中文视频在线| 国产精品黄色影片导航在线观看| 亚洲综合在线小说| 91极品女神在线| 久久久久久久av| 亚洲精品久久久久中文字幕二区| 久久欧美在线电影| 欧美午夜美女看片| 精品亚洲男同gayvideo网站| 国产成人自拍视频在线观看| 亚洲一区制服诱惑|