How can I print an image on a Bluetooth printer in Android?
I also tried this and I got to my own solution and I think I figured out how the SELECT_BIT_IMAGE_MODE
command works.
The command public static byte[] SELECT_BIT_IMAGE_MODE = {0x1B, 0x2A, 33, 255, 3};
in the class PrinterCommands
is the POS Command for image printing.
The first two are pretty standard, the next three determine the mode and the dimensions of the image to be printed. For the sake of this solution, let's just assume that the second element (33, we are indexed zero) is always 33.
The last two elements of that byte[] refers to the Width (in pixels) property of the image that you want to print, element 3 is sometimes referred to as nL
and element 4 is sometimes referred to as nH
. They are actually both referring to the Width, nL
is the Low Byte
while nH
is the High Byte
. This means that we can have at the most an image with a width of 1111 1111 1111 1111b (binary) which is 65535d (decimal), though I haven't tried it yet. If nL or nH aren't set to the proper values, then there will be trash characters printed along with the image.
Somehow, Android docs tells us that the limits of the value for a byte in a byte array is -128 and +127, when I tried to put in 255, Eclipse asked me to cast it to Byte.
Anyway, going back to nL and nW, for your case, you have an image with width 576, if we convert 576 to Binary, we get two bytes which would go like:
0000 0010 0100 0000
In this case, the Low Byte is 0100 0000
while the High Byte is 0000 0010
. Convert it back to decimal and we get nL = 64
and nH = 2
.
In my case, I printed an image that has width of 330px, converting 330 to binary we get:
0000 0001 0100 1010
In this case now, the Low Byte is 0100 1010
and the High Byte is 0000 0001
. Converting to decimal, we get nL = 74
and nH = 1
.
For more information, look at these documentation/tutorials:
Star Asia Mobile Printer Documentation
ECS-POS programming guide - really extensive
Another Documentation
The expanded version of the code above, with more explanation
Explanation of the code above
Hope these helps.
I solve it converting Bitmap to Byte array. Remember that your image must be black & white format.
For full source code: https://github.com/imrankst1221/Thermal-Printer-in-Android
public void printPhoto() {
try {
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.img);
if(bmp!=null){
byte[] command = Utils.decodeBitmap(bmp);
printText(command);
}else{
Log.e("Print Photo error", "the file isn't exists");
}
} catch (Exception e) {
e.printStackTrace();
Log.e("PrintTools", "the file isn't exists");
}
}