在蓝牙打印机上打印UTF-8字符

时间:2013-08-14 12:06:43

标签: java android bluetooth zebra-printers

我有一个应用程序,我应该可以在蓝牙打印机Zebra iMZ320上打印,但我有一些UTF-8特定字符(Æ,Ø或Å)的问题。

我按如下方式连接到设备:

        BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(macAddr);
        Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { Integer.TYPE });
        bSocket = (BluetoothSocket)m.invoke(device, new Object[] { Integer.valueOf(1) });
        bSocket.connect();
        outStream = bSocket.getOutputStream();
        inStream = bSocket.getInputStream();

套接字打开后,我将在CPCL中发送数据:

        String cpclData = "! U1 SETLP 5 2 24 \r\n"+text+"\r\n";
        outStream.write(cpclData.getBytes());
        outStream.flush();

但是当我试图打印上面提到的字符时,它会写一些异常字符。

我联系了Zebra,他们的一位工程师写道,我应该尝试以下方法:

! 0 200 200 80 1 
IN-MILLIMETERS 
JOURNAL 
CENTER 
COUNTRY NORWAY
TEXT 4 0 0 8 COUNTRY IS NORWAY OR DENMARK
TEXT 4 0 0 15 Æ Ø Å
PRINT

但它绝对没有。

5 个答案:

答案 0 :(得分:2)

如果您尝试从Android设备打印标签,这很简单;当您使用“ISO-8859-1”编码编写数据时,请查看:

String cpclData = "! U1 SETLP 5 2 24 \r\n"+text+"\r\n";
outStream.write(EncodingUtils.getBytes(cpclData, "ISO-8859-1"));
outStream.flush();

答案 1 :(得分:1)

CPCL语言不支持Unicode。你可以在ZPL中完成,iMZ支持ZPL。看看这个link

答案 2 :(得分:0)

找到此解决方案here

public byte[] convertExtendedAscii(String input)
{
        int length = input.length();
    byte[] retVal = newbyte[length];

    for(int i=0; i<length; i++)
    {
              char c = input.charAt(i);

              if (c < 127)
              {
                      retVal[i] = (byte)c;
              }
              else
              {
                      retVal[i] = (byte)(c - 256);
              }
    }

    return retVal;
}

答案 3 :(得分:0)

在类似的问题中,尝试通过蓝牙在Zebra MZ220打印机上打印西班牙语特殊字符,最后我做了以下操作(在这个答案中我添加了字符Å,Æ,Ø,å,æ,ø):

  1. 定义一段代码,将目标字符串转换为所需的字节数组:

    public class Util
    {
        public final static String  caracteresEspeciales        = "ÜüÁáÉéÍíÓóÚúÑñÅÆØåæø";
    
        public final static byte[]  codigoCaracteresEspeciales  = new byte[] {(byte) 0xDC, (byte) 0xFC, (byte) 0xC1, (byte) 0xE1, (byte) 0xC9, (byte) 0xE9,
                (byte) 0xCD, (byte) 0xED, (byte) 0xD3, (byte) 0xF3, (byte) 0xDA, (byte) 0xFA, (byte) 0xD1, (byte) 0xF1, (byte) 0xC5, (byte) 0xC6, (byte) 0xD8,
                (byte) 0xE5, (byte) 0xE6, (byte) 0xF8           };
    
        public static byte[] stringABytes(String s)
        {
            int i, l, i_especial;
            byte b;
            byte[] b_arr;
            String s_sub;
    
            if(s == null)
                return null;
            if((l= s.length()) < 1)
                return new byte[0];
    
            // convertimos a byte carácter por carácter
            b_arr= new byte[l];
            for(i= 0; i < l; i++)
            {
                s_sub= s.substring(i, i + 1);
                i_especial= Util.caracteresEspeciales.indexOf(s_sub);
                if(i_especial < 0)
                    b= (s_sub.getBytes())[0];
                else
                    b= Util.codigoCaracteresEspeciales[i_especial];
                b_arr[i]= b;
            }
    
            return b_arr;
        }
    }
    
  2. 我们可以从PROMAN-CPCL文档中获取这些十六进制代码,这些代码随打印机一起提供(附录C-CHARACTER TABLES,拉丁语1字符集表)。

    1. 转换字符串并发送。

              String datos_cplc;
              byte[] b_arr;
              ...
              datos_cplc= "! 0 200 200 48 1\r\n" + 
                      "TEXT 7 0 0 0 12345678901234567890123456789012\r\n" + 
                      "TEXT 7 0 0 25 ÜüÁáÉéÍíÓóÚúÑñÅÆØåæø AEIOUaeiou1\r\n" + 
                      "FORM\r\n" + 
                      "PRINT\r\n";
              b_arr= Util.stringABytes(datos_cplc);
              ...
              connection.write(b_arr);
      
    2. 结果:

    3. enter image description here

答案 4 :(得分:0)

就我而言,它完美地使用了@jsanmarb提供的解决方案,但使用的代码页为850(拉丁语1-西欧语言),位于以下网址:https://www.ascii-codes.com/cp850.html

相关问题