Saturday, July 20, 2013

Image to BufferedImage

In general, image manipulation is performed on the BufferedImage object. So it is useful to convert from image object to buffered image object. In Java, when you have an image object, there are two ways to convert it to the buffered image object.

One way is using the read(File file) method of the ImageIO class. See the example code below:
public void ImageToBufferedImage(String filename){

BufferedImage File(filename));
//do something with the buffered image bi

}catch(IOException ie){}

Alternatively, you can create a blank buffered image object then draw the image on this buffered image object. See the example code below:

public void createImage(String imgfile){

BufferedImage bi=new BufferedImage(500,300, BufferedImage.TYPE_INT_ARGB);
Image img=Toolkit.getDefaultToolkit().getImage("d:/earth.png");
MediaTracker mt=new MediaTracker(this);
mt.addImage(img, 0);
}catch(InterruptedException e){}
Graphics2D g2d=bi.createGraphics();

//do something with the buffered image bi

No comments:

Post a Comment