Java 已解释ESC/POS命令
我正在开发一个应用程序,使用蓝牙热敏打印机打印收据。我可以连接并使用打印机打印收据,但我无法理解所有这些ESC/POS命令的含义 打印机在黑色背景上打印白色文本,我实际上希望文本是黑色和白色背景。我不知道如何使用ESC/POS命令实现这种格式 这是我的打印代码:Java 已解释ESC/POS命令,java,android,printing,escpos,Java,Android,Printing,Escpos,我正在开发一个应用程序,使用蓝牙热敏打印机打印收据。我可以连接并使用打印机打印收据,但我无法理解所有这些ESC/POS命令的含义 打印机在黑色背景上打印白色文本,我实际上希望文本是黑色和白色背景。我不知道如何使用ESC/POS命令实现这种格式 这是我的打印代码: if (btsocket == null) { Intent BTIntent = new Intent(getApplicationContext(), DeviceList.class);
if (btsocket == null) {
Intent BTIntent = new Intent(getApplicationContext(), DeviceList.class);
this.startActivityForResult(BTIntent, DeviceList.REQUEST_CONNECT_BT);
} else {
OutputStream opstream = null;
try {
opstream = btsocket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
outputStream = opstream;
//print command
try {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
outputStream = btsocket.getOutputStream();
byte[] format = { 27, 33, 0 };
byte[] printformat = {0x1B, 0 * 21, FONT_TYPE};
outputStream.write(format);
//print title
printUnicode();
//print normal text
outputStream.write(format);
printCustom(message, 0, 0);
//printPhoto(R.drawable.img);
printNewLine();
outputStream.write(format);
printText(" >>>> Thank you <<<< "); // total 32 char in a single line
//resetPrint(); //reset printer
//printUnicode();
printNewLine();
printNewLine();
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
if(btsocket==null){
Intent BTIntent=newintent(getApplicationContext(),DeviceList.class);
此.startActivityForResult(BTIntent,DeviceList.REQUEST\u CONNECT\u BT);
}否则{
OutputStream opstream=null;
试一试{
opstream=btsocket.getOutputStream();
}捕获(IOE异常){
e、 printStackTrace();
}
outputStream=opstream;
//打印命令
试一试{
试一试{
睡眠(1000);
}捕捉(中断异常e){
e、 printStackTrace();
}
outputStream=btsocket.getOutputStream();
字节[]格式={27,33,0};
字节[]printformat={0x1B,0*21,字体类型};
outputStream.write(格式);
//印刷品标题
printUnicode();
//打印普通文本
outputStream.write(格式);
打印自定义(消息,0,0);
//打印照片(R.drawable.img);
printNewLine();
outputStream.write(格式);
printText(“>>>>>谢谢您可以查看这些页面中的ESC/POS命令,对我来说非常有用:
也是我的一段代码,希望有助于:
public class MainActivity extends AppCompatActivity {
Button printButton;
final byte[] ALIGN_CENTER = {0x1b, 0x61, 0x01};
final byte[] ALIGN_LEFT = {0x1b, 0x61, 0x00};
final byte[] ALIGN_RIGHT = {0x1b, 0x61, 0x02};
final byte[] TEXT_SIZE_NORMAL = {0x1b, 0x21, 0x00};
final byte[] TEXT_SIZE_LARGE = {0x1b, 0x21, 0x30};
final byte[] INVERTED_COLOR_ON = {0x1d, 0x42, 0x01};
final byte[] BEEPER = {0x1b,0x42,0x05,0x05};
final byte[] INIT = {0x1b, 0x40};
//final byte[] CUT_PAPER = {0x1d, 0x56, 0x00};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
printButton = findViewById(R.id.main_print_button);
printButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new PrintTask().execute();
}
});
}
private class PrintTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
try {
String text1 = "No setting size text" + "\n\n";
String text2 = "Inverted color text" + "\n\n";
String text3 = "Large size text" + "\n\n\n";
Socket socket = new Socket("192.168.1.241", 9100); //one socket responsible for one device
OutputStream outputStream = socket.getOutputStream();
outputStream.write(text1.getBytes("GBK")); //when printing text, "write()" will print before "println()"
outputStream.write(INVERTED_COLOR_ON);
outputStream.write(text2.getBytes("GBK"));
outputStream.write(new byte[]{0x1D, 0x56, 0x41, 0x10}); //"0x1d, 0x56, 0x41" is for paper cut and "0x10" is for line feed
//outputStream.write(BEEPER); //hardware turn on
outputStream.write(INIT);
outputStream.close();
socket.close();
} catch (UnknownHostException e) {
Log.e("Print()", "UnknownHostException");
} catch (IOException e) {
Log.e("Print()", "IOException");
}
return null;
}
}
public类MainActivity扩展了AppCompatActivity{
按钮打印按钮;
最后一个字节[]ALIGN_CENTER={0x1b,0x61,0x01};
最后一个字节[]ALIGN_LEFT={0x1b,0x61,0x00};
最后一个字节[]ALIGN_RIGHT={0x1b,0x61,0x02};
最终字节[]文本大小正常={0x1b,0x21,0x00};
最后一个字节[]TEXT_SIZE_LARGE={0x1b,0x21,0x30};
最后一个字节[]反转的颜色={0x1d,0x42,0x01};
最后一个字节[]蜂鸣器={0x1b,0x42,0x05,0x05};
最后一个字节[]INIT={0x1b,0x40};
//最后一个字节[]切纸={0x1d,0x56,0x00};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
printButton=findViewById(R.id.main\u print\u按钮);
printButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
新建PrintTask().execute();
}
});
}
私有类PrintTask扩展了AsyncTask{
@凌驾
受保护的空位背景(空位…空位){
试一试{
String text1=“无设置大小文本”+“\n\n”;
String text2=“反转颜色文本”+“\n\n”;
String text3=“大尺寸文本”+“\n\n\n”;
插座插座=新插座(“192.168.1.241”,9100);//一个插座负责一个设备
OutputStream OutputStream=socket.getOutputStream();
outputStream.write(text1.getBytes(“GBK”);//打印文本时,“write()”将在“println()之前打印
outputStream.write(反转颜色);
outputStream.write(text2.getBytes(“GBK”);
outputStream.write(新字节[]{0x1D,0x56,0x41,0x10});/“0x1D,0x56,0x41”用于剪纸,“0x10”用于换行
//outputStream.write(蜂鸣器);//硬件打开
outputStream.write(INIT);
outputStream.close();
socket.close();
}捕获(未知后异常e){
Log.e(“Print()”,“UnknownHostException”);
}捕获(IOE异常){
Log.e(“Print()”,“IOException”);
}
返回null;
}
}
没有使用API的经验,但是printUnicode
实际上可能会编写一个BOM字符U+FEFF,其字节可能构成一个黑白相间的控制命令;请将其注释掉。打印机语言应记录在最初随附的打印机手册中。否则,可能可以从制造商处在线获取然后,您可以在文档中查找打印机语言。