首先发送初始化字节(又名 write(); flush();),而不是一起发送所有数据。然后发送你的角色。
public void print (String text, String codePage, OutputStream os)
{
/*Your codetable initialization here.
*You can refactor this more efficiently.
*Hardcoded just so you can understand.
*/
ByteBuffer init = ByteBuffer.allocate(3);
init.put((byte) 0x1B);
init.put((byte) 0x74);
init.put((byte) 2);
sendData(init.array(), os);
ByteBuffer dataToPrint = ByteBuffer.allocate(text.length());
dataToPrint.put(text.getBytes(codePage));
sendData(dataToPrint.array(), os);
}
private void sendData(byte[] buffer, OutputStream os) throws IOException
{
try {
ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
os.write(byteBuffer.array());
os.flush();
// tell the user data were sent
} catch (Exception e) {
e.printStackTrace();
}
}
您可以将其用作;
print("Лорем ăîîîîîîă", "cp852", yourOutputStream); // cp852 or any other codePage you desire.
如果这不起作用,请在打印前尝试关闭多字节(适合欧洲地区的单字节)。
ByteBuffer closeMultibyte = ByteBuffer.allocate(2);
closeMultibyte.put((byte) 0x1C);
closeMultibyte.put((byte) 0x2E);
sendData(closeMultibyte.array(), os);