如何解决如何从Android的ESC / POS机上减少字体和删除下划线?
我正在通过蓝牙连接从 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);
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 命令设置样式没有效果。所以请从制造商说明中找到设备默认字体样式设置。
关于商米设置可以在这里找到:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。