tags:

views:

531

answers:

2

I'm looking for a good alternative to the javax.imageio package, that lets me do simple rotating, cutting and scaling operations on images. For example, I would like to do

int angle, height, width;
image.rotateRight(angle).scale(height, width);

in order to obtain an image that is rotated angle degrees to the right and scaled down to height x width pixels.

Using Graphics2D and BufferedImages, I will have to do this, which is neither readable, nor easy to write:

BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = result.createGraphics();
graphics.translate(height/2, width/2);
graphics.rotate(angle);
graphics.translate(-width/2, -height/2);
graphics.drawImage(image, 0, 0, width, height, null);

(Actually, that code doesn't even account for non-square images, which will require me to do even more magic with the translating).

+5  A: 

There's the Java Advanced Imaging API which contains useful stuff like a RotatorDescriptor.

But I confess I find your above example pretty readable, so I'm not sure you'll get something more to your liking :-)

Brian Agnew
Thanks, I'll have a look at it
Jorn
+2  A: 

I agree with Brian: JAI is very good option for you. You may need to write some delegator object to get such readable code as you need and use it instead of JAI API.

Also you may use Processing (http://processing.org). It's API is simpler than JAI API. And as result of using Processing you'll get better quality for scaling and rotating operations by default.

ruslan