How to change image type
Java Image IO provides pluggable architecture for accessing images transparently.
It is simple, flexible and allows us to plugin various image readers and writers with ease.
The following example shows image conversion among various formats.
package com.bethecoder.tutorials.imageio;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
public class ChangeImageTypeTest {
/**
* @param args
* @throws IOException
*/
public static void main ( String [] args ) throws IOException {
URL url = new URL ( "http://www.google.com/logos/2008/stpatricks_08.gif" ) ;
BufferedImage bufImage = ImageIO.read ( url ) ;
//Convert the image to following formats [bmp, jpg, png, gif]
for ( String imageTypeSuffix : new String [] { "bmp" , "jpg" , "png" , "gif" }) {
ImageIO.write (
bufImage, imageTypeSuffix,
new File ( "C:/Temp/downloaded_image." + imageTypeSuffix )) ;
}
}
}
It gives the following output,