位图未在 kotlin 中的 58mm 热敏打印机的对齐中心打印

Posted

技术标签:

【中文标题】位图未在 kotlin 中的 58mm 热敏打印机的对齐中心打印【英文标题】:Bitmap not print in the align center of 58mm thermal Printer in kotlin 【发布时间】:2021-12-12 19:07:11 【问题描述】:

我将布局转换为位图,然后在 58 毫米热敏打印机中打印位图,但图像不在中心打印,它总是在左侧打印。如何在热敏纸的中心打印位图?

 val w = resources.getDimension(R.dimen.test_58mm).toInt()
    val h = binding.printLayout.getChildAt(0).height

    bitmap = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888)
    val c = Canvas(bitmap)
    val sizeSpecWidth: Int = View.MeasureSpec.makeMeasureSpec(w, View.MeasureSpec.EXACTLY)
    val sizeSpecHeight: Int = View.MeasureSpec.makeMeasureSpec(h, View.MeasureSpec.EXACTLY)
    printLayoutBinding.root.measure(sizeSpecWidth, sizeSpecHeight)

    val width: Int = printLayoutBinding.root.measuredWidth
    val height: Int = printLayoutBinding.root.measuredHeight
    Log.d(TAG, "createSnapshot: " + h)
    printLayoutBinding.root.layout(0, 0, width, height)

    printLayoutBinding.root.draw(c)

【问题讨论】:

【参考方案1】:

您可以添加名为 PrintCommands 的新类,然后在打印之前添加此行

BLUETOOTH_PRINTER!!.write(PrinterCommands.ESC_ALIGN_CENTER)
BLUETOOTH_PRINTER!!.printImage(bitmap)

PrintCommands 类

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 ;

【讨论】:

以上是关于位图未在 kotlin 中的 58mm 热敏打印机的对齐中心打印的主要内容,如果未能解决你的问题,请参考以下文章

Gprinter光栅位图点阵数据解析工具

如何在 Flutter 中打印到热敏网络打印机?

热敏条码打印机怎样连接手机使用

佳博热敏条码打印机怎么使用

如何用Lodop能过USB热敏打印机打开钱箱

wx.getBLEDeviceCharacteristics 微信小程序蓝牙 微信小程序热敏打印机