从字节数组中确定 x 和 y 的高字节和低字节

发布于 2025-01-08 01:55:53 字数 1463 浏览 1 评论 0原文

我想使用他们的 SDK 将 Android 位图发送到打印机(移动蓝牙打印机 Bixolon SPP R-200),我可以将位图转换为字节数组。出于参考目的(并了解我正在了解的内容),请查看 Bixolon 命令手册 此处。这就是我的操作方式:

向打印机发送一条命令,告诉它我希望如何使用以下命令对齐打印输出(左 = 0,右 = 2,中心 = 1):

    byte[] command = { 27, 97, 0 };

27 和 97 是Bixolon 命令手册中的命令 - 零表示“左对齐”。然后,我使用 SDK write(byte[]) 命令将此命令发送到打印机(成功)。

下一步是我遇到麻烦的地方:我想将“打印光栅位图像”命令发送到打印机,第一部分很简单:

    byte[] command2 = { 29, 118, 48, 0, 0, 0, 0, 0 };

29、118 和 48 一起组成了名为“GS v 0”的命令(对上述数字使用 ascii 转换表时的直译),这在参考 Bixolon 手册的第 126 页中有描述。第一个零将水平和垂直 DPI 设置为 203。

现在我坚持的部分是:根据手册,其余 4 个零必须替换为:

xL  xH  yL  yH  d1...d 

我假设是 x 位置低字节,x 位置高字节, y 位置低字节等...(但不确定 d1...d 代表什么?)。

在 SDK 中,他们用以下代码填充最后 4 个字节:

    int width = myBitmap.getWidth();
    int height = myBitmap.getHeight();
    int bytesOfWidth = width / 8 + (width % 8 != 0 ? 1 : 0);

    dimensionCommand[4] = (byte) (bytesOfWidth % 256);
    dimensionCommand[5] = (byte) (bytesOfWidth / 256);
    dimensionCommand[6] = (byte) (height % 256);
    dimensionCommand[7] = (byte) (height / 256);

但我不明白为什么。这是你计算x/y位置的高字节和低字节的方法吗?

然而 - 发送这个命令到打印机也有效,并且在最后一步中,我已转换为字节数组的位图也成功传输(至少没有发生 IO 错误),但没有打印任何内容(尽管纸张移动并且纸张左侧似乎打印了两个像素)。所以我怀疑上面对Bitmap的x和y坐标的高低字节的计算可能是错误的......

I want to send an Android Bitmap to a printer (Mobile Bluetooth Printer Bixolon SPP R-200) using their SDK I can convert the Bitmap to a byte Array. For reference purposes (and to understand what I am taking about) check out the Bixolon Command manual here. And this is how I proceed:

Send a command to the printer, to tell it, how I want my printout to be aligned (left = 0, right = 2, center = 1) using the following command:

    byte[] command = { 27, 97, 0 };

The 27 and 97 are a command from the Bixolon Command manual - the zero means "left aligned". I then send this command to the printer using the SDKs write(byte[]) command (which succeeds).

The next step is where I am having trouble: I want to send the "Print raster bit image" command to the printer and the first part is easy:

    byte[] command2 = { 29, 118, 48, 0, 0, 0, 0, 0 };

The 29, 118 and 48 together make up the command called "GS v 0" (literal translation when using a ascii conversion chart on the above numbers) which is described on page 126 in the referenced Bixolon manual. The first zero sets the horizontal and vertical DPI to 203.

Now the part I am stuck with: According to the manual, the remaining 4 zeros have to be replaced by:

xL  xH  yL  yH  d1...d 

which I assume to be x Position Low Byte, x Position High Byte, y position Low byte etc... (not sure what d1...d stands for though?).

In the SDK they're filling those last 4 Bytes with the following code:

    int width = myBitmap.getWidth();
    int height = myBitmap.getHeight();
    int bytesOfWidth = width / 8 + (width % 8 != 0 ? 1 : 0);

    dimensionCommand[4] = (byte) (bytesOfWidth % 256);
    dimensionCommand[5] = (byte) (bytesOfWidth / 256);
    dimensionCommand[6] = (byte) (height % 256);
    dimensionCommand[7] = (byte) (height / 256);

But I don't understand why. Is this how you calculate the high and low bytes of the x/y positions?

However - sending this command to the printer works too, and in the last step my Bitmap that has been converted to a byte array get's successfully transferred as well (at least no IO Error occurs), but nothing is being printed (although the paper moves and there seem to be two pixels being printed on the left side of the paper). So I suspected maybe the above calculation of the high and low bytes for the x and y coordinates of the Bitmap are wrong...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昔日梦未散 2025-01-15 01:55:53

线条

int bytesOfWidth = width / 8 + (width % 8 != 0 ? 1 : 0);
dimensionCommand[4] = (byte) (bytesOfWidth % 256);
dimensionCommand[5] = (byte) (bytesOfWidth / 256);

看起来很可疑。我会使用与高度相同的计算宽度。我还会使用按位 AND 和位移位来代替 %/ 运算符,相比之下,它们相当慢:

int width = myBitmap.getWidth();
int height = myBitmap.getHeight();

dimensionCommand[4] = (byte) (width & 0xFF);
dimensionCommand[5] = (byte) ((width >> 8) & 0xFF);
dimensionCommand[6] = (byte) (height & 0xFF);
dimensionCommand[7] = (byte) ((height >> 8) & 0xff);

Lines

int bytesOfWidth = width / 8 + (width % 8 != 0 ? 1 : 0);
dimensionCommand[4] = (byte) (bytesOfWidth % 256);
dimensionCommand[5] = (byte) (bytesOfWidth / 256);

look suspicious. I would use the same computation for width as there's for height. I would also use bitwise AND and bit-shift instead of % and / operators, which are rather slow in comparison:

int width = myBitmap.getWidth();
int height = myBitmap.getHeight();

dimensionCommand[4] = (byte) (width & 0xFF);
dimensionCommand[5] = (byte) ((width >> 8) & 0xFF);
dimensionCommand[6] = (byte) (height & 0xFF);
dimensionCommand[7] = (byte) ((height >> 8) & 0xff);
み青杉依旧 2025-01-15 01:55:53

失去 bytesOfWidth 计算,它没有意义。只需将宽度传递过去即可。

此标头后面的字节 d1、d2、...d3 是栅格 *d*ata。根据在线手册此光栅数据是每个像素 1 字节,而不是我认为您之前的问题假设的 1 位。

Lose that bytesOfWidth calculation, it doesn't make sense. Just pass the width through.

The bytes d1,d2,...d3, that follow this header are the raster *d*ata. According to the online manual this raster data is 1 byte per pixel, not 1 bit as I think your previous question assumed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文