0

I am trying to rotate and image left and right by 90 degrees.

For some reason though, the output of this process results in corruption.

Here is my code:
(its groovy but it doesnt take much imagination to pretend its java)

void rotate(File file){
    def image = ImageIO.read(file);
    double theta  = Math.PI / 2;
    def w = image.width / 2;
    def h = image.height / 2;

    def transform = new AffineTransform();
    transform.rotate(theta, h, w);
    def op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
    image = op.filter(image, null);

    def name = file.getName();
    def type = name.substring(name.lastIndexOf(".") + 1, name.length());
    ImageIO.write(image,type,file);
}

original: enter image description here

rotated:enter image description here

mkoryak
  • 57,086
  • 61
  • 201
  • 257

2 Answers2

1

If by corruption you are referring to the color change, take out the filter. That's giving you a negative image if I'm understanding the syntax properly.

Whenever I use transforms I leave filters off and do them by hand. It does take a lot of time, but they always turn out being more useful. Just a suggestion.

Jon Egeland
  • 12,470
  • 8
  • 47
  • 62
  • i dont really know what im doing here, but dropping the filter and doing `g.drawImage(origImg, transorm, null);` drew the right stuff into g – mkoryak May 06 '11 at 15:07
  • @mkyorak The way you were applying the filter was basically saying to take the color of a pixel, and multiply (is that the default?) those values by the same pixels color. Doing that results in a 'negative' image. For example, the darkest part of the leaves became very light blue. – Jon Egeland May 06 '11 at 19:00
1

The filter() method requires a src and dst BufferedImage, which must be different.

Image image = null;
try {
    image = ImageIO.read(new File("gZtC3.jpg"));
} catch (IOException ex) {
    ex.printStackTrace(System.err);
}
double theta = Math.PI / 2;
int w = image.getWidth(null);
int h = image.getHeight(null);
AffineTransform at = AffineTransform.getRotateInstance(theta, w / 2, h / 2);
BufferedImage src = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = src.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
BufferedImage dst = op.filter(src, null);
this.add(new JLabel(new ImageIcon(dst), JLabel.CENTER));
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • thanks for the code but this actually had the same issue, also in your example you still pass in null as 2nd arg to filter which is what i do. this seems to create another bufferedImage with contents of original, and writes into it. – mkoryak May 06 '11 at 15:05
  • Your source hasn't been rendered yet, so it's rotated but empty. Using `filter()` is nice for chaining, but your accepted approach is correct. Compare this [Q&A](http://stackoverflow.com/questions/5864490/how-to-setsize-of-image-using-rescaleop/5864503). – trashgod May 06 '11 at 18:01