0

我正在使用 ESC/POS 命令实现 POS 蓝牙打印机并尝试打印位图图像。图像正在打印,但总是在开始时打印一些空白区域,然后打印图像,因为我无法为每个标签打印 1 个图像。以下是我的打印和图像代码

    int bmpNewWidth = bmp.getWidth();
    int bmpNewHeight = bmp.getHeight();

    byte[] printBMPPackageHead = ESCUtil.bmpCmdHead(bmpMode,bmpNewWidth);

    int bmpBlockHeight = 0;
    int bmpBlockNums =0;
    if((bmpMode == 0) || (bmpMode ==1))
    {
        bmpBlockHeight = 8;
    }
    else if((bmpMode == 32) || (bmpMode ==33))
    {
        bmpBlockHeight = 24;
    }
    else
    {
        Log.d(TAG,"****bmpMode set error!!*****");
        return (new byte[1]);
    }
    bmpBlockNums = ((bmpNewHeight % bmpBlockHeight) == 0)? (bmpNewHeight/bmpBlockHeight) : (bmpNewHeight/bmpBlockHeight +1);
    int bmpBlockCMDSize = printBMPPackageHead.length + bmpNewWidth*bmpBlockHeight/8;
    byte[] bmpPrintData = new byte[bmpBlockNums*bmpBlockCMDSize];
    for(int n = 0; n < bmpBlockNums; n++)
    {
        byte[] bmpBlockPxBytes = getBitmapBlockData(n,bmpNewWidth,bmpBlockHeight,bmp);

        byte[][] bmpBlockPrintData = {printBMPPackageHead,bmpBlockPxBytes};
        System.arraycopy(ESCUtil.byteMerger(bmpBlockPrintData),0,bmpPrintData,n*bmpBlockCMDSize,bmpBlockCMDSize);
    }

  return bmpPrintData;
}

    public static byte[] bmpCmdHead(int mode, int bitmapWidth)
    {
    //byte[] result = new byte[]{ESC,42,0,0,0};
    byte[] result = new byte[]{ESC,42,0,0,0};
    result[2] = (byte)mode;
    result[3] = (byte)(bitmapWidth%256);
    result[4] = (byte)(bitmapWidth/256);
    return result;
}

   public static byte[] getBitmapBlockData(int blocknum, int bmpWidth, int bmpBlockHeight, Bitmap bmp)
{
    int blockHeightBytes = bmpBlockHeight/8;
    byte[] blockData = new byte[bmpWidth*blockHeightBytes];
    for (int i = 0;i < bmpWidth; i++)
    {
        for(int j = 0;j < blockHeightBytes;j++)
        {
            for(int p = 0; p < 8; p++)
            {
                byte px = px2Byte(i,blocknum * bmpBlockHeight+j*8+p,bmp);
                blockData[i*blockHeightBytes+j] |= (px << (7-p));
            }
        }
    }
    return  blockData;
}

就是这样。在调用这个函数之前,我只调用了初始化打印机命令。请帮我。提前致谢。

4

0 回答 0