gpt4 book ai didi

java - ESC/POS 命令解释

转载 作者:太空宇宙 更新时间:2023-11-04 09:35:23 25 4
gpt4 key购买 nike

我正在构建一个使用蓝牙热敏打印机打印收据的应用程序。我可以使用打印机连接和打印,但我无法弄清楚所有这些 ESC/POS 命令的含义。

打印机将我的文本打印为黑色背景上的白色,而我实际上希望文本为黑色,背景为白色。我不确定如何使用 ESC/POS 命令实现这种类型的格式化。

这是我的打印代码:

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();
}
}

第一行printUnicode();实际上在白色背景上打印了很好的黑色字符,但纸张的其余部分则在黑色背景上打印了白色字符。有没有解释所有 ESC/POS 命令的文档?

最佳答案

您可以检查这些页面中的 ESC/POS 命令,这对我来说非常有用:

https://github.com/escpos/escpos/blob/master/lib/escpos.rb

https://github.com/stefanosbou/esc-pos-java/blob/master/src/main/java/io/github/escposjava/print/Commands.java

<小时/>

还有我的一段代码,希望有帮助:

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;
}
}

关于java - ESC/POS 命令解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56578406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com