In this article, I will show you how to read GIF files in Java. We also have a related article covering how to write GIF files in Java.
GIF files can be read directly by Java’s own ImageIO class and I will also show you how to read them in JDeli.
What is a GIF?
GIF stands for Graphics Interchange Format. It is a lossless, bitmap image format which became popular on the world wide web because it supports transparency and simple animations in browsers. It can support up to 256 different colours from a 24bit range of RGB values. It uses LZW compression (which was subject to patents owned by Unisys). Issues over this in 1990s, led it to be largely replaced by the GIF format in modern usage.
The file name extension for GIF files is:
How to read a GIF image in Java with ImageIO
Step 1 Create a File handle, InputStream, or URL pointing to the raw GIF image.
Step 2 ImageIO will now be able to read a GIF file into a BufferedImage. This syntax is like so:
BufferedImage image = ImageIO.read(gifFileOrInputStreamOrURL)
How to read a GIF image in Java with JDeli
Step 1 Add JDeli to your class or module path. (download the trial jar).
Step 2 Create a File handle or InputStream pointing to the raw GIF image. You can also use a byte containing the image data.
Step 3 Read the GIF image into a BufferedImage
BufferedImage image = JDeli.read(gifFile);
Start reading and writing images with one line of code
BufferedImage image = JDeli.read(streamOrFile);
JDeli.write(myBufferedImage, OutputFormat.HEIC, outputStreamOrFile)