This question already has an answer here:
这个问题在这里已有答案:
- Java: Rotating Images 4 answers
Java:旋转图像4个答案
I try to rotate a BufferedImage and it works, but the rotated image has a black border around some sides and I don't know why... I also feel like it gets smaller after rotating.
我尝试旋转BufferedImage并且它可以工作,但旋转的图像在某些边上有一个黑色边框,我不知道为什么......我也觉得旋转后它会变小。
import java.awt.image.BufferedImage;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class example {
static void main(String[] args) throws IOException {
BufferedImage imgResc = ImageIO.read(new File("landscape.jpg"));
AffineTransform tx = new AffineTransform();
tx.rotate(Math.PI / 2, imgResc.getWidth() / 2, imgResc.getHeight() / 2);//(radian,arbit_X,arbit_Y)
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
BufferedImage rotatedImage = new BufferedImage(imgResc.getHeight(),imgResc.getWidth(),imgResc.getType());
op.filter(imgResc, rotatedImage);
ImageIO.write(rotatedImage, "JPEG", new File("rotated_90_right.jpg"));
}
}
Above is the code snippet for rotating the image like I found it online.
上面是用于旋转图像的代码片段,就像我在网上找到的那样。
Help would be great!
帮助会很棒!
EDIT:
This is how it should look like and what I get after rotating: (1920 x 1200)
这就是它应该是什么样子以及旋转后得到的结果:(1920 x 1200)
1 个解决方案
#1
1
If you rotate by PI/2 then the new image has width the height of the source and height the width of the source, so:
如果按PI / 2旋转,则新图像的宽度为源的高度,高度为源的宽度,因此:
BufferedImage rotatedImage = new BufferedImage(imgResc.getHeight(),
imgResc.getWidth(),
imgResc.getType());
would be better.
会更好。
#1
1
If you rotate by PI/2 then the new image has width the height of the source and height the width of the source, so:
如果按PI / 2旋转,则新图像的宽度为源的高度,高度为源的宽度,因此:
BufferedImage rotatedImage = new BufferedImage(imgResc.getHeight(),
imgResc.getWidth(),
imgResc.getType());
would be better.
会更好。