简体   繁体   中英

Image size calculation by keeping aspect ratio with different screen resolutions

I am trying to resize an image by keeping the aspect ratio. It should be just large enough to fill the screen with no blank space and if necessary some of the image should be off-screen.

The image below shows how the yellow image should be sized based on the black screen size.

在此输入图像描述

Heres the Code that I am actually using, is there any better way to do this?

if(bwidth > bheight) {
    if(bwidth > swidth && bheight > sheight) {
        new_height = sheight;
        new_width = (int) ((double) (bwidth/100)*(sheight/((double) (bheight)/100)));

    } else if(bwidth > swidth && bheight < sheight) {
        new_height = sheight;
        new_width = (int) ((double) (bwidth/100)*(sheight/((double) (bheight)/100)));

    } else if(bwidth < swidth && bheight < sheight) {
        new_height = sheight;
        new_width = (int) ((double) (bwidth/100)*(sheight/((double) (bheight)/100)));

    } else if(bwidth < swidth && bheight > sheight) {
        new_height = sheight;
        new_width = (int) ((double) (bwidth/100)*(sheight/((double) (bheight)/100)));

    } else if(bwidth >= swidth && bheight >= sheight) {
        new_width = swidth;
        new_height = (int) ((double) (bheight/100)*(swidth/((double) (bwidth)/100)));
    }

} else if(bwidth < bheight) {
    if(bwidth > swidth && bheight > sheight) {
        new_width = swidth;
        new_height = (int) ((double) (bheight/100)*(swidth/((double) (bwidth)/100)));

    } else if(bwidth < swidth && bheight > sheight) {        
        new_width = swidth;
        new_height = (int) ((double) (bheight/100)*(swidth/((double) (bwidth)/100)));

    } else if(bwidth < swidth && bheight < sheight) {
        new_width = swidth;
        new_height = (int) ((double) (bheight/100)*(swidth/((double) (bwidth)/100)));

    } else if(bwidth < swidth && bheight < sheight) {
        new_width = swidth;
        new_height = (int) ((double) (bheight/100)*(swidth/((double) (bwidth)/100)));

    } else if(bwidth >= swidth && bheight >= sheight) {
        new_width = swidth;
        new_height = (int) ((double) (bheight/100)*(swidth/((double) (bwidth)/100)));

    }
}
  • swidth = screen width
  • sheight = screen height
  • bwidth = image width
  • bheight = image height

Compare ratios.

If the width to height ratio of the image is more than the width to height ratio of the screen, then you know you'll be using the screen width and calculating the height. Otherwise you'll be using the screen height and calculating the width. Just make sure none of the heights are zero!

Note that the code here will resize the image so that it will always fill the screen. This effectively crops off any additional part of the image. To make the image as large as possible while being entirely visible, change the < to a > in the first line.

if (bwidth / bheight < swidth / sheight) {
    new_width = swidth;
    new_height = (int) Math.floor((double) bheight
                                  * (double) swidth / (double) bwidth);
} else {
    new_height = sheight;
    new_width = (int) Math.floor((double) bwidth
                                 * (double) sheight / (double) bheight);
}

I also made a couple more improvements:

  • Simplified the equations. Dividing a numerator and denominator both by 100 doesn't do anything.
  • Simplified the typecasting. I don't know the type of each variable, but they all need to be doubles.
  • Used Math.floor instead of just a typecast to int to make sure it doesn't round up.

Well thank you @Erick Robertson Changed a litte bit, but now it works!

Here is the changed code:

if (bwidth / swidth <  bheight / sheight) {
    new_width = swidth;
    new_height = (int) Math.floor((double) bheight 
                                  * (double) swidth / (double) bwidth);
} else {
    new_height = sheight;
    new_width = (int) Math.floor((double) bwidth 
                                 * (double) sheight / (double) bheight);
}

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