Printing BitImages on a thermal printer
Printing an Image (jpg,bmp,png,gif, whatever) via the BitImaging ESC * command.
Oh man, that was a hard nut to crack. The documentation on what to do is very precise and has that “shame on you if you make a mistake. just read more carefully!” philosophy to it. You need to define how much data is coming through per Image. Fair enough, it’s Widht*Hight px. …No its not. It’s byte*dotlines?
Byte of data Horizontal by dotlines in vertical direction. That’s a nice specification of a resolution. For BitImage printing on a 3inch Printer the width of an image must be 576pixel or to be exact, the data to be transmitted must be at least 72bytes. Makes sense, because the printer only prints the data that is in its buffer after a lineFeed. So it only prints full lines period.
Well then, lets load an image and convert it to black and white (1bit depth of course) and fill the empty space up to 576pixel with white.
In the code below I look at every pixel of an image. convert the pixel to gray values by weighting each color in a rough percentages like the human eye would weight them. After that I compare the 8bit gray scale value ranging from 0 - 255 with my threshold of 127. That means, if the average gray value of a pixel would be 120 I would print white because it’s not gray enough to be black.
Code Snippets:
for(int y = 0; y < fillHeight; y++){
for(int x = 0; x < fillWidth; x++){
if(x < myImage.getWidth()) {
java.awt.Color myColor = new java.awt.Color(myImage.getRGB(x,y));
int luma = (int)(myColor.getRed() * 0.3 + myColor.getGreen() * 0.59 + myColor.getBlue() * 0.11);
luma8[x][y] = luma;
dots[x][y] = (luma < threshold);
}else {
dots[x][y] = false;
}
}
}
Actuall Printing via ESC* command:
In retrospect - I could have made a better loop to write the values to the byte. But what the hell, it works.
int cHeight = dimensions[0].length;
int cWidth = 576;
int pixel = cWidth*cHeight;
picHeight[count] = myImage.getHeight() & 0xFF;
picHeight[count+1] = (myImage.getHeight() » 8) & 0xFF;
myPort.write(0x1B);//ESC
myPort.write(0x2A);//*
myPort.write((byte)97);//72byte wide
myPort.write(picHeight[count]);
myPort.write(picHeight[count+1]);
if(cWidth == 576 && cHeight <= 1023){
while(i < pixel) {
int slice = 0;
for(int b=0; b < 8;b++) {
boolean v = false;
if(y < cHeight){
if(x < cWidth){
v= dots[x][y];
x++;
}else{
x=0;
y++;
v= dots[x][y];
x++;
}
}
i++;
if(v == true) {
slice = slice | (1 « (7-b));
}else {
slice = slice | (0 « (7-b));
}
}
myPort.write((byte)slice);
}
println(“——-DONE———”);
}else{
println(“IMAGE EXCEEDS DIMENSIONS 576x1023”);
}
Before and on the thermal paper
