Java BufferedImage旋转

时间:2017-07-28 21:26:51

标签: java bufferedimage javax.imageio

我需要旋转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);
}

我使用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]));
}

但是,当我查看输出图像文件时,它们都显示为负数。 (我希望我能在这里附上图片)有人能指出我哪里做错了吗?

谢谢,

亚历

1 个答案:

答案 0 :(得分:0)

与@ haraldk的评论一样,将结果图像传递给AffineTransformOp.filter函数而不是使用null。请阅读@haraldk的评论以获得解释。

问候。