Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Crop an Image

 
Sam Saha
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using the read() and write() methods of the javax.imageio.ImageIO class, along with the getScaledInstance() method of the java.awt.Image class.
 
Sam Saha
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am wondering if you could provide a sample for that. I would really appreciate that. Thanks!!
 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like this (though I couldn't get it to output a JPEG thumbnail, only a PNG thumbnail)

 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may wish to read The Perils of Image.getScaledInstance(), though.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the link Lester. I didn't know that Image.getScaledInstance() was so outdated!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic