Writing an Image file in Java is very common scenario and in this article we will see a new way to write images into file in Java. javax.imageio.ImageIO is a utility class which provides lots of utility method related to images processing in Java. Most common of them is reading form image file and writing images to file in java. You can write any of .jpg, .png, .bmp or .gif images to file in Java. Just like writing, reading is also seamless with ImageIO and you can read BufferedImage directly from URL.
Reading Images are little different than reading text or binary file in Java as they they are associated with different format. Though you can still use getClass().getResourceAsStream() approach for loading images.
Reading Images are little different than reading text or binary file in Java as they they are associated with different format. Though you can still use getClass().getResourceAsStream() approach for loading images.
Image Read Write Example using ImageIO
Code Example Image Read and Write in Java
Here is complete Code Example of reading and writing Images to file in Java, we will first read the image and than write the same image in different format e.g. JPG, PNG and BMP into disk.
In this code Example of javax.imageio.ImageIO class we will see:
How to read Buffered Image in Java from File or URL
How to write JPG images to file in Java
How to write PNG images to file in Java
How to write BMP images to file in Java
How to write GIF images to file in Java
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
public class ImageIOExample {
public static void main( String[] args ){
BufferedImage image = null;
try {
//you can either use URL or File for reading image using ImageIO
File imagefile = new File("C://Documents and Settings/Javin/My Documents/My Pictures/loan.PNG");
image = ImageIO.read(imagefile);
//ImageIO Image write Example in Java
ImageIO.write(image, "jpg",new File("C:\\home_loan.jpg"));
ImageIO.write(image, "bmp",new File("C:\\credit_card_loan.bmp"));
ImageIO.write(image, "gif",new File("C:\\personal_loan.gif"));
ImageIO.write(image, "png",new File("C:\\auto_loan.png"));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Success");
}
}
Apart from seamless support of reading and writing images in Java, ImageIO class also contains lot of other utility methods for locating ImageReaders and ImageWriters and performing encoding and decoding.
That's all on this Reading and Writing Image File in Java using javax.imageio.ImageIO. Let me know if you face any issue while trying these examples of ImageIO.
Related Java Tutorials
This is a big help. I was having some trouble in correctly reading an image file needed for a Lab. I will be sure to use Javarevisited as a resource.
ReplyDelete@Thanks for your comment. Glad to here that you find Javarevisited blog helpful.
ReplyDeleteIs tif also supported?
ReplyDeleteFile imagefile = new File line indicates ""Illegal start of expression" when I tried the example.
Thanks.
ImageIO class is useful in Java as it can read and write image file. I have written many programs including image editor, image compression,... that use the ImageIO class. You may want to check those programs at www.javatheprogram.blogspot.com.
ReplyDeleteShould you close the file after reading or does this happen automatically? I have seen references to memory leaks if the application does not explicitly close the file. How would one do this in your example?
ReplyDeleteHi,
ReplyDeleteBy using this i am getting
Exception: javax.imageio.IIOException: Unsupported Image Type
Please give the solution.
Hi,
ReplyDeleteBy using this i am getting
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1301)
at Imageread.Imageread.main(Imageread.java:17)
Please give the solution.
pixel value got changed after writing the pixel into the image by using ImageIO.write method..i dont why.can you help me out..
ReplyDeleteif i wish to amke my program such that i would like the user to input the location of file to be converted into another format then there is a problem, the problem being that how can i input //
ReplyDelete