可以參考:
ESC/P 打印指令使用,3種票據打印方法 https://blog.csdn.net/pfworld/article/details/2084666
Android 藍牙連接 ESC/POS 熱敏打印機打印(ESC/POS指令篇)https://www.jianshu.com/p/c0b6d1a4823b
ESC/POS指令集在android設備上使用實例(通過socket) https://www.cnblogs.com/kunyashaw/p/4314383.html
ESC/POS指令開發入門 https://download.csdn.net/download/shyyl/2004198
碼錶
PrinterCmdUtils.java
打印的工具類,註釋中詳細說明了每個指令對應的ESC編碼和功能
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
//
public class PrinterCmdUtils {
// ------------------------------------------一位命令-----------------------------------------------------
private static final byte DATA_ESC = 0x1B; // 【ESC】
private static final byte DATA_GS = 0x1D; // 【GS】
private static final byte DATA_LF = 0x0A; // 【LF】打印並走紙一行line feed, new line
private static final byte DATA_HT = 0x09; // 【HT】橫向跳格
private static final byte DATA_FF = 0x0C; // 【FF】打印並回到標準模式(在頁模式下)或打印並將標籤進紙到打印起始位置
private static final byte DATA_CR = 0x0D; // 【CR】打印並回車
private static final byte DATA_CAN = 0x18; // 【CAN】頁模式下刪除打印緩衝區內容
// ------------------------------------------二位命令-----------------------------------------------------
// 【ESC FF】 頁模式下打印
// 【ESC 2】 設置默認行高(默認行間距)
private static final byte DATA_ESC_2 = 0x32;
// 【ESC @】 初始化打印機
private static final byte DATA_ESC_INIT = 0x40; // 【@】ESC @ (initialize printer)
// 【ESC L】 選擇頁模式
private static final byte DATA_L = 0x4C;
// 【ESC S】 選擇標準模式
private static final byte DATA_S = 0x53;
// 【GS FF】 將標記打印紙打印到起始位置
// 【GS :】 開始/結束宏定義
private static final byte DATA_MAOHAO = 0x3A; // 【:】
// 【FS &】 設定漢字模式
// 【FS .】 取消漢字字符
private static final byte DATA_DIAN = 0x2E; // 【.】
// ------------------------------------------三位命令-----------------------------------------------------
// 【GS G n】 設置打印濃度
// 【ESC SP n】設置字符右間距(即字符間距)
private static final byte DATA_SP = 0x20; // 【SP】
/**
* 【ESC ! n】 選擇打印模式 設置字型(ESC M n),加粗(ESC E n),倍高倍寬(GS ! n),下劃線(ESC - n)
*/
// 【GS ! n】 選擇字符放大倍數
private static final byte DATA_PRINTMODE = 0x21; // 【!】
// 【DLE EOT n】 實時狀態傳送
private static final byte DATA_DLE = 0x10; // 【DLE】
private static final byte DATA_EOT = 0x04; // 【EOT】
// 【ESC % n】 選擇/取消自定義字符
private static final byte DATA_BAIFEN = 0x25; // 【%】
// 【ESC - n】 選擇/取消下劃線模式
private static final byte DATA_ESC_UNDERLINE = 0x2D; // 【-】ESC - n (turn underline mode on/off ---- 0or48: off,
// 1or49: on & 1-dot witdh, 2or50: on & 2-dot witdh)
// 【ESC 3 n】 設置行高(行間距)
private static final byte DATA_ESC_3 = 0x33;
// 【ESC c 3 n】未使用命令
private static final byte DATA_ESC_c_3 = 0x33; // ESC c 3 n (Select page sensors to output pager-end signal)
// 【ESC ? n】 取消用戶自定義字符
private static final byte DATA_WEN = 0x3F; // 【?】
// 【ESC E n】 選擇/取消加粗模式
private static final byte DATA_ESC_E = 0x45; // ESC E n (turn emphasized mode on-1/off-0)
// 【ESC G n】 選擇/取消雙重打印模式
private static final byte DATA_ESC_G = 0x47; // 【G】ESC G n (turn double-strike mode on-1/off-0)
// 【ESC a n】 選擇對齊方式
private static final byte DATA_ESC_a = 0x61; // 【a】ESC a n (select align justification --- default: 0, 0or48: Left,
// 1or49: Center, 2or50: Rigth)
// 【ESC J n】 打印並走紙
private static final byte DATA_J = 0x4A;
// 【ESC M n】 選擇字體
private static final byte DATA_ESC_M = 0x4D; // ESC M n (select character font --- default: 0, 0or48: font A, 1or49:
// font B, 2or50: font C)
// 【ESC T n】 在頁模式下選擇打印區域方向
private static final byte DATA_T = 0x54;
// 【ESC V n】 設置/解除順時針旋轉 90°
// 【GS V m】【GS V m n】 選擇裁紙模式並裁紙
private static final byte DATA_GS_V = 0x56; // 【V】GS V m (select cut mode and cut paper)
// 【ESC d n】 打印並向前走紙 n 行
private static final byte DATA_ESC_d = 0x64; // 【d】ESC d n (print and feed n lines)
// 【ESC t n】 設置代碼頁
private static final byte DATA_ESC_t = 0x74; // 【t】ESC t n (select character code table)
// 【ESC { n】 選擇/取消倒置打印模式
private static final byte DATA_ZUOKUO = 0x7B; // 【{】
// GS / m 打印下載位圖
private static final byte DATA_XIE = 0x2F; // 【/】
// 【GS B n】 選擇 / 取消黑白反顯打印模式
private static final byte DATA_GS_B = 0x42; // 【B】GS B n (turn white/black reverse printing mode on-1/off-0)
// 【GS f n】 選擇 HRI(Human Readable Interpretation) 字符字型
private static final byte DATA_f = 0x66; // 【f】
// 【GS H n】 選擇 HRI 字符的打印位置
private static final byte DATA_H = 0x48; // 【H】
// 【GS I n】 傳送打印機 ID
private static final byte DATA_I = 0x49; // 【I】
// 【GS T n】 設置打印位置爲打印行起點
// 【GS a n】 允許/禁止自動回覆狀態(ASB)
// 【GS h n】 選擇條碼高度
private static final byte DATA_GS_h = 0x68; // 【h】GS h n (select bar code height: default n=162, 1 <= n <= 255)
// 【GS w n】 設置條形碼寬度
// 【FS ! n】 設置漢字字符打印模式組合
// 【FS - n】 設置/取消漢字字符下劃線模式
// 【FS W n】 設置/解除四倍角中文打印
// 【1B 1B 91】 獲取切刀模式
// 【ESC ESC p】 檢查打印機是否處於空閒狀態
// 【ESC ESC q】 檢查打印機緩存是否有數據
// ------------------------------------------四位命令-----------------------------------------------------
// 【GS F nL nH】 設置串口
private static final byte DATA_F = 0x46;
// 【ESC $ nL nH】 設置橫向絕對打印位置
private static final byte DATA_$ = 0x24; // 【$】
// 【ESC D n1...nk NUL】 設置橫向跳格位置
private static final byte DATA_D = 0x44;
// 【ESC \ nL nH】 設置相對打印位置
private static final byte DATA_FANXIE = 0x5C; // 【\】
// 【ESC c 5 n】 激活/禁止面板按鍵
private static final byte DATA_ESC_c = 0x63;
private static final byte DATA_ESC_c_5 = 0x35; // ESC c 5 n
// 【FS p n m】 打印下載到 NV 存儲器中的位圖
private static final byte DATA_FS = 0x1C; // 【FS】
private static final byte DATA_FS_p = 0x70;
// 【GS $ nL nH】 頁模式下設置縱向絕對位置
// 【GS L nL nH】 設置左邊距
// 【GS W nL nH】 設置打印區域寬度
private static final byte DATA_W = 0x49; // 【W】
// 【GS \ nL nH】 頁模式下設置相對垂直打印位置
// 【1B 1B 90 n】 設置黑標模式
// ------------------------------------------五位命令-----------------------------------------------------
// 【GS ^ r t m】 運行宏
private static final byte DATA_JIAN = 0x5E; // 【^】
// 【FS 2 c1 c2 d1...dk】 定義用戶自定義漢字字符 32//【2】
// 【GS k m d1...dk NUL】【GS k m n d1...dn】打印條碼
// ------------------------------------------其他命令-----------------------------------------------------
// ESC & y c1 c2 [x1 d1...d(y × x1)]...[xk d1...d(y × xk)]定義用戶自定義字符
private static final byte DATA_AND = 0x26; // 【&】
// ESC * m nL nH d1... dk 選擇位圖模式
private static final byte DATA_XINHAO = 0x2A; // 【*】
// ESC W xL xH yL yH dxL dyL dyH 在頁模式下設置打印區域
// FS q n [xL xH yL yH d1...dk]1...[xL xH yL yH d1...dk]n 定義 NV 位圖
// <功能 065> GS ( k pL pH cn fn n (cn = 48, fn = 65) 設置打印數據區域的列數
private static final byte DATA_GS_k = 0x6B; // 【k】
// ------------------------------------------PDF417條形碼-------------------------------------------------
// <功能 066> GS ( k pL pH cn fn n (cn = 48, fn = 66) 設置行數
// <功能 067> GS ( k pL pH cn fn n (cn = 48, fn = 67) 設置模塊寬度
// <功能 068> GS ( k pL pH cn fn n (cn = 48, fn = 68) 設置行高
// <功能 069> GS ( k pL pH cn fn m n (cn = 48, fn = 69) 設置糾錯等級
// <功能 070> GS ( k pL pH cn fn m (cn = 48, fn = 70) 選擇可選項
// <功能 080> GS ( k pL pH cn fn m d1...dk (cn = 48, fn = 80) 存儲數據到符號存儲區
// <功能 081> GS ( k pL pH cn fn m (cn = 48, fn = 81) 打印在符號存貯區的符號數據
// <功能 082> GS ( k pL pH cn fn m (cn = 48, fn = 82) 傳送在符號存貯區的符號數據的大小信息
// GS * x y d1...d(x × y × 8) 定義下傳位圖
// GS v 0 m xL xH yL yH d1....dk 打印光柵位圖
// ----------------------------------------------------------------------------------------------------
public static final byte ESC = 27;// 換碼
public static final byte FS = 28;// 文本分隔符
public static final byte GS = 29;// 組分隔符
public static final byte DLE = 16;// 數據連接換碼
public static final byte EOT = 4;// 傳輸結束
public static final byte ENQ = 5;// 詢問字符
public static final byte SP = 32;// 空格
public static final byte HT = 9;// 橫向列表
public static final byte LF = 10;// 打印並換行(水平定位)
public static final byte CR = 13;// 歸位鍵
public static final byte FF = 12;// 走紙控制(打印並回到標準模式(在頁模式下) )
public static final byte CAN = 24;// 作廢(頁模式下取消打印數據 )
// --------------------FEED 鍵、 BOOT 鍵指令------------------------
/**
* 【GS G n】 設置打印濃度 灰度 第5位爲1 ,默認開啓紙將盡傳感器 0010 1100 輕度 44 0010 0000 正常濃度 32 0010
* 1000 稍深 40 0010 0100 深度 36
*/
public static byte[] printer_gray(int gray) {
byte[] result = new byte[3];
result[0] = GS;
result[1] = DATA_ESC_G;
switch (gray) {
case 1:
result[2] = (byte) 44;
break;
case 2:
result[2] = (byte) 32;
break;
case 3:
result[2] = (byte) 40;
break;
case 4:
result[2] = (byte) 36;
break;
}
return result;
}
// ------------------------打印機初始化-----------------------------
/**
* 打印機初始化 【ESC @】 初始化打印機
*/
public static byte[] init_printer() {
byte[] result = new byte[2];
result[0] = DATA_ESC;// 0x1B ESC
result[1] = DATA_ESC_INIT;// 0x40 64;
return result;
}
// ------------------------打印機狀態-----------------------------
/**
* 【DLE EOT n】 實時狀態傳送 實時地傳送打印機狀態。 參數 n 用來指定所要傳送的打印機狀態, 定義如下: n = 1: 傳輸打印機狀態 n =
* 2: 傳輸脫機狀態 n = 3: 傳輸錯誤狀態 n = 4: 傳輸捲紙傳感器狀態
*/
public static byte[] printer_status(int n) {
byte[] result = new byte[3];
result[0] = DATA_DLE;// 0x10 【DLE】
result[1] = DATA_EOT;// 0x04 【EOT】
result[2] = (byte) n;
return result;
}
// ------------------------打印文本-----------------------------
public static byte[] printText(String text) {
byte[] result = null;
try {
result = text.getBytes("GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}
// ------------------------換行-----------------------------
/**
* 換行 【LF】打印並走紙
*
* @param lineNum要換幾行
*/
public static byte[] nextLine(int lineNum) {
byte[] result = new byte[lineNum];
for (int i = 0; i < lineNum; i++) {
result[i] = DATA_LF;// 0x0A LF
}
return result;
}
/**
* 【ESC J n】 打印並走紙 打印緩衝區數據並走紙[n × 0.125mm]
*
* @param lineNum要換幾行
* @return
*/
public static byte[] nextLines(int lineNum) {
byte[] result = new byte[3];
result[0] = DATA_ESC;// 0x1B ESC
result[1] = DATA_J;// 0x4A
result[2] = (byte) lineNum;
return result;
}
/**
* 【ESC d n】 打印並向前走紙 n 行 打印緩存區裏的數據並向前走紙 n 行(字符行)
*
* @param lineNum要換幾行
* @return
*/
public static byte[] nextLines2(int lineNum) {
byte[] result = new byte[3];
result[0] = DATA_ESC;// 0x1B ESC
result[1] = DATA_ESC_d;// 0x64;
result[2] = (byte) lineNum;
return result;
}
// ------------------------製表-----------------------------
/**
* 製表
*
* @param length要幾個表格
* @return
*/
public static byte[] printTab(int length) {
byte[] result = new byte[length];
for (int i = 0; i < length; i++) {
result[i] = HT; // "\t";
}
return result;
}
// --------------------------打印模式------------------------------
/**
* 【ESC ! n】 選擇打印模式 設置字型(ESC M n),加粗(ESC E n),倍高倍寬(GS ! n),下劃線(ESC - n)
*/
/**
* 【FS ! n】 設置漢字字符打印模式組合 禁止或允許 倍高倍寬,下劃線
* 0000 0100允許倍寬 4
* 0000 1000允許倍高 8
* 1000 0000允許下劃線模式 128
*/
public static byte[] chinese_charmode(int mode) {
byte[] result = new byte[3];
result[0] = FS;
result[1] = DATA_PRINTMODE;
result[2] = (byte)mode;//128下劃線,12放大
return result;
}
/**
* 【FS &】 設定漢字模式
*/
// ------------------------下劃線-----------------------------
/**
* 繪製下劃線(1點寬) 【ESC - n】 選擇/取消下劃線模式 可以使用【ESC ! n】【FS ! n】【FS - n】設置或取消下劃線模式,
* 並且以最後收到的命令爲有效
*/
public static byte[] underlineWithOneDotWidthOn() {
byte[] result = new byte[3];
result[0] = ESC;
result[1] = 45;// 0x2D
result[2] = 1;
return result;
}
/**
* 繪製下劃線(2點寬) 【ESC - n】 選擇/取消下劃線模式
*
* @return
*/
public static byte[] underlineWithTwoDotWidthOn() {
byte[] result = new byte[3];
result[0] = ESC;
result[1] = 45;
result[2] = 2;
return result;
}
/**
* 取消繪製下劃線 【ESC - n】 選擇/取消下劃線模式
*
* @return
*/
public static byte[] underlineOff() {
byte[] result = new byte[3];
result[0] = ESC;
result[1] = 45;
result[2] = 0;
return result;
}
// ------------------------加粗-----------------------------
/**
* 選擇加粗模式 【ESC E n】 選擇/取消加粗模式
*/
public static byte[] boldOn() {
byte[] result = new byte[3];
result[0] = ESC;
result[1] = DATA_ESC_E;// 69;
result[2] = 0xF;
return result;
}
/**
* 取消加粗模式 【ESC E n】 選擇/取消加粗模式
*/
public static byte[] boldOff() {
byte[] result = new byte[3];
result[0] = ESC;
result[1] = DATA_ESC_E;// 69;
result[2] = 0;
return result;
}
// ------------------------對齊-----------------------------
/**
* 左對齊
*
* @return
*/
public static byte[] alignLeft() {
byte[] result = new byte[3];
result[0] = ESC;
result[1] = 97;
result[2] = 0;
return result;
}
/**
* 居中對齊
*
* @return
*/
public static byte[] alignCenter() {
byte[] result = new byte[3];
result[0] = ESC;
result[1] = 97;
result[2] = 1;
return result;
}
/**
* 右對齊
*
* @return
*/
public static byte[] alignRight() {
byte[] result = new byte[3];
result[0] = ESC;
result[1] = 97;
result[2] = 2;
return result;
}
/**
* 設置文本對齊方式,或設置條碼對齊方式
*
* @param align
* 打印位置 0:居左(默認) 1:居中 2:居右
* @throws IOException
*/
public static byte[] setAlignPosition(int align) {
byte[] result = new byte[3];
result[0] = DATA_ESC;// 0x1B ESC;
result[1] = DATA_ESC_a;// 0x61 97
result[2] = (byte) align;
return result;
}
/**
* 水平方向向右移動col列
*
* @param col
* @return
*/
public static byte[] set_HT_position(byte col) {
byte[] result = new byte[4];
result[0] = ESC;
result[1] = 68;
result[2] = col;
result[3] = 0;
return result;
}
// ------------------------字體大小-----------------------------
/**
* 【GS ! n】 選擇字符放大倍數 用n的0到3位選擇字符高度, 4到7位選擇字符寬度 【ESC ! n】也能設置字符大小, 最後被執行的指令有效
*/
public static byte[] fontSizeSet(int n) {
byte[] result = new byte[3];
result[0] = GS;
result[1] = DATA_PRINTMODE;// 0x21 【!】
result[2] = (byte) n;
return result;
}
/**
* 字體變大爲標準的n倍
*
* @param num
* @return
*/
public static byte[] fontSizeSetBig(int num) {
byte realSize = 0;
switch (num) {
case 1:
realSize = 0;
break;
case 2:
realSize = 17;
break;
case 3:
realSize = 34;
break;
case 4:
realSize = 51;
break;
case 5:
realSize = 68;
break;
case 6:
realSize = 85;
break;
case 7:
realSize = 102;
break;
case 8:
realSize = 119;
break;
}
byte[] result = new byte[3];
result[0] = 29;
result[1] = 33;
result[2] = realSize;
return result;
}
/**
* 字體取消倍寬倍高
*
* @param num
* @return
*/
public static byte[] fontSizeSetSmall(int num) {
byte[] result = new byte[3];
result[0] = ESC;
result[1] = 33;
return result;
}
// ------------------------設置左邊距---------------------------
/**
* 【GS L nL nH】 設置左邊距 用nL 和 nH設定左邊空白量,左邊空白量設置爲[(nL+nH×256)×0.125毫米]
*/
public static byte[] setMarginLeft(int nL, int nH) {
byte[] result = new byte[4];
result[0] = GS;
result[1] = DATA_L;// 0x4C 【L】
result[2] = (byte) nL;
result[3] = (byte) nH;
return result;
}
// ------------------------設置字符間距---------------------------
/**
* 設置字符右間距ESC SP n 該命令不影響漢字字符的設定
*/
public static byte[] setWordGap(int gap) {
byte[] result = new byte[3];
result[0] = DATA_ESC;
result[1] = DATA_SP;// 0x20
result[2] = (byte) gap;
return result;
}
// ------------------------設置行間距---------------------------
/**
* 【ESC 2】 設置默認行高
*
*/
public static byte[] setDefaultLineGap(int gap) {
byte[] result = new byte[2];
result[0] = DATA_ESC;// 0x1B
result[1] = DATA_ESC_2;// 0x32
return result;
}
/**
* 【ESC 3 n】 設置行高--行間距 設置行間距爲 [ n × 0.125mm]
*/
public static byte[] setLineGap(int gap) {
byte[] result = new byte[3];
result[0] = DATA_ESC;// 0x1B
result[1] = DATA_ESC_3;// 0x33
result[2] = (byte) gap;
return result;
}
// ------------------------選擇頁模式或標準模式---------------------------
/**
* 【ESC L】 選擇頁模式 從標準模式轉換到頁模式,該指令只在標準模式的行首有效 當執行 FF 或 ESC S 後, 打印機返回到標準模式
* 將下列指令的設置轉換到頁模式下的值: ①設置字符右間距: ESC SP ②設置行間距: ESC 2, ESC 3
*/
/**
* 【ESC S】 選擇標準模式
*
*/
// 【ESC T n】 在頁模式下選擇打印區域方向
// ------------------------打印位圖---------------------------
/**
* ESC * m nL nH d1... dk 選擇位圖模式
*/
public static byte[] chooseBitmap(int m) {
// TODO
return null;
}
/**
* FS q n [xL xH yL yH d1...dk]1...[xL xH yL yH d1...dk]n 定義 NV 位圖
*
*/
public static byte[] defineBitmap(int m) {
// TODO
return null;
}
/**
* GS / m 打印下載位圖 打印一幅下載位圖, 打印模式由m指定 m 模式 縱向分辨率(DPI) 橫向分辨率(DPI) 0,48 正常 203.2
* 203.2 1,49 倍寬 203.2 101.6 2,50 倍高 101.6 203.2 3,51 倍寬倍高 101.6 101.6
*/
public static byte[] printBitmap(int m) {
byte[] result = new byte[3];
result[0] = GS;
result[1] = DATA_XIE;
result[2] = (byte) m;
return result;
}
/**
* 【FS p n m】 打印下載到 NV 存儲器中的位圖 n表示位圖(由指令FS q定義) 的圖號 以m指定的模式打印下載到NV存儲器中的位圖 m 模式
* 縱向分辨率(DPI) 橫向分辨率(DPI) 0,48 正常 203.2 203.2 1,49 倍寬 203.2 101.6 2,50 倍高 101.6
* 203.2 3,51 倍寬倍高 101.6 101.6
*/
public static byte[] printBitmap(int n, int m) {
byte[] result = new byte[4];
result[0] = DATA_FS;
result[1] = DATA_FS_p;
result[2] = (byte) n;
result[3] = (byte) m;
return result;
}
// ------------------------設置打印區域---------------------------
// 設置打印區域寬度GS W nL nH
// 用nL 和 nH設定打印區域寬度,打印區域寬度設置爲[(nL+nH×256)×0.125毫米]
public static byte[] setPrintAreaWidth(int nL, int nH) {
byte[] result = new byte[4];
result[0] = DATA_GS;
result[1] = DATA_W;
result[2] = (byte) nL;
result[3] = (byte) nH;
return result;
}
// ------------------------條形碼---------------------------
// 設置條碼的位置--與設置文本對齊方式相同
// 設置條碼的寬度GS w n
public static byte[] setBarCodeWith(int width) {
byte[] result = new byte[3];
result[0] = DATA_GS;// 0x1D
result[1] = DATA_W;// 'W'
result[2] = (byte) width;
return result;
}
// 設置條碼的高度GS h n
public static byte[] setBarCodeHeight(int height) {
byte[] result = new byte[3];
result[0] = DATA_GS;
result[1] = DATA_GS_h;
result[2] = (byte) height;
return result;
}
// 條碼註釋打印在條碼位置GS H n
// 打印條碼時, 爲HRI字符選擇打印位置(HRI 是對條碼內容註釋的字符)
// 0, 48 不打印;1, 49 在條碼上方;2, 50 在條碼下方;3, 51 在條碼上方及下方
public static byte[] setHRILocation(int loc) {
byte[] result = new byte[3];
result[0] = DATA_GS;
result[1] = DATA_H;
result[2] = (byte) loc;
return result;
}
/**
* 選定條形碼系統(m值)並打印條碼
*
* 【GS k m d1...dk NUL】 該命令在這種格式下以 NUL 結束 0 UPC-A 11 ≤ k ≤ 12 48 ≤ d ≤ 57 1
* UPC-E 11 ≤ k ≤ 12 48 ≤ d ≤ 57 2 JAN13 (EAN13) 12 ≤ k ≤ 13 48 ≤ d ≤ 57 3 JAN 8
* (EAN8) 7 ≤ k ≤ 8 48 ≤ d ≤ 57 4 CODE39 1 ≤ k 48 ≤ d ≤ 57, 65 ≤ d ≤ 90, 32, 36,
* 37, 43, 45, 46, 47 5 ITF 1 ≤ k (偶數) 48 ≤ d ≤ 57 6 CODABAR 1 ≤ k 48 ≤ d ≤ 57,
* 65 ≤ d ≤ 68, 36, 43, 45, 46, 47, 58
*
* 【GS k m n d1...dn】 n用來指示條碼數據的個數, 打印機將其後邊 n 字節數據作爲條碼數據處理 65 UPC-A 11 ≤ n ≤ 12
* 48 ≤ d ≤ 57 66 UPC-E 11 ≤ n ≤ 12 48 ≤ d ≤ 57 67 JAN13 (EAN13) 12 ≤ n ≤ 13 48
* ≤ d ≤ 57 68 JAN 8 (EAN8) 7 ≤ n ≤ 8 48 ≤ d ≤ 57 69 CODE39 1 ≤ n ≤ 255 48 ≤ d ≤
* 57, 65 ≤ d ≤ 90, 32, 36, 37, 43, 45, 46, 47 70 ITF 1 ≤ n ≤ 255(偶數) 48 ≤ d ≤
* 57 71 CODABAR 1 ≤ n ≤ 255 48 ≤ d ≤ 57, 65 ≤ d ≤ 68, 36, 43, 45, 46, 47, 58 72
* CODE93 1 ≤ n ≤ 255 0 ≤ d ≤ 127 [實例] 打印 GS k 72 7 67 111 100 101 13 57 51 73
* CODE128 2 ≤ n ≤ 255 0 ≤ d ≤ 127 [實例] 打印"No. 123456"的實例數據,首先用CODE B打印"No."
* 然後用CODE C 打印下列數字。GS k 73 10 123 66 78 111 46 123 67 12 34 56
*/
public static byte[] printBarCode(int m, byte[] dk) {
byte[] result = new byte[3 + dk.length];
result[0] = DATA_GS;
result[1] = DATA_GS_k;
result[2] = (byte) m;// 選定條形碼系統
for (int i = 0; i < dk.length; i++) {
result[3 + i] = dk[i];
}
return result;
}
public static byte[] printBarCode(int m, int n, byte[] dn) {
byte[] result = new byte[4 + n];
result[0] = DATA_GS;
result[1] = DATA_GS_k;
result[2] = (byte) m;// 選定條形碼系統
result[3] = (byte) n;// 條碼數據的個數
for (int i = 0; i < n; i++) {
result[4 + i] = dn[i];
}
return result;
}
// ------------------------切紙-----------------------------
/**
* 【GS V m】選擇裁紙模式並部分裁紙(保留一點不裁)
*/
public static byte[] feedPaperCutPartial(int m) {
byte[] result = new byte[3];
result[0] = GS;
result[1] = DATA_GS_V;// 86;//0x56
result[2] = (byte) m;// 1, 49
return result;
}
/**
* 【GS V m n】 選擇裁紙模式並進紙(裁紙位置+[n × 0.125 毫米]) , 並且進行部分裁紙(保留一點不裁)
*
* @return
*/
public static byte[] feedPaperCutPartial(int m, int n) {
byte[] result = new byte[4];
result[0] = GS;
result[1] = DATA_GS_V;// 86;//0x56
result[2] = (byte) 66;// (byte)m;//66
result[3] = (byte) n;
return result;
}
/**
* 進紙並全部切割
*
* @return
*/
public static byte[] feedPaperCutAll() {
byte[] result = new byte[4];
result[0] = GS;
result[1] = DATA_GS_V;// 86;//0x56
result[2] = 65;// 0x41
result[3] = 0;
return result;
}
/**
* 進紙並切割(左邊留一點不切)
*
* @return
*/
public static byte[] feedPaperCutPartial() {
byte[] result = new byte[4];
result[0] = GS;
result[1] = 86;
result[2] = 66;
result[3] = 0;
return result;
}
// ------------------------字節碼拼接--------------------------
public static byte[] byteMerger(byte[] byte_1, byte[] byte_2) {
byte[] byte_3 = new byte[byte_1.length + byte_2.length];
System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);
System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);
return byte_3;
}
public static byte[] byteMerger(byte[][] byteList) {
int length = 0;
for (int i = 0; i < byteList.length; i++) {
length += byteList[i].length;
}
byte[] result = new byte[length];
int index = 0;
for (int i = 0; i < byteList.length; i++) {
byte[] nowByte = byteList[i];
for (int k = 0; k < byteList[i].length; k++) {
result[index] = nowByte[k];
index++;
}
}
for (int i = 0; i < index; i++) {
// CommonUtils.LogWuwei("", "result["+i+"] is "+result[i]);
}
return result;
}
// ------------------------圖片-----------------------------
/** * 對圖片進行壓縮(去除透明度) * * @param bitmapOrg */
public static Bitmap compressPic(Bitmap bitmap) {
// 獲取這個圖片的寬和高
int width = bitmap.getWidth();
int height = bitmap.getHeight();
// 指定調整後的寬度和高度
int newWidth = 240;
int newHeight = 240;
Bitmap targetBmp = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);
Canvas targetCanvas = new Canvas(targetBmp);
targetCanvas.drawColor(0xffffffff);
targetCanvas.drawBitmap(bitmap, new Rect(0, 0, width, height), new Rect(0, 0, newWidth, newHeight), null);
return targetBmp;
}
/**
* * 灰度圖片黑白化,黑色是1,白色是0 * * @param x 橫座標 * @param y 縱座標 * @param bit 位圖 * @return
*/
public static byte px2Byte(int x, int y, Bitmap bit) {
if (x < bit.getWidth() && y < bit.getHeight()) {
byte b;
int pixel = bit.getPixel(x, y);
int red = (pixel & 0x00ff0000) >> 16;
// 取高兩位
int green = (pixel & 0x0000ff00) >> 8;
// 取中兩位
int blue = pixel & 0x000000ff;
// 取低兩位
int gray = RGB2Gray(red, green, blue);
if (gray < 128) {
b = 1;
} else {
b = 0;
}
return b;
}
return 0;
}
/** * 圖片灰度的轉化 */
private static int RGB2Gray(int r, int g, int b) {
int gray = (int) (0.29900 * r + 0.58700 * g + 0.11400 * b);
// 灰度轉化公式
return gray;
}
/*************************************************************************
* * 假設一個240*240的圖片,分辨率設爲24, 共分10行打印 * 每一行,是一個 240*24 的點陣, 每一列有24個點,存儲在3個byte裏面。
* * 每個byte存儲8個像素點信息。因爲只有黑白兩色,所以對應爲1的位是黑色,對應爲0的位是白色
**************************************************************************/
/** * 把一張Bitmap圖片轉化爲打印機可以打印的字節流 * * @param bmp * @return */
public static byte[] draw2PxPoint(Bitmap bmp) {
// 用來存儲轉換後的 bitmap 數據。爲什麼要再加1000,這是爲了應對當圖片高度無法 //整除24時的情況。比如bitmap 分辨率爲 240 *
// 250,佔用 7500 byte, //但是實際上要存儲11行數據,每一行需要 24 * 240 / 8 =720byte
// 的空間。再加上一些指令存儲的開銷, //所以多申請 1000byte 的空間是穩妥的,不然運行時會拋出數組訪問越界的異常。
int size = bmp.getWidth() * bmp.getHeight() / 8 + 1000;
byte[] data = new byte[size];
int k = 0;
// 設置行距爲0的指令
data[k++] = 0x1B;
data[k++] = 0x33;
data[k++] = 0x00;
// 逐行打印
for (int j = 0; j < bmp.getHeight() / 24f; j++) {
// 打印圖片的指令
data[k++] = 0x1B;// ESC
data[k++] = 0x2A;// 【*】
data[k++] = 33;// 24點雙密度
/** 橫向打印點數由nL和nH決定, 總的點數爲 nL + nH × 256 */
data[k++] = (byte) (bmp.getWidth() % 256); // nL
data[k++] = (byte) (bmp.getWidth() / 256); // nH
// 對於每一行,逐列打印
for (int i = 0; i < bmp.getWidth(); i++) {
// 每一列24個像素點,分爲3個字節存儲
for (int m = 0; m < 3; m++) {
// 每個字節表示8個像素點,0表示白色,1表示黑色
for (int n = 0; n < 8; n++) {
byte b = px2Byte(i, j * 24 + m * 8 + n, bmp);
data[k] += data[k] + b;
}
k++;
}
}
data[k++] = LF;// 10 換行
}
return data;
}
}