简体   繁体   中英

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?

Update:

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

在此处输入图片说明

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM