storing transformed BufferedImage in Java

2019-07-18 14:21发布

问题:

In Java, instead of using photoshop to transform my images(that I use in the program), I want to use code to transform and save them.

I have created an AffineTransform object "at" and called the rotate() method. I have a BufferedImage called "image".

I can draw the image on the screen with the desired transformation with this code:

g2d.drawImage(image, at, null);

What I want to do is to store the combination of at and image in a new BufferedImage image2. How can I do this so thatg2d.drawImage(image2,50,50, null); will show the rotated version of image?

edit: I've tweaked Ezequiel's answer a bit to get the effect I wanted. This did the trick:

BufferedImage image2= null;
AffineTransformOp affineTransformOp = new AffineTransformOp(at,AffineTransformOp.TYPE_BILINEAR);
image2 = affineTransformOp.filter(image, image2);
g2d.drawImage(image2, 50, 50, null);

回答1:

With AffineTransformOp class:

BufferedImage original; //Instatiate with desired image.
BufferedImage transformed:  //Used to store transformed image.
AffineTransform at; //Transformations needed.

AffineTransformOp affineTransformOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
affineTransformOp.filter(original, transformed );