• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do you crop an image in Java?

 
Phil Powell
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried two different techniques, and both of them failed:

I thought I got it and again, I failed.





This constantly throws the following exception:





I was under the impression that I'm using java.awt.Image, where did sun.awt.image.ToolkitImage come from? I can't figure out how to cast this correctly either, what do I do?

-------------------------------------------------------

Second approach:




And now I get this exception:




Is image cropping even possible in Java?
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is image cropping even possible in Java?
Yes.
About the exception ClassCastException: sun.awt.image.ToolkitImage cannot be cast to
java.awt.image.BufferedImage
:
You probably loaded your source image with one of the older Toolkit getImage or
createImage methods. These return type Image which cannot be cast to BufferedImage.
To obtain a BufferedImage with an Image you can create a new BufferedImage and draw the
Image into it:

Or, you could use a newer image–loading method that returns a BufferedImage:

About the exception RasterFormatException: (x + width) is outside of Raster:
This exception is thrown by the getSubimage method as stated in the BufferedImage
api. You have to be very careful/precise in using this method to avoid trouble. Another
problem with using it to obtain smaller images is that the two images are not independent.
Making changes to one image automatically changes the other. Sometimes this can cause
trouble. You can always copy the subimage into a new BufferedImage.
Another option for obtaining a clipped image is to create a new BufferedImage the size of
your clipping Rectangle, translate its graphics context by clipX, clipY and draw the src
image as above. It takes a little playing–around with to see how it works.
 
Phil Powell
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure if this might help but here are the numbers that spawned the RasterFormatException:

X = 218
Y = 399
width = 418
height = 403
 
Phil Powell
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Craig Wood:
Is image cropping even possible in Java?
Yes.
About the exception ClassCastException: sun.awt.image.ToolkitImage cannot be cast to
java.awt.image.BufferedImage
:
You probably loaded your source image with one of the older Toolkit getImage or
createImage methods. These return type Image which cannot be cast to BufferedImage.


Actually, I loaded the source image using ImageIO.read(URL) so I have no idea where TookitImage comes from except that I am using createImage() to create the image, at least thinking it would be java.awt.Image


To obtain a BufferedImage with an Image you can create a new BufferedImage and draw the
Image into it:

Or, you could use a newer image–loading method that returns a BufferedImage:

About the exception RasterFormatException: (x + width) is outside of Raster:
This exception is thrown by the getSubimage method as stated in the BufferedImage
api. You have to be very careful/precise in using this method to avoid trouble. Another
problem with using it to obtain smaller images is that the two images are not independent.
Making changes to one image automatically changes the other. Sometimes this can cause
trouble. You can always copy the subimage into a new BufferedImage.
Another option for obtaining a clipped image is to create a new BufferedImage the size of
your clipping Rectangle, translate its graphics context by clipX, clipY and draw the src
image as above. It takes a little playing–around with to see how it works.
 
Phil Powell
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GOT IT!



Phil
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!