【问题标题】:How to print the text right aligned in a Bluetooth POS Printer?如何在蓝牙 POS 打印机中打印右对齐的文本?
【发布时间】: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");

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

【问题讨论】:

    标签: android printing bluetooth byte pos


    【解决方案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 };
      

      【讨论】:

        猜你喜欢
        • 2021-04-14
        • 2019-11-23
        • 1970-01-01
        • 2011-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-27
        • 1970-01-01
        相关资源
        最近更新 更多