java改变图片大小

/**
* 改变图片大小
*
* @param filePath 原图路径
* @param newFilePath 新图保存路径
* @param size 设置的宽度
* @throws IOException
*/
private void changeSize(String filePath, String newFilePath, int size) {
InputStream is = null;
try {
is = new FileInputStream(new File(filePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedImage prevImage = null;
try {
prevImage = ImageIO.read(is);
} catch (IOException e) {
e.printStackTrace();
}
double width = prevImage.getWidth();
double height = prevImage.getHeight();
double percent = size / width;
int newHeight = Integer.parseInt((height * percent + “”).substring(0, (height * percent + “”).indexOf(".")));
System.out.println(“设置的宽为” + size + " " + “高为” + newHeight);
try {
Thumbnails.of(filePath).size(size, newHeight).toFile(newFilePath);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}

	}
}
上一篇:java 学习笔记-IO(六)


下一篇:JDBC 编程