如何在蓝牙 POS 打印机中打印右对齐的文本?

Posted

技术标签:

【中文标题】如何在蓝牙 POS 打印机中打印右对齐的文本?【英文标题】:How to print the text right aligned in a Bluetooth POS Printer? 【发布时间】:2018-12-08 06:59:46 【问题描述】:

我一直在尝试在 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");

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

【问题讨论】:

【参考方案1】:

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

例子,

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 个字符

谢谢。

【讨论】:

【参考方案2】:

添加此打印机命令

public static final byte[] ESC_ALIGN_RIGHT = new byte[]  0x1b, 'a', 0x02 ;

【讨论】:

以上是关于如何在蓝牙 POS 打印机中打印右对齐的文本?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Flutter 中使用带有打印机名称的以太网 POS 打印机打印收据?

如何在 Android 上使用 Python 通过蓝牙发送 ESC 打印命令?

如何在 Linux 中向热敏打印机发送 ESC/POS 命令

如何格式化蓝牙打印机的文本?

如何在 Android 的蓝牙打印机上打印图像?

如何使用javascript将单词向右对齐