简体   繁体   中英

Java BufferedImage Rotate

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

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

Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM