在Java中保持宽高比的同时调整图像大小


问题内容

即时通讯试图在Java的内存中调整bufferdImage的大小,但要保持图像的长宽比,即时通讯有类似这样的内容,但这不好

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()应避免在某些答案中使用的原因。

本文还提供了替代代码。