Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java image resize, maintain aspect ratio

I have an image which I resize:

if((width != null) || (height != null)) {     try{         // scale image on disk         BufferedImage originalImage = ImageIO.read(file);         int type = originalImage.getType() == 0? BufferedImage.TYPE_INT_ARGB                                                : originalImage.getType();          BufferedImage resizeImageJpg = resizeImage(originalImage, type, 200, 200);         ImageIO.write(resizeImageJpg, "jpg", file);          } catch(IOException e) {            System.out.println(e.getMessage());        } } 

This is how I resize the image:

private static BufferedImage resizeImage(BufferedImage originalImage, int type,                                          Integer img_width, Integer img_height) {     BufferedImage resizedImage = new BufferedImage(img_width, img_height, type);     Graphics2D g = resizedImage.createGraphics();     g.drawImage(originalImage, 0, 0, img_width, img_height, null);     g.dispose();      return resizedImage; } 

Now the problem is, I also need to maintain aspect ratio. That is, I need the new 200/200 image to contain the new image scaled. Something like this: enter image description here

I tried some things but they didn't work out as expected. Any help is appreciated. Thanks alot.

like image 282
Fofole Avatar asked Apr 20 '12 11:04

Fofole


People also ask

What is BufferedImage in Java?

A BufferedImage is comprised of a ColorModel and a Raster of image data. The number and types of bands in the SampleModel of the Raster must match the number and types required by the ColorModel to represent its color and alpha components. All BufferedImage objects have an upper left corner coordinate of (0, 0).


2 Answers

Here we go:

Dimension imgSize = new Dimension(500, 100); Dimension boundary = new Dimension(200, 200); 

Function to return the new size depending on the boundary:

public static Dimension getScaledDimension(Dimension imgSize, Dimension boundary) {      int original_width = imgSize.width;     int original_height = imgSize.height;     int bound_width = boundary.width;     int bound_height = boundary.height;     int new_width = original_width;     int new_height = original_height;      // first check if we need to scale width     if (original_width > bound_width) {         //scale width to fit         new_width = bound_width;         //scale height to maintain aspect ratio         new_height = (new_width * original_height) / original_width;     }      // then check if we need to scale even with the new height     if (new_height > bound_height) {         //scale height to fit instead         new_height = bound_height;         //scale width to maintain aspect ratio         new_width = (new_height * original_width) / original_height;     }      return new Dimension(new_width, new_height); } 

In case anyone also needs the image resizing code, here is a decent solution.

If you're unsure about the above solution, there are different ways to achieve the same result.

like image 104
Ozzy Avatar answered Sep 21 '22 23:09

Ozzy


Translated from here:

Dimension getScaledDimension(Dimension imageSize, Dimension boundary) {      double widthRatio = boundary.getWidth() / imageSize.getWidth();     double heightRatio = boundary.getHeight() / imageSize.getHeight();     double ratio = Math.min(widthRatio, heightRatio);      return new Dimension((int) (imageSize.width  * ratio),                          (int) (imageSize.height * ratio)); } 

You can also use imgscalr to resize images while maintaining aspect ratio:

BufferedImage resizeMe = ImageIO.read(new File("orig.jpg")); Dimension newMaxSize = new Dimension(255, 255); BufferedImage resizedImg = Scalr.resize(resizeMe, Method.QUALITY,                                         newMaxSize.width, newMaxSize.height); 
like image 41
Matthias Braun Avatar answered Sep 21 '22 23:09

Matthias Braun