如何从 Android 减少 ESC/POS 机器的字体和删除下划线?
How to decrease font and Remove underline from ESC/POS machine from Android?
我正在通过蓝牙连接从 Android 向 POS 机发送 ESC 命令,但是在机器端字体非常大并且打印了下划线文本。
机器有 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,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("[=12=]", "."),0,1);
printUnicode();
//***********************************************
for (int i=1; i< 3;i++) {
String qty = "" + i;
String productName = "Product " + i;
String productQty = qty + " " + productName;
printText(leftRightAlign(productQty , "0"));
printNewLine();
}
//***********************************************
printUnicode();
printText(leftRightAlign("Total" , "0"));
//***********************************************
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();
}
}
}
使用对齐方式和字体大小打印字符串:
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();
}
}
常数:
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};
打印机命令:
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 };
}
代码没有问题,商米设备设置已开启:
一个。双倍宽度
b.下划线文字
通过 ESC 命令设置样式无效。所以请从制造商说明中找到设备默认字体样式设置。
商米的设置可以在这里找到:
我正在通过蓝牙连接从 Android 向 POS 机发送 ESC 命令,但是在机器端字体非常大并且打印了下划线文本。
机器有 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,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("[=12=]", "."),0,1);
printUnicode();
//***********************************************
for (int i=1; i< 3;i++) {
String qty = "" + i;
String productName = "Product " + i;
String productQty = qty + " " + productName;
printText(leftRightAlign(productQty , "0"));
printNewLine();
}
//***********************************************
printUnicode();
printText(leftRightAlign("Total" , "0"));
//***********************************************
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();
}
}
}
使用对齐方式和字体大小打印字符串:
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();
}
}
常数:
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};
打印机命令:
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 };
}
代码没有问题,商米设备设置已开启:
一个。双倍宽度
b.下划线文字
通过 ESC 命令设置样式无效。所以请从制造商说明中找到设备默认字体样式设置。
商米的设置可以在这里找到: