I need a logic to re-size an image so that its always 320X240 or smaller and should maintain aspect ratio.
Asked
Active
Viewed 1,897 times
0
-
possible duplicate of [Resize an image in Java - Any Open Source Library ?](http://stackoverflow.com/questions/244164/resize-an-image-in-java-any-open-source-library) – Greg Hewgill Apr 29 '11 at 21:55
-
What specific part of this are you having trouble with? – Oliver Charlesworth Apr 29 '11 at 21:56
2 Answers
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