Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Crop an Image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Crop an Image " Watch "Crop an Image " New topic
Author

Crop an Image

Sam Saha
Ranch Hand

Joined: Jan 23, 2010
Posts: 104
I have an image which original size is 125px wide and 150px tall. I am setting this image into the bean for web service purpose. But I wanted to crop the original image to a size of 80px wide and 80px tall before I set it to the bean. I would appreciate if someone can suggest me how if it is possible and how would I able to do that. Thank you very much.

Here is the code how I am setting the Image into the bean:

Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 273

Try using the read() and write() methods of the javax.imageio.ImageIO class, along with the getScaledInstance() method of the java.awt.Image class.


SCJP 6 || SCWCD 5
Sam Saha
Ranch Hand

Joined: Jan 23, 2010
Posts: 104
I am wondering if you could provide a sample for that. I would really appreciate that. Thanks!!
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 273

Something like this (though I couldn't get it to output a JPEG thumbnail, only a PNG thumbnail)

Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
You may wish to read The Perils of Image.getScaledInstance(), though.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Do you want to crop or scale? They're not the same thing. How are you acquiring the image, any guarantee it's in a compatible format for the system? That can have a HUGE impact on the performance of things like drawImage.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Its not clear to me if you want to just crop or scale one image, or to programatically scale or crop lots of images in your Java application.

If its the former, I'd suggest you use something like The Gimp, or one of the web services Flikr/Picassa that support simple photo editing.
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 273

Thanks for the link Lester. I didn't know that Image.getScaledInstance() was so outdated!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Crop an Image
 
Similar Threads
Crop an image in Java
How to crop image in java
Resizing Image: Java vs PHP
several animation frames in one file, transparency issues
How can I split an Image ?