Printer doesn't take the alignment of the image in to account. The logo is always left aligned by default. Text gets aligned with no problem. So there is nothing wrong with the aligncenter()
method .When I set the alignment inside the while loop, a series of question marks get printed all over the paper.
public void printImage(Bitmap bitmap, boolean center) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int newWidth = this.mType58 ? 384 : 576;
if (width > (this.mType58 ? 384 : 576)) {
bitmap = LibUtil.setImgSize(bitmap, (float) newWidth, (float) (height / (width / newWidth)), (float) width, (float) height);
}
try {
PrinterWriter mThirdPrinter = this.mType58 ? new PrinterWriter58mm(384, 384) : new PrinterWriter80mm(576, 576);
ArrayList<byte[]> datas = mThirdPrinter.getImageByte(bitmap);
if (mThirdPrinter.getDataAndClose() == null) {
if (this.mCallback != null) {
this.mCallback.onError(ErrorCode.IMAGE_NOT_FONUD);
}
return;
}
try {
datas.add(mThirdPrinter.getDataAndClose());
} catch (NullPointerException e) {
e.printStackTrace();
}
if (datas == null) {
DebugLog.LogD("imgpath is empty, datas is null, maybe is TFCard");
return;
}
this.mPrinterModule.sendData(new byte[]{(byte) 27, (byte) 74, (byte) 0});
Iterator it = datas.iterator();
byte [] alignment = alignCenter();
int i = 0 ;
byte [] data1 = null ;
while (it.hasNext()) {
data1[i] = (byte) it.next();
}
this.mPrinterModule.sendData(alignment);
this.mPrinterModule.sendData(data1);
} catch (IOException e2) {
e2.printStackTrace();
}
}
when I was in another office, and this kind of project I had a similar problem, all the alignment features in the library didn't work. But i have tricky solution to make Logo image get Center. I draw a bitmap with a white or transparent resolution and place the position of the width just right in the middle of the receipt
public Bitmap createBitmap(Rect rectImage, int i, int j) {
Paint p = new Paint();
p.setStyle(Style.FILL_AND_STROKE);
p.setAntiAlias(true);
p.setFilterBitmap(true);
p.setDither(true);
p.setColor(Color.WHITE);
Bitmap bitmap = Bitmap.createBitmap(rectImage.width() * 2,
rectImage.height() * 2, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
// c.drawColor(Color.RED);
c.drawRect(rectImage.left, rectImage.top, rectImage.right,
rectImage.bottom, p);
return bitmap;
}
Then merge this bitmap with your Logo, please use code similar to this (not this code)
public static Bitmap mergeToPin(Bitmap left, Bitmap right) {
Bitmap result = Bitmap.createBitmap(left.getWidth(), left.getHeight(), left.getConfig());
Canvas canvas = new Canvas(result);
int widthleft = left.getWidth();
int widthright = right.getWidth();
canvas.drawBitmap(left, 0f, 0f, null);
canvas.drawBitmap(right, widthleft, 0f, null);
return result;
}
As you know, the size of the receipt can be predicted. So you can make static value for width size of transparent image