Breaking News
Home / Java / Java Snippets / Recursively Convert GIF Images to JPEG

Recursively Convert GIF Images to JPEG

This snippets shows how to re-cursively convert image in gif format to jpeg.

package com.livrona.image.tools;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Date;

import javax.imageio.ImageIO;

/**
 * @author mvohra
 *
 */
public class ImageConverter
{

    public static void main(String args[])
    {
        // root directory where gif images are stored
        String rootDir = "c:gif-image-dir";
        File dir = new File(rootDir);

        // It is also possible to filter the list of returned files.
        // This example does not return any files that start with `.'.
        FilenameFilter filter = new FilenameFilter()
        {
            public boolean accept(File dir, String name)
            {
                return name.endsWith("gif");
            }
        };
        String[] children = dir.list(filter);

        for (int i = 0; i < children.length; i++)
        {
            // Get filename of file or directory
            String filename = children[i];
            log("Converting  " + filename);

            File myGifFile = new File(rootDir + "" + filename);
            File outFile = new File(rootDir + "" + filename.replace("gif", "jpg"));
            try
            {
                // Using Image IO
                BufferedImage bufi = ImageIO.read(myGifFile);
                ImageIO.write(bufi, "JPEG", outFile);
            }
            catch (Exception e)
            {
                log(e.getMessage());
            }
        }

    }

    public static void log(String msg)
    {
        System.out.println(new Date() + " - " + msg);
    }
}

 [pb_builder]

Check Also

How to progamatically get the Java version?

Snippet The key here is to use the system property java.version and get minor and …

Leave a Reply