9

I have a lot of .ico formatted pictures, and I want to use them in my Java SE project, but it doesn't know the format. How can I work around this?

CoffeeRain
  • 4,460
  • 4
  • 31
  • 50
victorio
  • 6,224
  • 24
  • 77
  • 113
  • Java does not seem to natively support ico-format. try this link: http://stackoverflow.com/questions/11090508/how-to-get-favicon-ico-from-a-website-using-java – mindandmedia Jul 09 '12 at 18:16

3 Answers3

7

Try out image4j - Image Library for Java

The image4j library allows you to read and write certain image formats in 100% pure Java.

Currently the following formats are supported:

  • BMP (Microsoft bitmap format - uncompressed; 1, 4, 8, 24 and 32 bit)
  • ICO (Microsoft icon format - 1, 4, 8, 24 and 32 bit [XP uncompressed, Vista compressed])

With the library you can easily decode your ico file

List<BufferedImage> image = ICODecoder.read(new File("input.ico"));
Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143
4

Apache Commons Imaging allows to read and write ICO files:

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

It supports several popular formats of metadata too (EXIF, IPTC and XMP).

TwelveMonkeys ImageIO allows to extend the ImageIO API to support ICO and numerous other image file formats.

gouessej
  • 3,640
  • 3
  • 33
  • 67
  • 1
    I had some .ico files that Apache Commons Imaging couldn't read (and they hadn't been png files). And there were not much releases in the past (according to https://mvnrepository.com/artifact/org.apache.commons/commons-imaging): 1.0-alpha1 in 2019, 1.0-alpha2 in 2020. Up to now (September 2021) none in 2021. –  Sep 19 '21 at 20:22
  • @endofrainbow Personally, I prefer using TwelveMonkeys as it has no dependency and it's actively maintained. Do you know which .ico files that couldn't be read with Apache Commons Imaging? I advise you to fill a bug report if necessary. – gouessej Sep 21 '21 at 10:44
  • Yes, I know the problematic .ico files (and won't publish here because they contain company logos). I will try to read them with TwelveMonkeys and then report. That will take some time because for the moment I'm fine with the manual workaround (a) loading the .ico file in GIMP (b) re-exporting as .ico (c) reading with Apache Commons Imaging. The volume of automatically processed .ico files is small. –  Sep 22 '21 at 18:48
0

Hint for reading ico files with Apache Commons Imaging 1.0-alpha2:

There seems to be a difference in between reading ico files as a file and reading ico files as a byte[]: Imaging.getAllBufferedImages(File) reads an ico file, Imaging.getAllBufferedImages(new ByteArrayInputStream(byte[] icoFileContent, yourIcoFilename) also reads the ico file. Imaging.getAllBufferedImages(byte[]) does not read the same ico file but throws an ImageReadException. See code below.

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

Additionally here is a guide how I created the .ico file that is not readable by Apache Commons Imaging 1.0-alpha2 as byte[] (but is readable as File and is readable as ByteArrayInputStream):

  • Start GIMP (in my case version 2.10.22)
  • Window menu "File" > "New..."
  • Template: [empty]
  • Width: 48px
  • Height: 48px
  • Leave the rest as is (see screenshot below)
  • Draw something (e.g. a blue dot)
  • Window menu "File" > "Export as..."
  • File name: "bluedot.ico"
  • Icon Details: "4 bpp, 1-bit alpha, 16-slot palette"
  • Compressed (PNG): Not checked
  • Click "Export"
  • Imaging.getAllBufferedImages(byte[]) will throw org.apache.commons.imaging.ImageReadException: Can't parse this format.
  • Imaging.getAllBufferedImages(File) will read this file.

Dialog for creating an ico file