gpt4 book ai didi

android - Logo 图像未在收据打印机中居中对齐

转载 作者:行者123 更新时间:2023-11-29 02:22:45 25 4
gpt4 key购买 nike

打印机不考虑图像的对齐方式。默认情况下, Logo 始终左对齐。文本对齐没有问题。所以 aligncenter() 方法没有任何问题。当我在 while 循环中设置对齐方式时,一系列问号打印在整张纸上。

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();
}

}

最佳答案

当我在另一个办公室时,我遇到了类似的问题,库中的所有对齐功能都不起作用。但是我有一个棘手的解决方案来使 Logo 图像居中。我画了一张白色或者透明分辨率的位图,把宽度的位置放在收据正中间

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;

}

然后将这个位图与你的Logo合并,请使用类似这样的代码(不是这个代码)

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;
}

如您所知,收据的大小是可以预测的。所以你可以为透明图像的宽度大小设置静态值

关于android - Logo 图像未在收据打印机中居中对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54274475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com