Convert Image File to Bufferedimage

Converting Image file to Buffered in Java


If you want to update the existing image file using java we need to convert the image file to bufferedimage object format. Here the below java code will convert the given input image file to BufferedImage object format. Just look at the below java program and use it.
Note:
change the image_file.gif to your file name.
//source code ImageFileToBufferedImage.java

import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.util.*;
import java.sql.*;
import java.awt.Graphics2D;

public class ImageFileToBufferedImage{
private static final int DEFAULT_IMAGE_TYPE = BufferedImage.TYPE_INT_RGB;
public static void main(String arg[]){
try{ File file = new File("image_file.gif");
Image image = ImageIO.read(file);
BufferedImage cpimg=bufferImage(image,BufferedImage.TYPE_INT_RGB);
Graphics g = cpimg.createGraphics();
//g.setColor(background);
//g.fillRect(0,0,width,height);
Font fnt=new Font("Impact",1,14);
Color fntC = new Color(0,100,0);
g.setColor(fntC);
g.setFont(fnt);
g.drawString("Text tobe displayed on the image",53,15);
File f1 = new File("img"+ ".gif");
ImageIO.write(cpimg, "gif", f1);
}
catch(Exception e){

}
}
public static BufferedImage bufferImage(Image image, int type) {
BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
Graphics2D g = bufferedImage.createGraphics();
g.drawImage(image, null, null);
//waitForImage(bufferedImage);
return bufferedImage;
}
}


Related Topic Java Download Image from URL
Java - Write Text in Image file
A to Z Linux Commands quick reference sheet
PHP Code to Convert JPG Image to RGB Colored ASCII
How to Install CentOS by Bootable USB

nScraps.com 2011   Privacy Policy  Terms of Service  Feedback