java中热敏打印机的命令

Ama*_*alo 0 java printing android thermal-printer

我有一套用于我的热敏打印机的命令:这是一个例子

ASCII ESC a n
Decimal 27 97 n
Hexadecimal 1B 61 n

Description Default is 0
0 ? m ? 2 or 48 ? m ? 50
Align left: n=0,48
Align middle: n=1,49
Align right: n=2,50 
Run Code Online (Sandbox Code Playgroud)

我想知道如何使用这个命令?? 我知道我需要像这样写入打印机命令:

   byte [] cmd = new byte[3];
   cmd[0]=??
   cmd[1]???
    mmOutputStream.write(cmd);//out put stream of soccket connected to
    //printer by bluetooth
Run Code Online (Sandbox Code Playgroud)

有关更多说明:我想向我的打印机添加命令以使文本显示在中心

lau*_*une 8

这里,在Hex和plain中,您可以将字符代码序列发送到打印机,以便文本左对齐,居中和右对齐.转义序列只是嵌入在常规文本中.

1b 40 
1b 61 00 This is left-aligned 0a
1b 61 01 This is centered 0a
1b 61 02 This is right-aligned 0a
Run Code Online (Sandbox Code Playgroud)

最初的ESC @重置格式.

我不知道48,49,50会产生什么,但类似的实验应该告诉你.

要编写转义序列,请将其存储在字节数组中并像常规文本一样编写它:

byte[] center = new byte[]{ 0x1b, 0x61, 0x01 };
outputStream.write( center );
outputStream.write( "This is centered\n".getBytes() );
Run Code Online (Sandbox Code Playgroud)

并且您可以在OutputStream周围"包装"PrintWriter,这使得一切都变得简单:

// next stmt according to OP
BufferedWriter bw =new OutputStreamWriter( otputStream, "8859_6" );
PrintWriter pw = new PrintWriter( bw, true );
String center = "\u001b\u0061\u0001";
pw.println( center + "This is centered" );
Run Code Online (Sandbox Code Playgroud)