在Java中保持纵横比的同时调整图像大小

问题描述:

我试图在java的内存中调整bufferdImage的大小,但要保持图像的宽高比
im有这样的东西,但这不是很好

im trying to resize bufferdImage in memory in java but to keep the aspect ratio of the image im have something like this but this is not good

int w = picture.getWidth();
int h = picture.getWidth();
int neww=w;
int newh=h;
int wfactor = w;
int hfactor = h;
if(w > DEFULT_PICTURE_WIDTH || h > DEFULT_PICTURE_HIGHT)
{
    while(neww > DEFULT_PICTURE_WIDTH)
    {
        neww = wfactor /2;
        newh = hfactor /2;
        wfactor = neww;
        hfactor = newh;
    }
}

picture = Utils.resizePicture(picture,neww,newh);


你可以看一下 perils-of-image-getscaledinstance.html 解释为什么 getScaledInstance(),应该避免在某些答案中使用。

You may have a look at perils-of-image-getscaledinstance.html that explains why getScaledInstance(), used in some of the answers, should be avoided.

该文章还提供了替代方案代码。

The article also provides alternative code.