开发者

How to calculate new height and width for an image to fit with in predefined height and width, without affecting the image real aspect ratio in java?

I want to scale images to fit with in some predefined size without affecting the actual image's aspect ratio.

Do we have any predefined algorithms for that in java?

U开发者_运维知识库pdate:

Resizing Like this. The output is the same image but in smaller size. The outside frame is just a mark.

How to calculate new height and width for an image to fit with in predefined height and width, without affecting the image real aspect ratio in java?


you're going to have gaps in either the width or the height... the question is figuring out which.

double widthRatio = realWidth / definedWidth;
double heightRatio = realHeight / definedHeight;

if(heightRatio > widthRatio) {
    // scale image to match the height, and let the width have the gaps

} else {
    // scale image to match the width, and let the height have the gaps

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜