ESC/POS 热敏打印机,如何在 Android 中将位图图像居中? [英] ESC/POS thermal printer, how to center a bitmap image in Android?
问题描述
我正在 Android 中编写一个简单的应用程序,以通过 ESC/POS 热敏打印机进行打印.我只是有问题.该应用程序生成一个二维码(使用 zxing 库),将其转换为位图并将其发送到打印机.打印机打印出来,但我无法将它居中.相反,文本我没有定位问题.
I'm writing a simple app in Android to print through a ESC/POS thermal printer. I've just a problem. The app generate a QR Code (with zxing library), convert it in a bitmap and send it to the printer. The printer print it, but I'm not able to center it. Instead with text I have no positioning issue.
这是打印文本和二维码的代码.
This is the code to print the text and the QR.
byte[] INIT = {27, 64};
byte[] FEED_LINE = {10};
byte[] SELECT_FONT_A = {27, 33, 0};
byte[] FONT_B = {0x1B, 0x4D, 0x01};
byte[] ALLINEA_SX = {0x1B, 0x61, 0x00};
byte[] ALLINEA_CT = {0x1B, 0x61, 0x01};
byte[] ALLINEA_DX = {0x1B, 0x61, 0x02};
byte[] GRASSETTO_ON = {0x1B, 0x47, 0x11};
byte[] GRASSETTO_OFF = {0x1B, 0x47, 0x00};
byte[] SET_6 = {0x1B, 0x52, 0x06};
byte[] CODICI = {0x1B, 0x74, 0x13};
byte[] EURO = {(byte) 0xD5};
byte[] PALLINO = {(byte) 0x5B};
byte[] FONT_3X = {0x1D, 0x21, 0x21};
byte[] FONT_2X = {0x1D, 0x21, 0x11};
byte[] FONT_1X = {0x1D, 0x21, 0x00};
byte[] SET_BAR_CODE_HEIGHT = {29, 104, 100};
byte[] PRINT_BAR_CODE_1 = {29, 107, 2};
byte[] SEND_NULL_BYTE = {0x00};
byte[] SELECT_PRINT_SHEET = {0x1B, 0x63, 0x30, 0x02};
byte[] FEED_PAPER_AND_CUT = {0x1D, 0x56, 66, 0x00};
byte[] SELECT_CYRILLIC_CHARACTER_CODE_TABLE = {0x1B, 0x74, 0x11};
byte[] SELECT_BIT_IMAGE_MODE = {0x1B, 0x2A, 33, 127, 3};
byte[] SET_LINE_SPACING_24 = {0x1B, 0x33, 24};
byte[] SET_LINE_SPACING_30 = {0x1B, 0x33, 30};
byte[] TRANSMIT_DLE_PRINTER_STATUS = {0x10, 0x04, 0x01};
byte[] TRANSMIT_DLE_OFFLINE_PRINTER_STATUS = {0x10, 0x04, 0x02};
byte[] TRANSMIT_DLE_ERROR_STATUS = {0x10, 0x04, 0x03};
byte[] TRANSMIT_DLE_ROLL_PAPER_SENSOR_STATUS = {0x10, 0x04, 0x04};
mService.write(INIT);
mService.write(ALLINEA_CT); //text to center
mService.write(SELECT_FONT_A);
mService.write(FONT_2X);
mService.write("Hello stackoverflow!!\n".getBytes());
mService.write(FONT_1X);
mService.write(ALLINEA_SX); //text to left
mService.write("Thanks for help!\n".getBytes());
int QRCODE_IMAGE_HEIGHT = 255;
int QRCODE_IMAGE_WIDTH = 255;
QRCodeWriter qrWriter = new QRCodeWriter();
try {
Hashtable<EncodeHintType, ErrorCorrectionLevel> hints = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
BitMatrix bitMatrix = qrWriter.encode("https://stackoverflow.com",
BarcodeFormat.QR_CODE,
QRCODE_IMAGE_WIDTH,
QRCODE_IMAGE_HEIGHT, hints);
int bitMatrixWidth = bitMatrix.getWidth();
int bitMatrixHeight = bitMatrix.getHeight();
int[] pixels = new int[bitMatrixWidth * bitMatrixHeight];
for (int y = 0; y < bitMatrixHeight; y++) {
int offset = y * bitMatrixWidth;
for (int x = 0; x < bitMatrixWidth; x++) {
pixels[offset + x] = bitMatrix.get(x, y) ?
getResources().getColor(R.color.QRCodeBlackColor):getResources().getColor(R.color.QRCodeWhiteColor);
}
}
Bitmap bitmap = Bitmap.createBitmap(bitMatrixWidth, bitMatrixHeight, Bitmap.Config.ARGB_4444);
bitmap.setPixels(pixels, 0, 255, 0, 0, bitMatrixWidth, bitMatrixHeight);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.k_city5);
Bitmap bmOverlay = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bitmap, new Matrix(), null);
canvas.drawBitmap(bmp, 94, 94, null); //34x34
//canvas.drawBitmap(bmp, 86, 86, null); //42x42
if(bmOverlay!=null) {
byte[] command = Utils.decodeBitmap(bmOverlay);
mService.write(ALLINEA_CT); //This command should center my QR, but it's not working
mService.write(command);
mService.write(FEED_LINE);
mService.write(FEED_LINE);
}
这是打印的结果:
推荐答案
在左侧填充图像,直到看起来正确为止.请参阅Android:用白色填充位图.
Pad the image on the left until it looks right. See Android: padding left a bitmap with white color.
您可以通过反复试验来做到这一点,或者以点为单位(在其数据表中提供)获取打印机的宽度并进行数学计算:
You can do this via trial and error, or take the width of the printer in dots (available in its data sheet) and do the maths:
padding = (page width - image width) / 2
ESC/POS 中有一些命令可以使文本居中,而在某些 打印机上,这将使图像居中.还有其他方法可以在给定位置(页面模式)打印图像,或者以更像文本(列格式)的方式对齐,但是填充图像是对示例的更简单的修改.
There are commands in ESC/POS to centre text, and on some printers, this will centre an image. There are also alternative ways to print an image at a given location (page mode), or in a way that aligns more like text (column format), but padding the image is a much simpler modification to your example.
这篇关于ESC/POS 热敏打印机,如何在 Android 中将位图图像居中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!