[SOLVED] Storing an image in the system as a file
hi all
I have been trying to store an image in the system. The problem I am encountering is that I have to convert an object of Image class into an object of BufferedImage class.
Here is the sample code I have written:
Code:
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
public class mig3{
public static void main(String args[]){
//loading an image
Image one=null;
int iw,ih,i;
int pixels[];
try {
File f = new File("C:/One.jpg");
BufferedImage sl = ImageIO.read(f);
one= (Image)sl;
}catch(Exception en){en.printStackTrace(); System.out.println("Error! "+en);}
//transforming to an array
iw=one.getWidth(null);
ih=one.getWidth(null);
pixels = new int[iw * ih];
try{
PixelGrabber grabber = new PixelGrabber(one, 0, 0, iw, ih, pixels,0,iw);
grabber.grabPixels();
} catch (InterruptedException ie) { }
/*System.out.println("The pixel values are:\n");
for(i=0;i<iw*ih;i++)
System.out.println("\t"+pixels[i]);*/
int [] pix = (int [])pixels;
Canvas c=new Canvas();
Image one1=c.createImage(new MemoryImageSource(iw,ih,pix,0,iw));
//Storing
try{
File fil = new File("C:/One1.jpg");
[COLOR="Red"]//I need the buffered image to use the write method here[/COLOR]
ImageIO.write([COLOR="Red"]/*Buffered Image must come here*/[/COLOR], "jpg", fil);
}catch(Exception en){en.printStackTrace(); System.out.println("Error! "+en);}
}
}
Is there any other method to store the image as a file?
Any help is appreciated!
Thank you.