user442058
user442058

Reputation: 11

java function to determine height of an image with the given width?

i'm having an image(some dimensions) i set the width of some pixel position. i need a java method to calculate the height of the image to display properly.

Upvotes: 1

Views: 334

Answers (2)

Rakesh
Rakesh

Reputation: 15047

Are you doing it in BLackberry appln,if so use Display.getWidth() to calculate

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499760

Do you mean you want to maintain the original aspect ratio, you're changing the width and you want to know the height? If so, it should be something like:

int newHeight = (oldHeight * newWidth) / oldWidth;

Performing multiplication then division will avoid some rounding errors, but may fail if the image is huge (i.e. if it overflows). An alternative is to use floating point:

int newHeight = (int) (oldHeight * ((double) newWidth / oldWidth));

Upvotes: 3

Related Questions