Search code examples
androidthermal-printerescpossunmi

How to decrease font and Remove underline from ESC/POS machine from Android?


I am sending ESC command to POS machine from Android via Bluetooth connection , but on the machine side font is Very large and Underline text is printed .

enter image description here

Machine have 58mm paper width .

Code:

    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,1);

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

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

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

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

                printUnicode();

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

                printText(leftRightAlign("Qty: Name" , "Price "));
                printCustom(new String(new char[32]).replace("\0", "."),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 **",0,1);
                printNewLine();
                printNewLine();

                outputStream.flush();

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

        }
    }

Printing String with Alignment and Font Size:

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,0x21,0x00};  // 0- normal size text
    byte[] bb = new byte[]{0x1B,0x21,0x08};  // 1- only bold text
    byte[] bb2 = new byte[]{0x1B,0x21,0x20}; // 2- bold with medium text
    byte[] bb3 = new byte[]{0x1B,0x21,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,e.getLocalizedMessage(),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,e.getLocalizedMessage(),Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(this,e.getLocalizedMessage(),Toast.LENGTH_SHORT).show();
    }
}


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

}

Constants:

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

PrintterCommand:

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, 0x21, 1, 0x1B,
            0x21, 1 };
    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 };

}

Solution

  • There was no problem with the code , Sunmi Device setting was turned on for :

    a. Double Width

    b. Underline text

    and setting the styling via ESC command had no effect . so please find the device default font style setting from manufactures instruction.

    For Sunmi setting can be found here:

    setting video