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

Posted

技术标签:

【中文标题】如何格式化蓝牙打印机的文本?【英文标题】:How to format Text for Bluetooth Printer? 【发布时间】:2018-10-16 09:52:00 【问题描述】:

我正在考虑如何将文本格式化为居中并使其字体更大。我测试了很多,不知道它是如何工作的......

这是我的代码

 byte[] center = new byte[]0x1B, 'a', 0x01;
 byte[] bold = new byte[]0x1B,0x21,0x08;

我将这些应用到这里

                outputStream.write(center);
                outputStream.write(header1.getBytes());
                outputStream.write(header2.getBytes());
                outputStream.write(header3.getBytes());
                outputStream.write(header4.getBytes());

                outputStream.write(bold);
                outputStream.write(dot.getBytes());
                outputStream.write(txnNo.getBytes());
                outputStream.write(name.getBytes());
                outputStream.write(amount.getBytes());
                outputStream.write(Date.getBytes());
                outputStream.write(Users.getBytes());

                outputStream.write(center);
                outputStream.write(company.getBytes());
                outputStream.write(space.getBytes());
                outputStream.write(space.getBytes());

实际上我希望标题比普通文本大并居中对齐。但我不断更改此示例0x1B,0x21,0x08。它给了我很多不同的结果...需要帮助...在此先感谢,不胜感激...

【问题讨论】:

没有解决办法...? 【参考方案1】:

对不起,我的回答迟到了。但是,它可能会在以后对其他人有所帮助。我使用了这个library 对我来说获得正确的文本对齐、大小和样式设置有点困难。最后,我能够弄清楚。我创建了以下两种方法分别打印和添加新行。

protected void printConfig(String bill, int size, int style, int align)

   //size 1 = large, size 2 = medium, size 3 = small
    //style 1 = Regular, style 2 = Bold
    //align 0 = left, align 1 = center, align 2 = right

    try

        byte[] format = new byte[]27,33, 0;
        byte[] change = new byte[]27,33, 0;

        outputStream.write(format);

        //different sizes, same style Regular
        if (size==1 && style==1)  //large
        
            change[2] = (byte) (0x10); //large
            outputStream.write(change);
        else if(size==2 && style==1) //medium
        
            //nothing to change, uses the default settings
        else if(size==3 && style==1) //small
        
            change[2] = (byte) (0x3); //small
            outputStream.write(change);
        

        //different sizes, same style Bold
        if (size==1 && style==2)  //large
        
            change[2] = (byte) (0x10 | 0x8); //large
            outputStream.write(change);
        else if(size==2 && style==2) //medium
        
            change[2] = (byte) (0x8);
            outputStream.write(change);
        else if(size==3 && style==2) //small
        
            change[2] = (byte) (0x3 | 0x8); //small
            outputStream.write(change);
        


        switch (align) 
            case 0:
                //left align
                outputStream.write(PrinterCommands.ESC_ALIGN_LEFT);
                break;
            case 1:
                //center align
                outputStream.write(PrinterCommands.ESC_ALIGN_CENTER);
                break;
            case 2:
                //right align
                outputStream.write(PrinterCommands.ESC_ALIGN_RIGHT);
                break;
        
        outputStream.write(bill.getBytes());
        outputStream.write(PrinterCommands.LF);
    catch(Exception ex)
        Log.e("error", ex.toString());
    

 protected void printNewLine() 
    try 
        outputStream.write(PrinterCommands.FEED_LINE);
     catch (IOException e) 
        e.printStackTrace();
    

下面的 PrinterCommands 类只是与上述方法相关的一个简短部分。

public class PrinterCommands 
public static final byte LF = 0x0A;
public static byte[] FEED_LINE = 10;

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 ;

要打印大号、粗体和居中,请调用 printConfig 方法,如下例所示

printConfig(header1.getBytes(),1,2,1);

【讨论】:

以上是关于如何格式化蓝牙打印机的文本?的主要内容,如果未能解决你的问题,请参考以下文章

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

uniapp实现蓝牙小票打印功能

C语言如何把以下格式的文本读出来打印到屏幕上

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

Bash脚本:如何在Linux Shell上输出和格式化文本

如何在JTextArea中设置文本格式