Как напечатать текст с выравниванием по праву на принтере Bluetooth POS? - PullRequest
0 голосов
/ 29 июня 2018

Я пытался создать приложение для POS на Android, где я печатаю квитанцию ​​на принтере Bluetooth, подключенном к приложению. Я могу напечатать «большой», «маленький» и QR-код с помощью HOINSDK, поставляемой с устройством, а также могу выровнять «левый» и «центральный», но не могу выровнять его «вправо». Любая помощь приветствуется.

Ниже приведены коды для соответствующих вещей:

//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 Ответ

0 голосов
/ 29 июня 2018

Раньше я сталкивался с такой же проблемой, но не нашел решения, поэтому в качестве решения использую метод ниже,

Пример,

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 символа .

Спасибо.

...