ESC / POS-如何使用辅助字体打印图像?

时间:2019-06-27 19:33:52

标签: java escpos

我正在使用此示例来打印图像(正确的8位移位):

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

当我将字体设置为9x17而不是12x24时,图像会打印有条纹:

ESC POS printed images

左图使用默认字体(12x24),右图使用其他较小/压缩字体。

我正在使用页面模式在左侧打印图像,在右侧打印文本。似乎我无法为每一侧设置一种字体,所以这就是为什么我需要压缩字体的原因。

我将其设置为:

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

关于我如何摆脱条纹的任何提示?

试图将行高设置为17,但没有成功。

完整代码:

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;
}

0 个答案:

没有答案