First of all to print image or logo or label to Thermal Printer, please decide the width of Image to be printed. Here I have described all printer commands for different widths of Images. Height does not depend on these commands. Height can be of any size. If the width-size of your Image does not fit to any size of width given below then you have generate the command on your own.
For example,
The binary bits of 256 is {1,0,0,0,0,0,0,0,0}. So the value of first significant byte is 1 and the value of last significant byte is 0. Now the command will be in little-endian format that is first 0 comes and then 1 comes. So combination will be {0, 1}.
Hence the command will be {0x1B, 0x2A, 33, 0, 1}.
Likewise you have to generate the command on your own depending on the width-size of the Image. .
public static byte[] SELECT_BIT_IMAGE_MODE_256 = {0x1B, 0x2A, 33, 0, 1}; // for width 256
public static byte[] SELECT_BIT_IMAGE_MODE_320 = {0x1B, 0x2A, 33, (byte)64, 1}; //for width 320
public static byte[] SELECT_BIT_IMAGE_MODE_350 = {0x1B, 0x2A, 33, (byte)94, 1};//for width 356
public static byte[] SELECT_BIT_IMAGE_MODE_400 = {0x1B, 0x2A, 33, (byte)144, 1};//for width 400
public static byte[] SELECT_BIT_IMAGE_MODE_500 = {0x1B, 0x2A, 33, (byte)244, 1};//for width 500
public static byte[] SELECT_BIT_IMAGE_MODE_550 = {0x1B, 0x2A, 33, (byte)38, 2};//for width 550
public static byte[] SELECT_BIT_IMAGE_MODE_600 = {0x1B, 0x2A, 33, (byte)88, 2};//for width 600
public static byte[] SELECT_BIT_IMAGE_MODE_150 = {0x1B, 0x2A, 33, (byte) 150, 0};//for width 150
public static byte[] SELECT_BIT_IMAGE_MODE_128 = {0x1B, 0x2A, 33, (byte) 128, 0};//for width 128
public static byte[] SELECT_BIT_IMAGE_MODE_160 = {0x1B, 0x2A, 33, (byte)160, 0};//for width 160
Height of the Image is immaterial for printing to Thermal Printer.
int mWidth;
int mHeight;
BitSet dots;
public String printlogo256(OutputStream outputStream) {
String errresult=””;
try {
BitmapFactory.Options opt=new BitmapFactory.Options();
opt.inScaled=false;
// logo_256 is the logo to be printed.
Bitmap bmp=BitmapFactory.decodeResource(context.getResources(),R.drawable.logo_256,opt);
errresult+=convertBitmap(bmp); // convert color Image to Gray Scale Image
outputStream.write(PrinterCommands.SET_LINE_SPACING_24);
int offset = 0;
while (offset < bmp.getHeight()) {
outputStream.write( PrinterCommands.CENTER_JUSTIFICATION ); // print image at center vertically
outputStream.write(PrinterCommands.SELECT_BIT_IMAGE_MODE_256);
for (int x = 0; x < bmp.getWidth(); ++x) {
for (int k = 0; k <3; ++k) {
byte slice = 0;
for (int b = 0; b < 8; ++b) {
int y = (((offset/8) + k) * 8) + b;
int i = (y * bmp.getWidth()) + x;
boolean v = false;
if (i < dots.length()) {
v = dots.get(i);
}
slice |= (byte) ((v ? 1 : 0)<< (7-b));
}
outputStream.write(slice);
}
}
offset += 24;
outputStream.write(PrinterCommands.FEED_LINE);
}
outputStream.write(PrinterCommands.SET_LINE_SPACING_30);
if(bmp!=null){
bmp.recycle();
bmp=null;
}
// System.out.println(“Go to the end of printing”);
} catch (IOException ioe) {
errresult+=””+ioe+”\r\n”;
}
return errresult;
}
Here are the following codes of convertBitmap–
public String convertBitmap(Bitmap inputBitmap) {
String errresult=””;
mWidth = inputBitmap.getWidth();
mHeight = inputBitmap.getHeight();
errresult+=convertArrayToGrayScale(inputBitmap, mWidth, mHeight);
return errresult;
}
Here are the codes of convertArrayToGrayScale—
private String convertArrayToGrayScale(Bitmap original, int width, int height) {
String errresult=””;
int pixel;
int k = 0;
int B, G, R;
dots = new BitSet();
try {
for (int x = 0; x < height; x++) {
for (int y = 0; y < width; y++) {
pixel = original.getPixel(y, x);
//retrieve color of all channels
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
//take conversion up to one single value by calculating pixel intensity
R=G=B=(int) (0.299 * R + 0.587 * G + 0.114 * B);
//set bit into bitset, by calculating the pixel’s luma
if (R < 55) {
dots.set(k);//this is the bitset that i’m printing
}
k++;
}
}
} catch (Exception e) {
errresult+=””+e.toString()+”\r\n”;
}
return errresult;
}
Use these above codes for printing any Image/Logo/label directly to Thermal Printer. Here Android Library-Bitmap has been used since Development has been done for Android Mobile Application. You need to change Bitmap class for your preferred language.
For example, to develop for Desktop Application using java language, you have to use BufferedImage class instead of Bitmap since Bitmap is only supported by Android Library.