>打印机不考虑图像的对齐方式。默认情况下,徽标始终左对齐。文本对齐没有问题。所以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();
}
}
当我在另一个办公室时,这种项目我遇到了类似的问题,库中的所有对齐功能都不起作用。但是我有棘手的解决方案可以使徽标图像成为中心。我用白色或透明分辨率绘制一个位图,并将宽度的位置恰好放在收据的中间
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;
}
然后将此位图与您的徽标合并,请使用与此类似的代码(不是此代码)
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;
}
如您所知,可以预测收据的大小。因此,您可以为透明图像的宽度大小创建静态值
您可以添加此新类并在打印命令之前添加此行
mThirdPrinter.write(PrinterCommands.ESC_ALIGN_CENTER);
public class PrinterCommands {
public static final byte HT = 0x9;
public static final byte LF = 0x0A;
public static final byte CR = 0x0D;
public static final byte ESC = 0x1B;
public static final byte DLE = 0x10;
public static final byte GS = 0x1D;
public static final byte FS = 0x1C;
public static final byte STX = 0x02;
public static final byte US = 0x1F;
public static final byte CAN = 0x18;
public static final byte CLR = 0x0C;
public static final byte EOT = 0x04;
public static final byte[] INIT = {27, 64};
public static byte[] FEED_LINE = {10};
public static byte[] SELECT_FONT_A = {20, 33, 0};
public static byte[] SET_BAR_CODE_HEIGHT = {29, 104, 100};
public static byte[] PRINT_BAR_CODE_1 = {29, 107, 2};
public static byte[] SEND_NULL_BYTE = {0x00};
public static byte[] SELECT_PRINT_SHEET = {0x1B, 0x63, 0x30, 0x02};
public static byte[] FEED_PAPER_AND_CUT = {0x1D, 0x56, 66, 0x00};
public static byte[] SELECT_CYRILLIC_CHARACTER_CODE_TABLE = {0x1B, 0x74, 0x11};
public static byte[] SELECT_BIT_IMAGE_MODE = {0x1B, 0x2A, 33, -128, 0};
public static byte[] SET_LINE_SPACING_24 = {0x1B, 0x33, 24};
public static byte[] SET_LINE_SPACING_30 = {0x1B, 0x33, 30};
public static byte[] TRANSMIT_DLE_PRINTER_STATUS = {0x10, 0x04, 0x01};
public static byte[] TRANSMIT_DLE_OFFLINE_PRINTER_STATUS = {0x10, 0x04, 0x02};
public static byte[] TRANSMIT_DLE_ERROR_STATUS = {0x10, 0x04, 0x03};
public static byte[] TRANSMIT_DLE_ROLL_PAPER_SENSOR_STATUS = {0x10, 0x04, 0x04};
public static final byte[] ESC_FONT_COLOR_DEFAULT = new byte[] { 0x1B, 'r',0x00 };
public static final byte[] FS_FONT_ALIGN = new byte[] { 0x1C, 0x21, 1, 0x1B,
0x21, 1 };
public static final byte[] ESC_ALIGN_LEFT = new byte[] { 0x1b, 'a', 0x00 };
public static final byte[] ESC_ALIGN_RIGHT = new byte[] { 0x1b, 'a', 0x02 };
public static final byte[] ESC_ALIGN_CENTER = new byte[] { 0x1b, 'a', 0x01 };
public static final byte[] ESC_CANCEL_BOLD = new byte[] { 0x1B, 0x45, 0 };
/*********************************************/
public static final byte[] ESC_HORIZONTAL_CENTERS = new byte[] { 0x1B, 0x44, 20, 28, 00};
public static final byte[] ESC_CANCLE_HORIZONTAL_CENTERS = new byte[] { 0x1B, 0x44, 00 };
/*********************************************/
public static final byte[] ESC_ENTER = new byte[] { 0x1B, 0x4A, 0x40 };
public static final byte[] PRINTE_TEST = new byte[] { 0x1D, 0x28, 0x41 };
}