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…
Lines
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: