Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Examples Io Java Write To Image File Example

Related Tutorials


 
 

Share on Google+Share on Google+

Java Write To Image File Example

Advertisement
To Write Image in different format you may use javax.imageio.ImageIO class. To compress the image in different format you need to do the following

Java Write To Image File Example

To Write Image in different format you may use javax.imageio.ImageIO class. To compress the image in different format you need to do the following

At first create an instance of your original Image file as

/* Create new Image File Object */
File origionalImage = new File("myImage.jpg");

Create an ImageBuffer object of Origional Image as

/* Read the image */
BufferedImage bufferedImage = ImageIO.read(origionalImage);

Create an instance of Different Image Type

/* Creating an Instance for different Image types */
File myJpegImage  = new File("myJpegImage.jpg");
File myBmpImage = new File("myBmpImage.bmp");
File myGifImage = new File("myGifImage.gif");
File myPngImage = new File("myPngImage.png");

Finally write the Images using ImageIO's  write() method in different format.

ImageIO.write(bufferedImage, "jpg", myJpegImage);
ImageIO.write(bufferedImage, "bmp", myBmpImage);
ImageIO.write(bufferedImage, "gif", myGifImage);
ImageIO.write(bufferedImage, "png", myPngImage);  

An example of above file is given below

WriteImageFileExample.java

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;

public class WriteImageFileExample {    

    public static void main( String[] args ) throws IOException{      
		
		/* Create new Image File Object */
		File origionalImage = new File("myImage.jpg");

		/* Read the image */
		BufferedImage bufferedImage = ImageIO.read(origionalImage);

		/* Creating an Instance for different Image types */
		File myJpegImage  = new File("myJpegImage.jpg");
		File myBmpImage = new File("myBmpImage.bmp");
		File myGifImage = new File("myGifImage.gif");
		File myPngImage = new File("myPngImage.png");


		/* Writing different images using write() method of ImageIO class that takes three parameters
		   Origional Image Buffer, type of image and new Image file which have to change */
		ImageIO.write(bufferedImage, "jpg", myJpegImage);
		ImageIO.write(bufferedImage, "bmp", myBmpImage);
		ImageIO.write(bufferedImage, "gif", myGifImage);
		ImageIO.write(bufferedImage, "png", myPngImage);       
    }
}

Note:- please put a image named myImage.jpg in the same directory where your class

Download Select Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 18, 2012

Related Tutorials

Discuss: Java Write To Image File Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com