代码之家  ›  专栏  ›  技术社区  ›  Frank

在Java中如何检测Unicode图像是否可以在一个字节码中显示?

  •  0
  • Frank  · 技术社区  · 5 年前

    我发现这个问题: detect-when-unicode-character-cannot-be-displayed-correctly

    但是它不是在Java中。

    我有以下代码:

    int codepoint=integer.parseint(unicodetext,16);
    byte ebytes[]=新字符串(character.tochars(codepoint)).getbytes(standardcharset.utf_8);
    string str=新字符串(ebytes,charset.forname(“utf-8”));
    
    jbutton button=新jbutton(str);
    

    它可以在jbutton上显示unicode图像,如下所示:

    因此,如果将unicodetext设置为“1F602”,它可以在按钮上显示图像。

    我的问题是:

    我尝试了Java 8和Java 12,结果是一样的,它们从某些UNIODE中丢失了相同的图像,为什么?可以做的是让缺失的图像出现,似乎Java升级没有做到。 2和gt;我怎样才能从我的Java应用程序中检测出哪些不能显示?

    但它不是Java语言。

    我有以下代码:

    int codePoint=Integer.parseInt(unicodeText,16);
    byte eBytes[]=new String(Character.toChars(codePoint)).getBytes(StandardCharsets.UTF_8);
    String str=new String(eBytes,Charset.forName("UTF-8"));
    
    JButton button=new JButton(str);
    

    它可以像这样在jbutton上显示Unicode图像:

    enter image description here

    因此,如果将unicodetext设置为“1F602”,它可以在按钮上显示图像。

    我的问题是:

    <我尝试了Java 8和Java 12,结果是一样的,它们从某些UNIODE中丢失了相同的图像,为什么?可以做的是让失踪的图像出现,似乎Java升级没有做到这一点。

    <2 &我怎样才能从我的Java应用程序中检测到UnDoice不能被显示?

    1 回复  |  直到 5 年前
        1
  •  1
  •   Olivier Grégoire    5 年前

    Font::canDisplayUpTo

    Font

    JButton button = new JButton(str);
    Font font = button.getFont();
    int failingIndex = font.canDisplayUpTo(str);
    if (failingIndex >= 0) {
      // failingIndex points to the first codepoint in your string that cannot be represented with the font.
    } else {
      // This string can be displayed with the given font.
    }