Patrick Sheppard Patrick is a Java developer at IDRsolutions. He has been passionate about programming for many years (mostly self-taught). He also enjoys skiing, cooking, and learning new things.

How to read GIF files in Java

1 min read

In this blog post I will demonstrate how to read GIF files in Java using either ImageIO or JDeli, our Java image library. Both options return a BufferedImage and can read from several input types.

As is always the case when reading files, there is the issue of catching IOExceptions (or similar), which are easily handled with a try/catch block. However, what is done after catching the error is very much up to the program in question and the programmer writing the code.

Using ImageIO

Reading GIF files in ImageIO is rather simple as it will automatically detect the type of the file and decode it appropriately. The drawback of this method is that it is a bit more difficult to pass raw data to ImageIO.

try {
 BufferedImage image = ImageIO.read(new File(“path/to/file.gif”));
} catch (IOException e) {
 // Deal with read error here.
}

Alternatively you can pass InputStream, ImageInputStream, or URL objects to the read() method.

The ImageIO javadocs can be found here if you need more information.

Using JDeli

The JDeli way of reading GIF files offers more flexibility than ImageIO in the sense that you can input a byte array directly to the decoder for processing.

Both of the following examples use the JDeli class which is found at com.idrsolutions.image.JDeli (full Javadoc available here). These methods can also be used for reading other types of files.

1. Reading from and decoding File objects

try {
 BufferedImage image = JDeli.read(new File(“path/to/file.gif”));
} catch (IOException e) {
 // Deal with read error here.
}

2. Decoding byte arrays

In this method I am using the java non-blocking IO (NIO) library to read bytes from a file; making it equivalent to the previous examples. However, the bytes (raw gif data) can come from any source.

try {
 byte[] bytes = Files.readAllBytes(Paths.get(“path/to/file.gif”));
 BufferedImage image = JDeli.read(bytes);
} catch (IOException e) {
 // Deal with read error here.
}

If you found this guide useful, you may be interested in our series of articles on handling common image file formats in Java.

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (Twitter, Facebook and Google+) or the Blog RSS.

Patrick Sheppard Patrick is a Java developer at IDRsolutions. He has been passionate about programming for many years (mostly self-taught). He also enjoys skiing, cooking, and learning new things.

Hacking the NetBeans IDE – 4. Changing the way…

To help us prepare for our talks for our talks at Oracle CodeOne – “Writing Better Code (Faster) in the Apache NetBeans IDE ” and “Hacking...
Mark Stephens
1 min read

Leave a Reply

Your email address will not be published. Required fields are marked *