我正在尝试旋转bufferedImage
90°,但我无法完成它。
我可以旋转图像,但结果图像有点奇怪。
1)左侧和右侧的黑色边框
2)信息丢失
以下是一个例子:
这是原始图片。
旋转图像后,我得到了这个:
我真的不知道我在这里做错了什么。
这是我用来旋转bufferedImage的代码:
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"));
非常感谢帮助!
答案 0 :(得分:1)
使用AffineTransform
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"));
}
}
没有AffineTransform
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"));
}
}