0

I need a logic to re-size an image so that its always 320X240 or smaller and should maintain aspect ratio.

Pit Digger
  • 9,618
  • 23
  • 78
  • 122

2 Answers2

0

Have a look at ImgScalr, library for java. Has several quality and speed settings for conversion as well as automatically scaling it for you.

BufferedImage sourceImage = ImageIO.read(new File(sourceFile));
BufferedImage destImage = Scalr.resize(sourceImage, Scalr.Method.ULTRA_QUALITY, 320, 240, Scalr.OP_ANTIALIAS);
dutchman191
  • 273
  • 1
  • 3
  • 7
0
1) determine which side of the image is the larger one
2) if its width, calc: faktor = 320/width, else faktor = 240/height;
3) downscale the image by size.x *faktor, size.y *faktor.

example:

Given: image has 1024x640

1) its wider than high.
2) faktor = 320 / 1024 -> 0.3125
3) 1024 * 0.3125 = 320 (huh!)     
   640 * 0.3125 = 200.
thedanielhanke
  • 730
  • 1
  • 6
  • 22