File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Crop an image in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Crop an image in Java" Watch "Crop an image in Java" New topic
Author

Crop an image in Java

Amit Shanker
Greenhorn

Joined: Mar 08, 2003
Posts: 23
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

Joined: Jul 11, 2001
Posts: 15300
    
    6

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.


GenRocket - Experts at Building Test Data
Amit Shanker
Greenhorn

Joined: Mar 08, 2003
Posts: 23
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

Joined: Jul 11, 2001
Posts: 15300
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Crop an image in Java