0

I need to rotate a jpg image so I wrote this function:

BufferedImage rotate(BufferedImage bufferedImage) {
    AffineTransform tx = new AffineTransform();
    tx.rotate(Math.PI/2.0, bufferedImage.getWidth() / 2, bufferedImage.getHeight() / 2);

    AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
    return op.filter(bufferedImage, null);
}

I them use ImageIO to read and write the image from file:

String [] photos = { "IMG_1998.JPG" , "IMG_1999.JPG" ,"IMG_2001.JPG" ,"IMG_2002.JPG" ,"IMG_2003.JPG"};

for(int i=0; i<photos.length-1; i++) {
     BufferedImage nextImage = rotate(ImageIO.read(new File("d:/gif/" + photos[i])));
     ImageIO.write(nextImage, "JPG", new File("d:/gif/A_" + photos[i]));
}

However when I view the output image files, they all appears as negative. (I wish I can attache the images here) Can someone point where do I do wrong?

Thanks,

Alex

Harald K
  • 26,314
  • 7
  • 65
  • 111
user1941319
  • 375
  • 3
  • 19
  • Most probably you'll find that the problem is that your JPEGs are stored as RGB with alpha channel, and most software interprets this as CMYK (thus the "inverted" or "negative" colors). Try passing an explicit destination (instead of `null`) to `op.filter(...)`, and make sure this is a `TYPE_3BYTE_BGR` or `TYPE_INT_RGB` image. – Harald K Jul 29 '17 at 10:17
  • Thanks @haraldk, I found the solution but did not know why. Now I understand why :) – user1941319 Aug 10 '17 at 20:10

1 Answers1

0

Like @haraldk's comment, pass the result image to AffineTransformOp.filter function instead of using null. Read @haraldk's comment for an explanation.

Regards.

user1941319
  • 375
  • 3
  • 19