有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java在Android应用程序上打印非拉丁字符

我有一个Android应用程序,我正在尝试使用蓝牙打印机打印一些文本。问题是我无法正确打印任何非拉丁字符。 我有这个密码:

 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) {}
}

我使用for来检查编码是否正确,因此我查看了结果值,它们似乎没有问题(我将它们转换回字符串,得到了相同的文本)

这是printText函数:

public void printText(byte[] b) throws IOException {
    synchronized(this) {
        this.write(b);
    }
}

这是我写的:

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);
}

结果如下:

enter image description here

我知道打印机支持这些字符,因为它可以用另一个应用程序正确打印这些字符


共 (1) 个答案

  1. # 1 楼答案

    这些打印机通常在8位字符模式下工作,不能直接理解UTF-8。相反,您必须使用一些ESC/POS命令在打印机中设置正确的代码页,然后在将文本发送到打印机之前将其转换为该代码页。在BSD/Linux系统上,您可以使用iconv来实现此目的