ESC / POS命令解释

时间:2019-06-13 10:21:47

标签: java android printing escpos

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

1 个答案:

答案 0 :(得分:2)

您可以在这些页面上查看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;
    }
}