我有一个要调整大小的图像:
if((width != null) || (height != null))
{
try{
// scale image on disk
BufferedImage originalImage = ImageIO.read(file);
int type = originalImage.getType() == 0? BufferedImage.TYPE_INT_ARGB
: originalImage.getType();
BufferedImage resizeImageJpg = resizeImage(originalImage, type, 200, 200);
ImageIO.write(resizeImageJpg, "jpg", file);
} catch(IOException e) {
System.out.println(e.getMessage());
}
}
这是我调整图像大小的方法:
private static BufferedImage resizeImage(BufferedImage originalImage, int type,
Integer img_width, Integer img_height)
{
BufferedImage resizedImage = new BufferedImage(img_width, img_height, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, img_width, img_height, null);
g.dispose();
return resizedImage;
}
现在的问题是,我还需要保持宽高比。也就是说,我需要新的200/200图像以包含缩放后的新图像。像这样:
我尝试了一些方法,但未按预期进行。任何帮助表示赞赏。非常感谢。