gpt4 book ai didi

java - 旋转 bufferedImage 会改变图像的大小

转载 作者:行者123 更新时间:2023-11-30 10:22:42 26 4
gpt4 key购买 nike

我正在尝试将 bufferedImage 旋转 90°,但我无法完全完成。

我可以旋转图像,但生成的图像有点奇怪。

1) 左右两侧黑边

2)信息丢失

举个例子:

这是原始图像。

enter image description here

旋转图像后,我得到了这个:

enter image description here

我真的不知道我在这里做错了什么。

这是我用来旋转缓冲图像的代码:

BufferedImage imgIn = ImageIO.read(new File("landscape.jpg"));
BufferedImage imgIn2 = imgIn;
AffineTransform tx = new AffineTransform();
tx.rotate(Math.PI / 2, imgIn2.getWidth() / 2, imgIn2.getHeight() / 2);//(radian,arbit_X,arbit_Y)
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
BufferedImage rotatedImage = new BufferedImage(imgIn2.getHeight(), imgIn2.getWidth(), imgIn2.getType());
op.filter(imgIn2, rotatedImage);
ImageIO.write(rotatedImage, "JPEG", new File("testRotation.jpg"));

帮助将不胜感激!

最佳答案

使用仿射变换

import javax.imageio.ImageIO;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;

public class test {
public static void main(String[] arg)throws Exception
{
BufferedImage imgIn = ImageIO.read(new File("/home/arpit/Desktop/image.jpg"));
BufferedImage imgIn2 = new BufferedImage(imgIn.getHeight(), imgIn.getWidth(), imgIn.getType());

AffineTransform tx = new AffineTransform();
tx.rotate(Math.PI / 2, imgIn2.getWidth() / 2, imgIn.getHeight() / 2);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
op.filter(imgIn, imgIn2);

ImageIO.write(imgIn2, "JPEG", new File("/home/arpit/Desktop/testrotated.jpg"));
}
}

没有仿射变换

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class test {
public static void main(String[] arg)throws Exception
{
BufferedImage imgIn = ImageIO.read(new File("/home/arpit/Desktop/image.jpg"));
BufferedImage imgIn2 = new BufferedImage(imgIn.getHeight(), imgIn.getWidth(), imgIn.getType());

for(int x=0;x<imgIn.getWidth();x++)
{
for(int y=0;y<imgIn.getHeight();y++)
{
int color = imgIn.getRGB(x,y);
imgIn2.setRGB(imgIn2.getWidth()-1-y,x,color);

}
}
ImageIO.write(imgIn2, "JPEG", new File("/home/arpit/Desktop/testrotated.jpg"));
}
}

关于java - 旋转 bufferedImage 会改变图像的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47334261/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com