日期:2014-05-17 浏览次数:20725 次
/**
* 根据宽度来计算新图形,按比例缩放
*
* @param srcImageFile
* @param result
* @param width
*/
public static void scaleByWidth(File srcImageFile, File result, int width) {
try {
BufferedImage src = ImageIO.read(srcImageFile); // 读入文件
int sWidth = src.getWidth(); // 得到源图宽
int sHeight = src.getHeight(); // 得到源图长
int height = (int) ((float) width / (float) sWidth * (float) sHeight);
Image image = src.getScaledInstance(width, height,
Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 绘制缩放后的图
g.dispose();
ImageIO.write(tag, "JPEG", result);// 输出到文件流
} catch (IOException e) {
e.printStackTrace();
}
}
/** */
/**
* @param args
*/
public static void main(String[] args) {
scaleByWidth(new File("c:\\180.jpg"), new File("C:\\50.jpg"), 50);
}