'ESC / POS - How to print images with secondary font?

I am using this sample for printing images (with the correct 8 bits shift):

Print bitmap full page width in thermal dot printer using ESC/POS in java

When I set the font to 9x17 instead of 12x24 the image gets printed with stripes:

ESC POS printed images

The left image is with default font (12x24) and the right image is with alternative smaller/condensed font.

I am using the page mode to print image on the left and text on the right. Seems that I cannot set one font for each side, so that's why I need the condensed font.

I set it with:

private static final byte[] CONDENSED_ON = {ESC, 0x4D, 0x01};

Any hints on how I could get rid of the stripes?

Tried to set line height to 17, no success.

Full code:

public EscPosPrinter imprimeImagem(BufferedImage image) throws IOException {
    BitSet imageBits = getBitsImageData(image);

    int lsb = (image.getWidth() & 0xFF);
    int msb = ((image.getWidth() >> 8) & 0xFF);

    setLineSize(24);

    byte[] modoImagem = {ESC, 0x2A, (byte) 33, (byte) lsb, (byte) msb}; //33 = 24 pontos densidade dupla

    int offset = 0;
    while(offset < image.getHeight()) {
        envia(modoImagem);

        int imageDataLineIndex = 0;
        byte[] imageDataLine = new byte[3 * image.getWidth()];

        for(int x = 0; x < image.getWidth(); ++x) {
            // Remember, 24 dots = 24 bits = 3 bytes.
            // The 'k' variable keeps track of which of those
            // three bytes that we're currently scribbling into.
            for(int k = 0; k < 3; ++k) {
                byte slice = 0;

                // A byte is 8 bits. The 'b' variable keeps track
                // of which bit in the byte we're recording.
                for(int b = 0; b < 8; ++b) {
                    // Calculate the y position that we're currently
                    // trying to draw. We take our offset, divide it
                    // by 8 so we're talking about the y offset in
                    // terms of bytes, add our current 'k' byte
                    // offset to that, multiple by 8 to get it in terms
                    // of bits again, and add our bit offset to it.
                    int y = (((offset / 8) + k) * 8) + b;

                    // Calculate the location of the pixel we want in the bit array.
                    // It'll be at (y * width) + x.
                    int i = (y * image.getWidth()) + x;

                    // If the image (or this stripe of the image)
                    // is shorter than 24 dots, pad with zero.
                    boolean v = false;
                    if(i < imageBits.length()) {
                        v = imageBits.get(i);
                    }
                    // Finally, store our bit in the byte that we're currently
                    // scribbling to. Our current 'b' is actually the exact
                    // opposite of where we want it to be in the byte, so
                    // subtract it from 7, shift our bit into place in a temp
                    // byte, and OR it with the target byte to get it into there.
                    slice |= (byte) ((v ? 1 : 0) << (7 - b));
                }
                imageDataLine[imageDataLineIndex + k] = slice;
            }
            imageDataLineIndex += 3;
        }

        offset += 24;

        send(imageDataLine);
        send(LF);
    }
    resetLineSize();

    return this;
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source