I have an image that I am resizing:
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 that I also need to maintain aspect ratio. That is, I need the new 200/200 image to contain the new scaled image. More or less like this: 
I tried some things, but they did not work as expected. Any help is appreciated. Thank you very much.
java image
Fofole Apr 20 '12 at 11:20 2012-04-20 11:20
source share