[英]Java - printing non-latin characters on Android app
I have an Android app and I'm trying to use a bluetooth printer to print some text. 我有一个Android应用程序,正在尝试使用蓝牙打印机打印一些文本。 The problem is that I cannot print correctly any non-latin character.
问题是我无法正确打印任何非拉丁字符。 I have this code:
我有以下代码:
public void printTaggedText() throws IOException {
try {
byte[] theText = "Întregul text în românește țș".getBytes("utf8 ");
for (byte bit : theText) {
System.out.println("Reached: " + Integer.toHexString(bit));
}
this.printText(theText);
} catch (Exception e) {}
}
I've put the for to check if the encoding is correct, therefore I looked at the resulting values and they seem to be ok (I converted them back to String and I got the same text). 我已将for用作检查编码是否正确,因此我查看了结果值,它们似乎没问题(我将它们转换回String并得到了相同的文本)。
This is the printText function: 这是printText函数:
public void printText(byte[] b) throws IOException {
synchronized(this) {
this.write(b);
}
}
And this is write: 这是这样写的:
public synchronized void write(int b) throws IOException {
this.write(new byte[]{(byte)b});
}
public synchronized void write(byte[] b) throws IOException {
this.write(b, 0, b.length);
}
public synchronized void write(byte[] b, int offset, int length) throws IOException {
this.mBaseOutputStream.write(b, offset, length);
}
The result looks like this: 结果看起来像这样:
I know that the printer supports these characters because it prints them correctly with another app. 我知道打印机支持这些字符,因为它可以使用另一个应用程序正确打印它们。
These printers usually work in 8-bit character mode and do not understand UTF-8 directly. 这些打印机通常以8位字符模式工作,不能直接理解UTF-8。 Rather, you have to set the correct codepage in the printer using some ESC/POS command, and then convert your text to that codepage before sending it to the printer.
相反,您必须使用某些ESC / POS命令在打印机中设置正确的代码页,然后将文本转换为该代码页,然后再将其发送到打印机。 On BSD/Linux system you could eg use iconv for this purpose.
在BSD / Linux系统上,您可以例如为此使用iconv。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.