首页 > 解决方案 > ESC/POS 命令解释

问题描述

我正在构建一个使用蓝牙热敏打印机打印收据的应用程序。我能够使用打印机连接和打印,但我无法弄清楚所有这些 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 命令的文件吗?

标签: javaandroidprintingescpos

解决方案


您可以检查这些页面是否有 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;
    }
}

推荐阅读