import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* @author WENLIANG
*
* 该类主要用于将图片重绘
*/
public class RedrawImage {
public RedrawImage() {}
/**
* @param redrawImage 要重绘的图片
* @param redrawWidth 重绘图片的宽度
* @param redrawHeight 重绘图片的高度
*/
public static void Redraw(String redrawImage,int redrawWidth, int redrawHeight) {
File file = new File(redrawImage);
if (!file.exists()) {
return;
}else {
Redraw(file,redrawImage,redrawWidth,redrawHeight);
}
}
/**
* @param sourceImage 源图片
* @param redrawImage 重绘后的图片
* @param redrawWidth 重绘图片的宽度
* @param redrawHeight 重绘图片的高度
*/
public static void Redraw(String sourceImage, String redrawImage,int redrawWidth, int redrawHeight) {
File file = new File(sourceImage);
if (!file.exists()) {
return;
}else {
Redraw(file,redrawImage,redrawWidth,redrawHeight);
}
}
/**
*
* @param file 要重绘的图片
* @param redrawImage 重绘后的图片
* @param redrawWidth 重绘图片的宽度
* @param redrawHeight 重绘图片的高度
*/
private static void Redraw(File file,String redrawImage,int redrawWidth, int redrawHeight) {
try {
Image image =ImageIO.read(file);
BufferedImage bufImage = new BufferedImage((int) redrawWidth,(int) redrawHeight, BufferedImage.TYPE_INT_RGB);
/*// 选择图像平滑度比缩放速度具有更高优先级的图像缩放算法。
bufImage.getGraphics().drawImage(image.getScaledInstance(redrawWidth,redrawHeight, Image.SCALE_SMOOTH), 0, 0, null);*/
/*//使用 Area Averaging 图像缩放算法。
bufImage.getGraphics().drawImage(image.getScaledInstance(redrawWidth,redrawHeight, Image.SCALE_AREA_AVERAGING), 0, 0, null);*/
// 选择一种图像缩放算法,在这种缩放算法中,缩放速度比缩放平滑度具有更高的优先级。
bufImage.getGraphics().drawImage(image.getScaledInstance(redrawWidth, redrawHeight,Image.SCALE_FAST), 0, 0, null);
FileOutputStream out = new FileOutputStream(redrawImage);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(bufImage);
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
注:本文转自 http://blog.csdn.net/love_java_love/archive/2009/11/20/4842038.aspx