【问题标题】:How to read an ico format picture in java?如何在java中读取ico格式的图片?
【发布时间】:2012-07-09 04:26:54
【问题描述】:

我有很多 .ico 格式的图片,我想在我的 Java SE 项目中使用它们,但它不知道格式。我该如何解决这个问题?

【问题讨论】:

标签: java image ico


【解决方案1】:

试试image4j - Image Library for Java

image4j 库允许您读取和写入某些图像格式 在 100% 纯 Java 中。

目前支持以下格式:

  • BMP(Microsoft 位图格式 - 未压缩;1、4、8、24 和 32 位)
  • ICO(Microsoft 图标格式 - 1、4、8、24 和 32 位 [XP 未压缩, Vista 压缩])

使用该库,您可以轻松解码您的 ico 文件

List<BufferedImage> image = ICODecoder.read(new File("input.ico"));

【讨论】:

    【解决方案2】:

    Apache Commons Imaging允许读写ICO文件:

        List<BufferedImage> images = Imaging.getAllBufferedImages(new File("input.ico"));
    

    它还支持多种流行的元数据格式(EXIF、IPTC 和 XMP)。

    TwelveMonkeys ImageIO 允许扩展 ImageIO API 以支持 ICO 和许多其他图像文件格式。

    【讨论】:

    • 我有一些 Apache Commons Imaging 无法读取的 .ico 文件(而且它们不是 png 文件)。而且过去没有多少版本(根据mvnrepository.com/artifact/org.apache.commons/commons-imaging):2019 年 1.0-alpha1,2020 年 1.0-alpha2。截至目前(2021 年 9 月),2021 年没有。
    • @endofrainbow 就我个人而言,我更喜欢使用 TwelveMonkeys,因为它没有依赖性并且得到积极维护。您知道 Apache Commons Imaging 无法读取哪些 .ico 文件吗?如有必要,我建议您填写错误报告。
    • 是的,我知道有问题的 .ico 文件(并且不会在此处发布,因为它们包含公司徽标)。我会尝试用 TwelveMonkeys 阅读它们,然后报告。这将需要一些时间,因为目前我对手动解决方法很好(a)在 GIMP 中加载 .ico 文件(b)重新导出为 .ico(c)使用 Apache Commons Imaging 读取。自动处理的 .ico 文件量很小。
    【解决方案3】:

    使用 Apache Commons Imaging 1.0-alpha2 读取 ico 文件的提示:

    将 ico 文件作为文件读取和将 ico 文件作为字节读取似乎是有区别的:Imaging.getAllBufferedImages(File) 读取 ico 文件,Imaging.getAllBufferedImages(new ByteArrayInputStream(byte[] icoFileContent, yourIcoFilename) 也读取 ico 文件。 Imaging.getAllBufferedImages(byte[]) 不会读取相同的 ico 文件,但会抛出 ImageReadException。请参阅下面的代码。

        File icoFile = new File("bluedot.ico");
    
        // Works fine
        List<BufferedImage> images = Imaging.getAllBufferedImages(icoFile);
        Assert.assertFalse(images.isEmpty());
        ImageIO.write(images.get(0), "png", new File("bluedot.png"));
    
        // Also works fine
        byte[] icoFileContent = Files.readAllBytes(icoFile.toPath());
        images = Imaging.getAllBufferedImages(new ByteArrayInputStream(icoFileContent), "bluedot.ico");
        Assert.assertFalse(images.isEmpty());
        ImageIO.write(images.get(0), "png", new File("bluedot2.png"));
    
        // Throws an exception
        images = Imaging.getAllBufferedImages(icoFileContent);
    

    另外,这里是我如何创建 .ico 文件的指南,该文件不能被 Apache Commons Imaging 1.0-alpha2 读取为byte[](但可以读取为File,并且可以读取为ByteArrayInputStream):

    • 启动 GIMP(在我的例子中是 2.10.22 版)
    • 窗口菜单“文件”>“新建...”
    • 模板:[空]
    • 宽度:48px
    • 高度:48px
    • 其余部分保持原样(见下面的屏幕截图)
    • 画一些东西(例如蓝点)
    • 窗口菜单“文件”>“导出为...”
    • 文件名:“bluedot.ico”
    • 图标详细信息:“4 bpp,1 位 alpha,16 槽调色板”
    • 压缩 (PNG):未选中
    • 点击“导出”
    • Imaging.getAllBufferedImages(byte[]) 将抛出 org.apache.commons.imaging.ImageReadException: Can't parse this format.
    • Imaging.getAllBufferedImages(File) 将读取此文件。

    【讨论】:

      猜你喜欢
      • 2015-07-28
      • 2010-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多