简体   繁体   中英

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. 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).

This is the printText function:

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. 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. On BSD/Linux system you could eg use iconv for this purpose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM