Image srcImage = ImageIO.read(srcFile);
// 原始图片大小
int srcImageWidth = srcImage.getWidth(null);
int srcImageHeight = srcImage.getHeight(null);
// 计算图片缩放后的大小
if (srcImageWidth >= srcImageHeight) {
newHeight = (int) Math.round((srcImageHeight * newWidth * 1.0 / srcImageWidth));
} else {
newWidth = (int) Math.round((srcImageWidth * newHeight * 1.0 / srcImageHeight));
}
BufferedImage distImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
// 缩放图片
distImage.getGraphics().drawImage(srcImage.getScaledInstance(newWidth, newWidth, Image.SCALE_SMOOTH), 0, 0, null);
// 输出
OutputStream os = null;
try {
os = new FileOutputStream(destFile);
ImageIO.write(distImage, "JPG", os);
os.flush();
} finally {
os.close();
}
JAVA用ImageIO处理JPG图片
最新推荐文章于 2023-12-20 09:04:28 发布