Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Crop an image in Java

 
Amit Shanker
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I crop a JPEG image in JAVA maintaining the aspect ratio of the image. Help will be appreciated.
Thanks,
Amit.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I think maybe you might be using the wrong word. When you crop an image, typically, you are cropping to a specific bound region. Therefor, keeping the aspect ratio is nonrelivent.
However, if you are speaking of simply resizing the image, then keeping the aspect ratio is relivent.
Let's clear this up first, then we can get to the nitty gritty.
 
Amit Shanker
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gregg,
You are right. I have not worded my question correctly. Actually, I want to crop an image to get the aspect ratio I want.
If the original aspect ratio of image is 2 ( 200 x 100 pixels image size), I may want to crop the image to get the aspect ratio of 1 (100 x 100 pixels image size) reducing 50 pixels on either sides.
Hope I'm clear this time.
Thanks,
Amit.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amit Shanker:
Hi Gregg,
You are right. I have not worded my question correctly. Actually, I want to crop an image to get the aspect ratio I want.
If the original aspect ratio of image is 2 ( 200 x 100 pixels image size), I may want to crop the image to get the aspect ratio of 1 (100 x 100 pixels image size) reducing 50 pixels on either sides.
Hope I'm clear this time.
Thanks,
Amit.

Interesting, because going from a 200px x 100px to a 100px x 100px is not keeping the aspect ratio. You are turning a rectangle into a square. If your image has a larger width than height, then resizing the image and keeping the aspect ratio will always leave a width greater than the height.
On the other hand, if you want to completely chop off 100px off the width (this would be cropping) but the image is not really being resized, then that would be different. In this case, ratio does not come into play at all.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic