简体   繁体   English

我的代码有什么问题? 我无法打印条形码或二维码

[英]What is wrong with my code? I can't print a Barcode or a Qrcode

I have a problem with a print that is compatible with ESC/POS commands, I print successfully texts and lines.我遇到了与 ESC/POS 命令兼容的打印问题,我成功打印了文本和行。

this is my model from : https://www.inputservice.com.br/leopardo-a8这是我的模型: https ://www.inputservice.com.br/leopardo-a8

and in the documentation, the print support:在文档中,打印支持:

Códigos de Barras (Barcode translating): EAN8, EAN13, CODE39, ITF, CODEBAR, CODE128, CODE93, UPC-A, UPC-E, PDF417* e QR CODE. Códigos de Barras(条码翻译):EAN8、EAN13、CODE39、ITF、CODEBAR、CODE128、CODE93、UPC-A、UPC-E、PDF417* e QR CODE。 Emulação (EMULATION): ESC/POS Emulação(仿真):ESC/POS

I don't understand that if I want to print a barcode I need to transform the bar in an image?我不明白,如果我想打印条形码,我需要在图像中转换条形吗? or not?或不?

I found a tutorial to print large images, this is the link : http://new-grumpy-mentat.blogspot.com/2014/06/java-escpos-image-printing.html我找到了打印大图像的教程,这是链接:http: //new-grumpy-mentat.blogspot.com/2014/06/java-escpos-image-printing.html

I follow the tutorial but the image from my Qrcode or Barcode is not always good, see the picture :我按照教程进行操作,但我的二维码或条形码中的图像并不总是很好,请参见图片:

在此处输入图像描述

Now see my code :现在看看我的代码:

PrintCommands.java打印命令.java

package com.bravos.automap.Util.Print;包 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 ( this example is from my EAN_13 code, but is the same thing, just change to QRCODE 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();
                }

In Following Fucntion :在以下功能中:

  1. data : can be link or anything you want to put in QR-Code数据:可以是链接或任何您想放入 QR 码的内容<\/li>
  2. size : howbig you want QR-Code to be size : 你希望 QR 码有多大<\/li><\/ol><\/blockquote>
    public static byte[] QRCodeDataToBytes(String data, int size) throws EscPosBarcodeException { ByteMatrix byteMatrix = null; try { EnumMap<EncodeHintType, Object> hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); QRCode code = Encoder.encode(data, ErrorCorrectionLevel.L, hints); byteMatrix = code.getMatrix(); } catch (WriterException e) { e.printStackTrace(); throw new EscPosBarcodeException("Unable to encode QR code"); } if (byteMatrix == null) { return EscPosPrinterCommands.initImageCommand(0, 0); } int width = byteMatrix.getWidth(), height = byteMatrix.getHeight(), coefficient = Math.round((float) size \/ (float) width), imageWidth = width * coefficient, imageHeight = height * coefficient, bytesByLine = (int) Math.ceil(((float) imageWidth) \/ 8f), i = 8; if (coefficient < 1) { return EscPosPrinterCommands.initImageCommand(0, 0); } byte[] imageBytes = EscPosPrinterCommands.initImageCommand(bytesByLine, imageHeight); for (int y = 0; y < height; y++) { byte[] lineBytes = new byte[bytesByLine]; int x = -1, multipleX = coefficient; boolean isBlack = false; for (int j = 0; j < bytesByLine; j++) { StringBuilder stringBinary = new StringBuilder(); for (int k = 0; k < 8; k++) { if (multipleX == coefficient) { isBlack = ++x < width && byteMatrix.get(x, y) == 1; multipleX = 0; } stringBinary.append(isBlack ? "1" : "0"); ++multipleX; } lineBytes[j] = (byte) Integer.parseInt(stringBinary.toString(), 2); } for (int multipleY = 0; multipleY < coefficient; ++multipleY) { System.arraycopy(lineBytes, 0, imageBytes, i, lineBytes.length); i += lineBytes.length; } } return imageBytes; }<\/code><\/pre> 
             

    在此处输入图像描述<\/a>

    I Prefer this library<\/a> for printing over thermal printer<\/strong>我更喜欢这个库<\/a>来打印热敏打印机<\/strong>

    <\/blockquote>"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的代码有什么问题,我不能使用KeyPressed方法? - What wrong with my code , I can't use KeyPressed method? 我似乎无法弄清楚如何处理此实例,以便我的代码可以打印出要求 - I can't seem to figure out what to do with this instance so that my code could print out the requirement 我的代码有什么问题,因此无法播放wav文件? - What is wrong with my code so it can't play wav file? 无法弄清楚我的合并排序代码有什么问题 - Can't figure out what is wrong with my merge sort code 代码有什么问题,因为我无法打印 3 和 2 - what is wrong with the code as i am unable to print 3 and 2 我正在尝试打印质数,但不确定我的代码有什么问题 - I'm trying to print out prime number but not sure what's wrong with my code 我的代码做错了什么? 翻译成 PigLatin 并无法弄清楚如何打印返回值 - What am I doing wrong with my code? Translating into PigLatin and cant figure out how to print returned value 有人可以解释我的代码有什么问题吗? - Can someone explain what is wrong with my code? 无法打印年薪。 我究竟做错了什么? - Can't get Annual Salary to Print. What am I doing wrong? 我无法访问代码中的 else 部分,谁能解释我的代码有什么问题? - I am unable to access the else part in the code, can anyone explain what is wrong with my code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM