Java escpos charset encoding

2020-07-24 05:03发布

问题:

i have to print a raw text printer where i have to se the codepage charset. I found that the best charset for my application is the IBM858. Now the problem is to encode the strings to this codepage.

I tried this:

test = new String("#$@°\\è^ùàòèì\n".getBytes(),"Cp858");
file.write(test.getBytes());

but chars don't match.

回答1:

This encodes the UTF-16 string as Cp858:

file.write("#$@°\\è^ùàòèì\n".getBytes("Cp858"));


回答2:

With ESC/POS printer dealing with international charset you have to :

  • set the code page of the printer : for instance ESC t 40 will select the code page ISO8859-15. i.e. in Java outputStream.write( new byte[]{ 0x1B, 0x74, 40 } );
  • encode the string you want to print with the same code page : outputStream.write(theStringToPrint.getBytes("ISO8859-15"));

You must check with your specific printer the code page available and the number to use in the ESC t n command. The EPSON ESC/POS documentation for the ESC t lists code pages available for EPSON printer.

For EPSON printers, useful documentation is found in the page Character Code Tables for TM printers. Check the Single-byte Characters/Code Pages section.

Don't use the International Character Sets and the ESC r n command : it will replace some ASCII character to be able to print some international character with a ASCII code page, it's outdated.



回答3:

Printing with raw text printer i completely solved with this:

new PrintStream(stream,true,"Cp858")

however with

file.write("#$@°\\è^ùàòèì\n".getBytes("Cp858"));

the printer misses some chars, i don't know if it was a printer or java trouble.