How to write an image to file – Java ImageIO
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 :
- “jpg”
- “gif”
- “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");
}
}
Nice tutorial. It helped me a lot.
is there any other alternative without using ImageIO.write
It is not working for the url – https://lh5.ggpht.com/FJsccpuphUKRxZTFzTiXXgTO4Vqi8IRmW6oRLmzZpY90UVmi1EwC9Y9fyYidPnPCxE0=w300-rw
Please let me know if any fix.
Hi i am not able write the pixel values to rgb image properly.. after writing the pixel value to image by using ImageIO.write..the pixel value have changed while reading it..
I am not able read the image from ur; “http://www.hdwallsource.com/img/2014/7/harley-davidson-wallpaper-16885-17439-hd-wallpapers.jpg”
Thanks for sharing this nifty code :DD
Thanks,
but it is not woking for me. please help me, i am getting error shown in below.
javax.imageio.IIOException: Can’t get input stream from URL!
at javax.imageio.ImageIO.read(Unknown Source)
at Image.main(Image.java:17)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
thanks for your understand
Hi,
How do you incorporate scribling of text onto an image in java…..
I have a java project in which i have to scribble text using a writing pad onto an existing image in a label…
please help me on the above case……
After converting the image to 0101 and encrypted, I can not bring it back as an image please help
I am trying to upload a file path through action class to the server from local system.I have done it statically if(customerBean.getUploadFileFileName() != null) { String filePath = “E:\\photos\\customer\\”; String photoPath = “E:/photos/customer/”+customerBean.getUploadFileFileName(); File fileToCreate = new File(filePath, customerBean.getUploadFileFileName()); if(!fileToCreate.exists()) { setErrormessage(“Please check if Uploaded Photo exists in the Customer Folder”); return “failure”; } status = custDAO.insertUser(customerBean,photoPath); } Could you please help me out with a working code where i can upload images dynamically on the server and store the image path in server datatbase. Thanks, Amit.
Respected sir,
iam doing project on visual cryptography
i.e: based on two images generate another image. here one image is orinal image with text. another is indecipheralble form. generate another image is also indecipherable form.both are combine whatever the text in original image. that will be visible. plz help me sir. plz
based on two images generate another image here one image is orinal image with text another is indecipheralble form generate another image is also indecipherable form. plz help me sir
sir plz help me iam doing project on visual cryptography.i want code in java
When i user an image *.png and try to get the *.bmp the file generated is a 0 bytes file.
May be you could help me to know why is this happening??
Thanks
Above code works on my machine , thanks for sharing
Exception in thread “main” java.lang.IllegalArgumentException: im == null!
Below is the code :
Hi,
For Java its working, while I try to put the code for a managed bean in JSF in NetBean IDE the file is created under build\web directory instead of web. Can you please help on this
FacesContext con = FacesContext.getCurrentInstance();
ExternalContext contx = con.getExternalContext();
String path = contx.getRealPath(“/”);
URL url = new URL(“http://www.mkyong.com/image/mypic.jpg”);
image = ImageIO.read(url);
ImageIO.write(image, “jpg”,new File(path +”out.jpg”));
Thanks