Reputation: 6646
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?
Upvotes: 9
Views: 9076
Reputation:
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
):
Imaging.getAllBufferedImages(byte[])
will throw org.apache.commons.imaging.ImageReadException: Can't parse this format.
Imaging.getAllBufferedImages(File)
will read this file.Upvotes: 0
Reputation: 4075
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.
Upvotes: 4
Reputation: 29493
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"));
Upvotes: 8