Image Processing


import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.math.BigDecimal;

import javax.imageio.ImageIO;

public class ImageTest01 {

public static void main(String[] args) {

	BufferedImage img = loadImage(”C:\\_Temp\\duke-real.gif”);
	float w = img.getWidth();
	float h = img.getHeight();

	System.out.println(”w = ” + w + “, h =” + h);

	int newH = 50;
	float scalingFactor = newH/h;
	//int newW = (new BigDecimal(w*scalingFactor)).round
	int newW = Math.round(w*scalingFactor);
	BufferedImage dimg = new BufferedImage(newW, newH, img.getType());
	//ImageIO.read(new File(file));
	Graphics2D g = dimg.createGraphics();
	g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
	g.drawImage(img, 0, 0, newW, newH, 0, 0, Math.round(w), Math.round(h), null);
	g.dispose();

	System.out.println(”newW = ” + newW + “, newH =” + newH);

	saveImage(dimg, “C:\\_Temp\\duke-new.gif”);

	System.out.println(”Done”);

}

public static BufferedImage loadImage(String file) {
	BufferedImage bimg = null;
	try {
		bimg = ImageIO.read(new File(file));
	} catch (Exception e) {
		e.printStackTrace();
	}
	return bimg;
}

public static void saveImage(BufferedImage img, String file) {
	try {
		File imageFile = new File(file);
		String format = imageFile.getName().split(”\\.”)[1];
		ImageIO.write(img, format, imageFile);
	} catch (Exception e) {
		e.printStackTrace();
	}
}	

}

You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

AddThis Social Bookmark Button

Similar Posts

Comments are closed.