2016-06-07 75 views
0

因此,我在我的網絡應用程序中上傳各種照片。 我想調整圖像的大小,使縱橫比不會改變,並且min(height,width) = 400px將BufferedImage調整爲固定尺寸

任何好的和快速的解決方案? (當照片上傳時,我不刷新頁面,並顯示圖像預覽)

+0

您可能還會發現[此示例](http://stackoverflow.com/questions/14115950/quality-of-image-after-resize-very-low-java/14116752#14116752)(這是其他答案)有用 – MadProgrammer

回答

2

使用getScaledInstance方法,使用內置功能很容易完成。你可以這樣使用它:

if(img.getWidth() > img.getHeight()){ 
    return img.getScaledInstance(400, -1, Image.SCALE_SMOOTH); 
} else{ 
    return img.getScaledInstance(-1, 400, Image.SCALE_SMOOTH); 
} 

這將保持高寬比,並將較大的軸設置爲400像素。如果您更喜歡速度超過質量,您也可以更改縮放算法。

+0

[Image.getScaledInstance的危害()](https://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html) – MadProgrammer