/**
* 根据scale来放大或缩小图像!
*/
public static void scale(String source, String result, int scale,
boolean flag) {try {
BufferedImage src = ImageIO.read(new File(source)); // 读入文件
int width = src.getWidth(); // 得到源图宽
int height = src.getHeight(); // 得到源图长
if (flag) {
// 放大
width = width * scale;
height = height * scale;
} else {
// 缩小
width = width / scale;
height = height / scale;
}
Image image = src.getScaledInstance(width, height,
Image.SCALE_DEFAULT);
//
BufferedImage tag = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 绘制缩小后的图
// // Graphics context no longer needed so dispose it
g.dispose();
ImageIO.write(tag, "JPEG", new File(result));// 输出到文件流} catch (IOException e) {
e.printStackTrace();
}
}/**
* 调整图像到指定尺寸!

* @param source
* @param result
* @param width
* @param height
*/
public static void scale(String source, String result, int width, int height) {try {
BufferedImage src = ImageIO.read(new File(source)); // 读入文件
Image image = src.getScaledInstance(width, height,
Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
//
double Ratio = 0.0;
if ((tag.getHeight() > 120) || (tag.getWidth() > 120)) {
if (tag.getHeight() > tag.getWidth())
Ratio = 120.0 / tag.getHeight();
else
Ratio = 120.0 / tag.getWidth();
}
AffineTransformOp op = new AffineTransformOp(AffineTransform
.getScaleInstance(Ratio, Ratio), null);
tag = op.filter(tag, null);
//
// tag.getGraphics().drawImage(image, 0, 0, null); // 绘制缩小后的图ImageIO.write(tag, "JPEG", new File(result));// 输出到文件流} catch (IOException e) {
e.printStackTrace();
}
}