如何从Android的ESC / POS机上减少字体和删除下划线?

如何解决如何从Android的ESC / POS机上减少字体和删除下划线?

我正在通过蓝牙连接从 Android 向 POS 机发送 ESC 命令,但在机器端字体非常大并且打印了下划线文本。

enter image description here

机器有58mm的纸宽。

代码

    protected void printBill() {
        if(mBluetoothSocket == null){
           askToScanDevice();
        }
        else{
 
            try {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printstacktrace();
                }
                outputStream = mBluetoothSocket.getoutputStream();

                 resetPrint();
                // ***********************************************
                String merchantName = "My App Market";
                printCustom(merchantName,3,1);

                String restaurantName = "Mac Donald";
                printCustom(restaurantName,2,1);

                String restaurantAddress = "Gurugram,Haryana 12208,India";
                printCustom(restaurantAddress,1,1);

                String restaurantPhone = "Phone: +91-0987654321";
                printCustom(restaurantPhone,1);

                printUnicode();
                // ***********************************************
                printCustom("To ",0);

                String customerName = "Abhinav Raj";
                printCustom(customerName,0);

                String customerAddress = "Jharkhand 12208,India";
                printCustom(customerAddress,0);

                String customerPhone = "Phone: +91-0987654321";
                printCustom(customerPhone,0);

                printUnicode();

                //***********************************************

                printText(leftRightAlign("Qty: Name","Price "));
                printCustom(new String(new char[32]).replace("\0","."),1);

                printUnicode();

                //***********************************************

                for (int i=1; i< 3;i++) {
                    String qty = "" + i;
                    String productName = "Product " + i;
                    String productQty = qty + " " + productName;
                    printText(leftRightAlign(productQty,"$200"));
                    printNewLine();
                }

                //***********************************************
                printUnicode();
                printText(leftRightAlign("Total","$600"));
                //***********************************************
                printUnicode();

                printNewLine();

                printCustom("** THANK YOU VISIT AGAIN **",1);
                printNewLine();
                printNewLine();

                outputStream.flush();

            } catch (IOException e) {
                e.printstacktrace();
                Toast.makeText(this,e.getLocalizedMessage(),Toast.LENGTH_LONG).show();
             }

        }
    }

带有对齐和字体大小的打印字符串:

private void printCustom(String msg,int size,int align) {
    //Print config "mode"
    byte[] cc = new byte[]{0x1B,0x21,0x03};  // 0- normal size text
    //byte[] cc1 = new byte[]{0x1B,0x00};  // 0- normal size text
    byte[] bb = new byte[]{0x1B,0x08};  // 1- only bold text
    byte[] bb2 = new byte[]{0x1B,0x20}; // 2- bold with medium text
    byte[] bb3 = new byte[]{0x1B,0x10}; // 3- bold with large text
    try {
        switch (size){
            case 0:
                outputStream.write(cc);
                break;
            case 1:
                outputStream.write(bb);
                break;
            case 2:
                outputStream.write(bb2);
                break;
            case 3:
                outputStream.write(bb3);
                break;
        }

        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(msg.getBytes());
        outputStream.write(PrinterCommands.LF);
        //outputStream.write(cc);
        //printNewLine();
    } catch (IOException e) {
        e.printstacktrace();
        Toast.makeText(this,Toast.LENGTH_SHORT).show();
    }

} 




  public  void resetPrint() {
        try{
           outputStream.write(PrinterCommands.ESC_FONT_COLOR_DEFAULT);
           outputStream.write(PrinterCommands.FS_FONT_ALIGN);
           outputStream.write(PrinterCommands.ESC_ALIGN_LEFT);
           outputStream.write(PrinterCommands.ESC_CANCEL_BOLD);
           outputStream.write(PrinterCommands.LF);
        } catch (IOException e) {
            e.printstacktrace();
        }
    }

//print unicode
public void printUnicode(){
    try {
        outputStream.write(PrinterCommands.ESC_ALIGN_CENTER);
        printText(Utils.UNICODE_TEXT);
        printText(Utils.UNICODE_TEXT);
    } catch (UnsupportedEncodingException e) {
        e.printstacktrace();
        Toast.makeText(this,Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        e.printstacktrace();
        Toast.makeText(this,Toast.LENGTH_SHORT).show();
    }
}


//print new line
private void printNewLine() {
    try {
        outputStream.write(PrinterCommands.Feed_LINE);
    } catch (IOException e) {
        e.printstacktrace();
        Toast.makeText(this,Toast.LENGTH_SHORT).show();
    }

}

常量:

 public static final byte[] UNICODE_TEXT = new byte[] {0x2D,0x2D,0x2D};

打印机命令:

public class PrinterCommands { 

    public static final byte LF = 0x0A;
 public static byte[] Feed_LINE = {10};
  public static final byte[] ESC_FONT_COLOR_DEFAULT = new byte[] { 0x1B,'r',0x00 };
    public static final byte[] FS_FONT_ALIGN = new byte[] { 0x1C,0x1B,1 };
    public static final byte[] ESC_ALIGN_LEFT = new byte[] { 0x1b,'a',0x00 };
    public static final byte[] ESC_ALIGN_RIGHT = new byte[] { 0x1b,0x02 };
    public static final byte[] ESC_ALIGN_CENTER = new byte[] { 0x1b,0x01 };
    public static final byte[] ESC_CANCEL_BOLD = new byte[] { 0x1B,0x45,0 };

}

解决方法

代码没有问题,商米设备设置为:

一个。双倍宽度

b.下划线文本

并且通过 ESC 命令设置样式没有效果。所以请从制造商说明中找到设备默认字体样式设置。

关于商米设置可以在这里找到:

setting video

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?