0

我一直在尝试在 android 中做一个 POS 应用程序,我在连接到该应用程序的蓝牙打印机上打印收据。我可以使用设备随附的 HOINSDK 打印“大”、“小”和 QR 码,并且可以对齐“左”和“中心”,但无法将其与“右”对齐。任何帮助表示赞赏。

以下是各个事物的代码:

//For Large Text

                cmd[0] = 27;
                cmd[1] = 97;
                cmd[2] &= 0xEF;
                mService.write(cmd);
                mService.sendMessage("" + large_txt.getText().toString(), "GBK");

//For Small Text
                cmd[0] = 0x1b;
                cmd[1] = 0x21;
                cmd[2] &= 0xEF;
                mService.write(cmd);          
                mService.sendMessage("" + small_txt.getText().toString(), "GBK");

//For Center Alignment small text
                cmd[0] = 27;
                cmd[1] = 97;
                cmd[2] |= 1;
                mService.write(cmd);
                cmd[0] = 27;
                cmd[1] = 97;
                cmd[2] = 1;
                mService.write(cmd);  
                mService.sendMessage("" + center_txt.getText().toString(), "GBK");

//For centre alignment Large Text
                cmd[1] = 97;
                cmd[2] |= 1;
                mService.write(cmd);
                cmd[0] = 0x1b;
                cmd[1] = 0x21;
                cmd[2] = 0x10;
                mService.write(cmd);  
                mService.sendMessage("" + center_txt.getText().toString(), "GBK");

//For Left alignment
                cmd[0] = 27;
                cmd[1] = 97;
                cmd[2] |= 1;
                mService.write(cmd);
                cmd[0] = 27;
                cmd[1] = 97;
                cmd[2] = 0;
                mService.write(cmd);
                mService.sendMessage("" + left_txt.getText().toString(), "GBK");

如果有人可以帮助我使用正确的对齐代码,那将会很有帮助。

4

2 回答 2

0

添加此打印机命令

public static final byte[] ESC_ALIGN_RIGHT = new byte[] { 0x1b, 'a', 0x02 };
于 2021-11-03T04:52:10.123 回答
0

我之前遇到过同样的问题,但我没有找到任何解决方案,所以我使用以下方法作为解决方案,

例子,

String largetext = "Title";
String largeFontMessage = getWhiteSpace(24 - largetext.length()) + largetext;
String smalltext = "Message";
String smallFontMessage = getWhiteSpace(42 - smalltext.length()) + smalltext;

// print largeFontMessage;
// print smallFontMessage;

public static String getWhiteSpace(int size) {
    StringBuilder builder = new StringBuilder(size);
    for (int i = 0; i < size; i++) {
        builder.append(' ');
    }
    return builder.toString();
}

在我的情况下,单行小文本包含 42 个字符大文本包含 24 个字符

谢谢。

于 2018-06-29T08:09:55.863 回答