Search code examples
javaprintingqr-codeescpos

ESC/POS commands for QR prints only the content


I'm trying to print a QR code on a Custom VKP printer. The printer supports QR codes. I send ESC/POS commands to it, but all that is printed is the text and not the QR code. The following is my code in Java:

    String content = "Hello !!";
    int store_len = content.length() + 3;
    byte store_pL = (byte) (store_len % 256);
    byte store_pH = (byte) (store_len / 256);


    byte ESC = 0x1b;
    byte[] INIT = new byte[]{ESC, '@'};
    byte[] CUT = new byte[]{0x0c};
    byte[] FUNC_165 = new byte[]{Commands.GS, 0x28, 0x6b, 0x04, 0x00, 0x31, 0x41, 0x51, 0x00};
    byte[] FUNC_167 = new byte[]{Commands.GS, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x43, 0x64};
    byte[] FUNC_169 = new byte[]{Commands.GS, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x45, 0x48};
    byte[] FUNC_180 = new byte[]{Commands.GS, 0x28, 0x6b, store_pL, store_pH, 0x31, 0x50, 0x30};
    byte[] FUNC_181 = new byte[]{Commands.GS, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x51, 0x48};
    byte[] FUNC_182 = new byte[]{Commands.GS, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x52, 0x48};

    ByteArrayOutputStream writer = new ByteArrayOutputStream();

    writer.write(INIT);
    writer.write(FUNC_165);
    writer.write(FUNC_167);
    writer.write(FUNC_169);
    writer.write(FUNC_180);
    writer.write(content.getBytes());
    writer.write(FUNC_181);
    writer.write(FUNC_182);
    writer.write(CUT);
    writer.close();

The output is QHello !!.

What am I doing wrong here. Any help is appreciated.


Solution

  • If you are referring to these pages in EPSON, the numbers for the parameters written in them are decimal, not hexadecimal.

    GS ( k <Function 165>
    GS ( k <Function 167>
    GS ( k <Function 169>
    GS ( k <Function 180>
    GS ( k <Function 181>
    GS ( k <Function 182>

    Or is it the right parameter for a Custom VKP printer?
    I can't judge it because I don't have the ESC/POS command reference for the Custom VKP printer.

    • From the numerical value, it seems that I am trying to print Micro QR Code, is the printer supported?
    • Some unknown value is specified for the module size, but EPSON is in the range of 1 to 16. The default is 3.
    • And since FUNC_182 is a function to notify the print size of the barcode, calling it after printing has no meaning.

    Assuming the printer supports MicroQRCode printing, the command creation part would look like this:

    byte[] FUNC_165 = new byte[]{Commands.GS, 0x28, 0x6b, 0x04, 0x00, 0x31, 0x41, 0x33, 0x00};
    byte[] FUNC_167 = new byte[]{Commands.GS, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x43, 0x03};
    byte[] FUNC_169 = new byte[]{Commands.GS, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x45, 0x30};
    byte[] FUNC_180 = new byte[]{Commands.GS, 0x28, 0x6b, store_pL, store_pH, 0x31, 0x50, 0x30};
    byte[] FUNC_181 = new byte[]{Commands.GS, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x51, 0x30};
    byte[] FUNC_182 = new byte[]{Commands.GS, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x52, 0x30};
    
    • Although there is no problem with the current contents, actually specify the data length as the length of the byte array, not the length of the string.

    So the first part would be:

    String content = "Hello !!";
    byte[] content_bytes = content.getBytes(StandardCharsets.US_ASCII)
    int store_len = content_bytes.length + 3;
    byte store_pL = (byte) (store_len % 256);
    byte store_pH = (byte) (store_len / 256);
    

    The actual writing will be like this?:

    writer.write(INIT);
    writer.write(FUNC_165);
    writer.write(FUNC_167);
    writer.write(FUNC_169);
    writer.write(FUNC_180);
    writer.write(content_bytes);
    writer.write(FUNC_181);
    writer.write(CUT);
    writer.close();
    

    Please try adjusting it to the parameter range that your Custom VKP printer actually supports.