|
|
@@ -0,0 +1,494 @@
|
|
|
+package com.uas.uas_mes_standard.bluebooths;
|
|
|
+
|
|
|
+import java.io.BufferedWriter;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.io.OutputStreamWriter;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 汉印的指令集为CPCL 不支持ESC 所以这个工具类没有进行有效的测试
|
|
|
+ * ESC/POS协议打印机工具类(个别打印机型不支持)
|
|
|
+ */
|
|
|
+public class EscPosHelper {
|
|
|
+ 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;// 作废(页模式下取消打印数据 )
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * CodePage table
|
|
|
+ */
|
|
|
+ public static class CodePage {
|
|
|
+ public static final byte PC437 = 0;
|
|
|
+ public static final byte KATAKANA = 1;
|
|
|
+ public static final byte PC850 = 2;
|
|
|
+ public static final byte PC860 = 3;
|
|
|
+ public static final byte PC863 = 4;
|
|
|
+ public static final byte PC865 = 5;
|
|
|
+ public static final byte WPC1252 = 16;
|
|
|
+ public static final byte PC866 = 17;
|
|
|
+ public static final byte PC852 = 18;
|
|
|
+ public static final byte PC858 = 19;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 条形码编号
|
|
|
+ */
|
|
|
+ public static class BarCode {
|
|
|
+ public static final byte UPC_A = 0;
|
|
|
+ public static final byte UPC_E = 1;
|
|
|
+ public static final byte EAN13 = 2;
|
|
|
+ public static final byte EAN8 = 3;
|
|
|
+ public static final byte CODE39 = 4;
|
|
|
+ public static final byte ITF = 5;
|
|
|
+ public static final byte NW7 = 6;
|
|
|
+ public static final byte CODE128 = 73;
|
|
|
+ }
|
|
|
+
|
|
|
+// ------------------------打印机初始化-----------------------------
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 打印机初始化
|
|
|
+ *
|
|
|
+ * @return bytes for this command
|
|
|
+ */
|
|
|
+ public static byte[] init_printer() {
|
|
|
+ byte[] result = new byte[2];
|
|
|
+ result[0] = ESC;
|
|
|
+ result[1] = 64;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // ------------------------换行-----------------------------
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 换行
|
|
|
+ *
|
|
|
+ * @param -lineNum要换几行
|
|
|
+ */
|
|
|
+ public static byte[] nextLine(int lineNum) {
|
|
|
+ byte[] result = new byte[lineNum];
|
|
|
+ for (int i = 0; i < lineNum; i++) {
|
|
|
+ result[i] = LF;
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ // ------------------------下划线-----------------------------
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置绘制下划线
|
|
|
+ *
|
|
|
+ * @param width 下划线的宽度 字节
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static byte[] setUnderline(byte width) {
|
|
|
+ byte[] result = new byte[3];
|
|
|
+ result[0] = ESC;
|
|
|
+ result[1] = 45;
|
|
|
+ result[2] = width;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 取消绘制下划线
|
|
|
+ */
|
|
|
+ public static byte[] underlineOff() {
|
|
|
+ byte[] result = new byte[3];
|
|
|
+ result[0] = ESC;
|
|
|
+ result[1] = 45;
|
|
|
+ result[2] = 0;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // ------------------------加粗-----------------------------
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 选择加粗模式
|
|
|
+ */
|
|
|
+ public static byte[] boldOn() {
|
|
|
+ byte[] result = new byte[3];
|
|
|
+ result[0] = ESC;
|
|
|
+ result[1] = 69;
|
|
|
+ result[2] = 0xF;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 取消加粗模式
|
|
|
+ */
|
|
|
+ public static byte[] boldOff() {
|
|
|
+ byte[] result = new byte[3];
|
|
|
+ result[0] = ESC;
|
|
|
+ result[1] = 69;
|
|
|
+ result[2] = 0;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+// ------------------------对齐-----------------------------
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 左对齐
|
|
|
+ */
|
|
|
+ public static byte[] alignLeft() {
|
|
|
+ byte[] result = new byte[3];
|
|
|
+ result[0] = ESC;
|
|
|
+ result[1] = 97;
|
|
|
+ result[2] = 0;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 居中对齐
|
|
|
+ */
|
|
|
+ public static byte[] alignCenter() {
|
|
|
+ byte[] result = new byte[3];
|
|
|
+ result[0] = ESC;
|
|
|
+ result[1] = 97;
|
|
|
+ result[2] = 1;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 右对齐
|
|
|
+ */
|
|
|
+ public static byte[] alignRight() {
|
|
|
+ byte[] result = new byte[3];
|
|
|
+ result[0] = ESC;
|
|
|
+ result[1] = 97;
|
|
|
+ result[2] = 2;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 水平方向向右移动col列
|
|
|
+ */
|
|
|
+ public static byte[] offset(byte col) {
|
|
|
+ byte[] result = new byte[4];
|
|
|
+ result[0] = ESC;
|
|
|
+ result[1] = 68;
|
|
|
+ result[2] = col;
|
|
|
+ result[3] = 0;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+// ------------------------字体变大-----------------------------
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字体变大为标准的n倍
|
|
|
+ */
|
|
|
+ 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] = GS;
|
|
|
+ result[1] = 33;
|
|
|
+ result[2] = realSize;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字体取消倍宽倍高
|
|
|
+ */
|
|
|
+ public static byte[] fontSizeSetSmall(int num) {
|
|
|
+ byte[] result = new byte[3];
|
|
|
+ result[0] = ESC;
|
|
|
+ result[1] = 33;
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // ------------------------切纸-----------------------------
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 进纸并全部切割
|
|
|
+ */
|
|
|
+ public static byte[] feedPaperCutAll() {
|
|
|
+ byte[] result = new byte[4];
|
|
|
+ result[0] = GS;
|
|
|
+ result[1] = 86;
|
|
|
+ result[2] = 65;
|
|
|
+ result[3] = 0;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 进纸并切割(左边留一点不切)
|
|
|
+ */
|
|
|
+ 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[] printBarGs(byte barCodeType, String text) {
|
|
|
+ return printBar(BarCode.CODE128, barCodeType, text);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static byte[] printBarESC(byte barCodeType, String text) {
|
|
|
+ return printBar(ESC, barCodeType, text);
|
|
|
+ }
|
|
|
+
|
|
|
+ //设置条形码的宽度单位
|
|
|
+ public static byte[] setBarCodeWidth(int n) {
|
|
|
+ byte[] result = new byte[3];
|
|
|
+ result[0] = GS;
|
|
|
+ result[1] = 119;
|
|
|
+ result[2] = (byte) n;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置条码高度
|
|
|
+ *
|
|
|
+ * @param dots ( default = 162 )
|
|
|
+ * @return bytes for this command
|
|
|
+ */
|
|
|
+ public static byte[] barCodeHeight(byte dots) {
|
|
|
+ byte[] result = new byte[3];
|
|
|
+ result[0] = GS;
|
|
|
+ result[1] = 104;
|
|
|
+ result[2] = dots;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 打印条形码
|
|
|
+ *
|
|
|
+ * @param type 指令类型
|
|
|
+ * @param barCodeType 条形码类型({@link BarCode})
|
|
|
+ * @param text 打印内容
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static byte[] printBar(byte type, byte barCodeType, String text) {
|
|
|
+ byte[] barCodeBytes = text.getBytes();
|
|
|
+ byte[] result = new byte[4 + barCodeBytes.length];
|
|
|
+ result[0] = type;
|
|
|
+ result[1] = 107;
|
|
|
+ result[2] = barCodeType;
|
|
|
+ int idx = 3;
|
|
|
+ if (type == ESC) {
|
|
|
+ result[3] = (byte) barCodeBytes.length;
|
|
|
+ idx = 4;
|
|
|
+ }
|
|
|
+ for (int i = 0; i < barCodeBytes.length; i++) {
|
|
|
+ result[idx] = barCodeBytes[i];
|
|
|
+ idx++;
|
|
|
+ }
|
|
|
+ if (type == GS) {
|
|
|
+ result[idx] = 0;
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ //开启双划线模式
|
|
|
+ public static byte[] doubleStrikeOn() {
|
|
|
+ byte[] result = new byte[3];
|
|
|
+ result[0] = ESC;
|
|
|
+ result[1] = 71;
|
|
|
+ result[2] = 1;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ //关闭双划线模式
|
|
|
+ public static byte[] doubleStrikeOff() {
|
|
|
+ byte[] result = new byte[3];
|
|
|
+ result[0] = ESC;
|
|
|
+ result[1] = 71;
|
|
|
+ result[2] = 0;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 选择字体(0-3)
|
|
|
+ *
|
|
|
+ * @return bytes for this command
|
|
|
+ */
|
|
|
+ public static byte[] selectFont(byte fout) {
|
|
|
+ byte[] result = new byte[3];
|
|
|
+ result[0] = ESC;
|
|
|
+ result[1] = 77;
|
|
|
+ result[2] = fout;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 选择字符代码表
|
|
|
+ *
|
|
|
+ * @param cp example:CodePage.WPC1252
|
|
|
+ * @return bytes for this command
|
|
|
+ */
|
|
|
+ public static byte[] selectCodeTab(byte cp) {
|
|
|
+ byte[] result = new byte[3];
|
|
|
+ result[0] = ESC;
|
|
|
+ result[1] = 116;
|
|
|
+ result[2] = cp;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * justification_left
|
|
|
+ * ESC a n
|
|
|
+ *
|
|
|
+ * @return bytes for this command
|
|
|
+ */
|
|
|
+ public static byte[] justification_left() {
|
|
|
+ byte[] result = new byte[3];
|
|
|
+ result[0] = ESC;
|
|
|
+ result[1] = 97;
|
|
|
+ result[2] = 0;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * justification_center
|
|
|
+ * ESC a n
|
|
|
+ *
|
|
|
+ * @return bytes for this command
|
|
|
+ */
|
|
|
+ public static byte[] justification_center() {
|
|
|
+ byte[] result = new byte[3];
|
|
|
+ result[0] = ESC;
|
|
|
+ result[1] = 97;
|
|
|
+ result[2] = 1;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * justification_right
|
|
|
+ * ESC a n
|
|
|
+ *
|
|
|
+ * @return bytes for this command
|
|
|
+ */
|
|
|
+ public static byte[] justification_right() {
|
|
|
+ byte[] result = new byte[3];
|
|
|
+ result[0] = ESC;
|
|
|
+ result[1] = 97;
|
|
|
+ result[2] = 2;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从缓冲区打印数据并保留几行在页尾
|
|
|
+ *
|
|
|
+ * @param n lines
|
|
|
+ * @return bytes for this command
|
|
|
+ */
|
|
|
+ public static byte[] printAndFeedLines(byte n) {
|
|
|
+ byte[] result = new byte[3];
|
|
|
+ result[0] = ESC;
|
|
|
+ result[1] = 100;
|
|
|
+ result[2] = n;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte[] setChineseOn() {
|
|
|
+ byte[] result = new byte[2];
|
|
|
+ result[0] = FS;
|
|
|
+ result[1] = 38;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte[] setChineseOff() {
|
|
|
+ byte[] result = new byte[2];
|
|
|
+ result[0] = FS;
|
|
|
+ result[1] = 46;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte[] setChineseSuperOn() {
|
|
|
+ byte[] result = new byte[3];
|
|
|
+ result[0] = FS;
|
|
|
+ result[1] = 33;
|
|
|
+ result[2] = 12;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte[] setChineseSuperOff() {
|
|
|
+ byte[] result = new byte[3];
|
|
|
+ result[0] = FS;
|
|
|
+ result[1] = 33;
|
|
|
+ result[2] = 0;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void test(OutputStream mOutputStream) throws IOException {
|
|
|
+ OutputStreamWriter outSW = new OutputStreamWriter(mOutputStream, "GBK");
|
|
|
+ BufferedWriter socketWriter = new BufferedWriter(outSW);
|
|
|
+ socketWriter.write(new String(justification_center(), "UTF-8"));
|
|
|
+ socketWriter.write("***测试***\n");
|
|
|
+ socketWriter.write("快来买吧\n");
|
|
|
+ socketWriter.write(new String(justification_left(), "UTF-8"));
|
|
|
+ socketWriter.write("订单号:170426543103\n");
|
|
|
+ socketWriter.write("哈哈哈\n");
|
|
|
+ socketWriter.write("-------------------------------------------\n");
|
|
|
+ socketWriter.write(new String(boldOn(), "UTF-8"));
|
|
|
+ socketWriter.write(new String(doubleStrikeOn(), "UTF-8"));
|
|
|
+ socketWriter.write("367#085_1300\n");
|
|
|
+ socketWriter.write(new String(doubleStrikeOff(), "UTF-8"));
|
|
|
+ socketWriter.write(new String(boldOff(), "UTF-8"));
|
|
|
+ socketWriter.write("测试人:王大帅哥\n");
|
|
|
+ socketWriter.write("手机:13052235269\n");
|
|
|
+ socketWriter.write(new String(boldOn(), "UTF-8"));
|
|
|
+ socketWriter.write(new String(setChineseSuperOn(), "UTF-8"));
|
|
|
+ socketWriter.write(new String(doubleStrikeOn(), "UTF-8"));
|
|
|
+ socketWriter.write("你说我是不是帅哥\n");
|
|
|
+ socketWriter.write(new String(doubleStrikeOff(), "UTF-8"));
|
|
|
+ socketWriter.write(new String(setChineseSuperOff(), "UTF-8"));
|
|
|
+ socketWriter.write(new String(boldOff(), "UTF-8"));
|
|
|
+
|
|
|
+ socketWriter.write("\n\n");
|
|
|
+ socketWriter.write(new String(setBarCodeWidth(2), "UTF-8"));
|
|
|
+ socketWriter.write(new String(printBarESC(BarCode.CODE128, "Bcb7099132890012345"), "UTF-8"));
|
|
|
+ socketWriter.write("\n");
|
|
|
+ socketWriter.write("cb7099132890012345");
|
|
|
+
|
|
|
+ //加一个空行
|
|
|
+ socketWriter.write("\n");
|
|
|
+ socketWriter.write(new String(feedPaperCutPartial(), "UTF-8"));
|
|
|
+ socketWriter.flush();
|
|
|
+ socketWriter.close();
|
|
|
+ }
|
|
|
+}
|