public String delPicture(String filePath,int outputWidth,int outputHeight,String outputPath) {
//参数分别代表的意思:源文件,输出高度,输出宽度,输出文件路径
try {
//获得源文件
file = new File(filePath);
if (!file.exists()) {
return "";
}
Image img = ImageIO.read(file);
// 判断图片格式是否正确
if (img.getWidth(null) == -1) {
//System.out.println("无法读取" + "<BR>");
return "no";
} else {
int newWidth; int newHeight;
newWidth = outputWidth; // 输出的图片宽度
newHeight = outputHeight; // 输出的图片高度
BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);
/*
* Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的
* 优先级比速度高 生成的图片质量比较好 但速度慢
*/
tag.getGraphics().drawImage(img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null);
FileOutputStream out = new FileOutputStream(outputPath);
// JPEGImageEncoder可适用于其他图片类型的转换
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
return "ok";
}