The “javax.imageio.ImageIO” is a handy class to read and write image into local system. In this example, we show you how to use “ImageIO” to read an image from an URL and write it into three file formats :

  1. “jpg”
  2. “gif”
  3. “png”

Full example to demonstrate the ImageIO usage.

package com.mkyong.image;
 
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
 
public class WriteImage 
{	
    public static void main( String[] args )
    {
    	BufferedImage image = null;
        try {
 
            URL url = new URL("http://www.mkyong.com/image/mypic.jpg");
            image = ImageIO.read(url);
 
            ImageIO.write(image, "jpg",new File("C:\\out.jpg"));
            ImageIO.write(image, "gif",new File("C:\\out.gif"));
            ImageIO.write(image, "png",new File("C:\\out.png"));
 
        } catch (IOException e) {
        	e.printStackTrace();
        }
        System.out.println("Done");
    }
}

Reference

  1. http://java.sun.com/j2se/1.4.2/docs/api/javax/imageio/ImageIO.html
Tags :
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.
My website mkyong.com is hosted by Liquid Web, I really like their fast and helpful support, it saved me a lot of time. Furthermore, their servers are extremely reliable, fast and stable, I don't remember when was the last time we had server downtime. Btw, they're offering $100 Coupon now, if you are sourcing for great hosting provider, go grab it.

Related Posts

Popular Posts