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){
try{

BufferedImage bi=ImageIO.read(new 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);
try{
mt.waitForID(0);
}catch(InterruptedException e){}
Graphics2D g2d=bi.createGraphics();
g2d.drawImage(img,0,0,null);

//do something with the buffered image bi
}

1 comment:

  1. The majority rule government in the United States is so unique in relation to the next nation. It appears that president runs the entire nation however it isn't genuine the foundation run the entire nation and get bestessays discounts from us. In this kind of the vote based system, you will never comprehend about the foundation.

    ReplyDelete