我的打印与ESC / POS命令兼容时出现问题,我成功打印了文本和行。
这是我的模型: https://www.inputservice.com.br/leopardo-a8
以及文档中的打印支持:
Códigosde Barras(条形码翻译):EAN8,EAN13,CODE39,ITF,CODEBAR,CODE128,CODE93,UPC-A,UPC-E,PDF417 * e QR代码。 Emulação(仿真):ESC / POS
我不明白,如果要打印条形码,是否需要在图像中转换条形码?还是不?
我找到了打印大图像的教程,这是链接: http://new-grumpy-mentat.blogspot.com/2014/06/java-escpos-image-printing.html
我按照教程进行操作,但是Qrcode或Barcode中的图像并不总是很好,请参见图片:
现在查看我的代码:
PrintCommands.java
包com.bravos.automap.Util.Print;
public class PrinterCommands {
public static final byte HT = 0x9;
public static final byte LF = 0x0A;
public static final byte CR = 0x0D;
public static final byte ESC = 0x1B;
public static final byte DLE = 0x10;
public static final byte GS = 0x1D;
public static final byte FS = 0x1C;
public static final byte STX = 0x02;
public static final byte US = 0x1F;
public static final byte CAN = 0x18;
public static final byte CLR = 0x0C;
public static final byte EOT = 0x04;
public final static char ESC_CHAR = 0x1B;
public final static byte[] LINE_FEED = new byte[]{0x0A};
public final static byte[] CUT_PAPER = new byte[]{GS, 0x56, 0x00};
public final static byte[] INIT_PRINTER = new byte[]{ESC_CHAR, 0x40};
public final static byte[] SET_LINE_SPACE_24 = new byte[]{ESC_CHAR, 0x33, 24};
public final static byte[] SET_LINE_SPACE_30 = new byte[]{ESC_CHAR, 0x33, 30};
public static final byte[] INIT = {27, 64};
public static byte[] FEED_LINE = {10};
public static byte[] SELECT_FONT_A = {27, 33, 0};
public static byte[] HEADER_FONT = {27, 33, 32};
public static byte[] FONT_3X = {0x1D, 0x21, 0x21};
public static byte[] FONT_2X = {0x1D, 0x21, 0x11};
public static byte[] FONT_1X = {0x1D, 0x21, 0x00};
// Bold
public static byte FONT_BOLD = ((byte)(0x8 | SELECT_FONT_A[2]));
// Height
public static byte FONT_HEIGHT = ((byte)(0x10 | SELECT_FONT_A[2]));
// Width
public static byte FONT_WIDTH = ((byte) (0x20 | SELECT_FONT_A[2]));
// Underline
public static byte FONT_UNDER = ((byte)(0x80 | SELECT_FONT_A[2]));
// Small
public static byte FONT_SMALL = ((byte)(0x1 | SELECT_FONT_A[2]));
public static byte[] SET_BAR_CODE_HEIGHT = {29, 104, 100};
public static byte[] PRINT_BAR_CODE_1 = {29, 107, 2};
public static byte[] SEND_NULL_BYTE = {0x00};
public static byte[] SELECT_PRINT_SHEET = {0x1B, 0x63, 0x30, 0x02};
public static byte[] FEED_PAPER_AND_CUT = {0x1D, 0x56, 66, 0x00};
public static byte[] SELECT_CYRILLIC_CHARACTER_CODE_TABLE = {0x1B, 0x74, 0x11};
public static byte[] SELECT_BIT_IMAGE_MODE = {0x1B, 0x2A, 33, (byte) 255, 3};
public static byte[] SELECT_BIT_IMAGE_MODE_OFI = {ESC_CHAR, 0x2A, 33};;
public static byte[] SELECT_BIT_IMAGE_MODE_V3 = {0x1B, 0x2A, 33, -128, 0};
public static byte[] SELECT_BIT_IMAGE_MODE_V4 = {0x1B, 0x2A, 33, 127, 0};
public static byte[] SELECT_BIT_IMAGE_MODE_V2 = {0x1B, 0x2A, 33};
public static byte[] SELECT_BIT_IMAGE_MODE_V5 = {0x1B, 0x2A, 33, 124, 1};
public static byte[] SET_LINE_SPACING_24 = {0x1B, 0x33, 24};
public static byte[] SET_LINE_SPACING_30 = {0x1B, 0x33, 30};
public static byte[] TRANSMIT_DLE_PRINTER_STATUS = {0x10, 0x04, 0x01};
public static byte[] TRANSMIT_DLE_OFFLINE_PRINTER_STATUS = {0x10, 0x04, 0x02};
public static byte[] TRANSMIT_DLE_ERROR_STATUS = {0x10, 0x04, 0x03};
public static byte[] TRANSMIT_DLE_ROLL_PAPER_SENSOR_STATUS = {0x10, 0x04, 0x04};
public static final byte[] ESC_FONT_COLOR_DEFAULT = new byte[] { 0x1B, 'r',0x00 };
public static final byte[] FS_FONT_ALIGN = new byte[] { 0x1C, 0x21, 1, 0x1B,
0x21, 1 };
public static final byte[] ESC_ALIGN_LEFT = new byte[] { 0x1b, 'a', 0x00 };
public static final byte[] ESC_ALIGN_RIGHT = new byte[] { 0x1b, 'a', 0x02 };
public static final byte[] ESC_ALIGN_CENTER = new byte[] { 0x1b, 'a', 0x01 };
public static final byte[] ESC_CANCEL_BOLD = new byte[] { 0x1B, 0x45, 0 };
/*********************************************/
public static final byte[] ESC_HORIZONTAL_CENTERS = new byte[] { 0x1B, 0x44, 20, 28, 00};
public static final byte[] ESC_CANCLE_HORIZONTAL_CENTERS = new byte[] { 0x1B, 0x44, 00 };
/*********************************************/
public static final byte[] ESC_ENTER = new byte[] { 0x1B, 0x4A, 0x40 };
public static final byte[] PRINTE_TEST = new byte[] { 0x1D, 0x28, 0x41 };
}
SendPrint.java(此示例来自我的EAN_13代码,但还是一样,只需更改为QRCODE
printerConstants = new PrinterConstants(mmOutputStream);
mmOutputStream.write(PrinterCommands.INIT);
mmOutputStream.write(PrinterCommands.ESC_ALIGN_CENTER);
printerConstants.printString("TESTANDO A FUNÇÃO");
mmOutputStream.write(PrinterCommands.FEED_LINE);
mmOutputStream.write(PrinterCommands.FEED_LINE);
printerConstants.setBold(true);
printerConstants.printString("TESTANDO A FUNÇÃO");
mmOutputStream.write(PrinterCommands.FEED_LINE);
printerConstants.resetToDefault();
String code = "7800000017106";
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
try {
BitMatrix bitMatrix = multiFormatWriter.encode(code, BarcodeFormat.EAN_13,200,80);
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
int[][] pixels = getPixelsSlow(bitmap);
printImage(pixels);
// printPhoto(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}