简体   繁体   English

Java BufferedImage旋转

[英]Java BufferedImage Rotate

I need to rotate a jpg image so I wrote this function: 我需要旋转jpg图像,所以我写了这个函数:

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: 我他们使用ImageIO从文件读取和写入图像:

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. 就像@haraldk的注释一样,将结果图像传递给AffineTransformOp.filter函数,而不要使用null。 Read @haraldk's comment for an explanation. 阅读@haraldk的评论以获取解释。

Regards. 问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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