Android 如何格式化蓝牙打印机的文本?
我正在考虑如何将文本格式设置为中心,并使其字体更大。我做了很多测试,不知道它是怎么工作的 这是我的代码Android 如何格式化蓝牙打印机的文本?,android,printing,bluetooth,format,printers,Android,Printing,Bluetooth,Format,Printers,我正在考虑如何将文本格式设置为中心,并使其字体更大。我做了很多测试,不知道它是怎么工作的 这是我的代码 byte[] center = new byte[]{0x1B, 'a', 0x01}; byte[] bold = new byte[]{0x1B,0x21,0x08}; 我把这些应用到这里 outputStream.write(center); outputStream.write(header1.getBytes())
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}。它给了我很多不同的结果。。。需要帮助。。。提前谢谢,谢谢…很抱歉我的回答晚了。不过,它以后可能会帮助其他人。我使用了它,但要获得文本对齐、大小和样式的正确设置对我来说有点困难。最后,我终于明白了。我创建了下面的两个方法,分别用于打印和添加新行
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);
没有办法。。。?