围绕它的中心旋转图片

是否有一种简单的方法可以围绕它的中心旋转图片? 我首先使用了AffineTransformOp 。 看起来很简单,需要找到一个矩阵的正确参数应该在一个漂亮而整洁的谷歌会话中完成。 所以我认为…

我的结果是这样的:

public class RotateOp implements BufferedImageOp { private double angle; AffineTransformOp transform; public RotateOp(double angle) { this.angle = angle; double rads = Math.toRadians(angle); double sin = Math.sin(rads); double cos = Math.cos(rads); // how to use the last 2 parameters? transform = new AffineTransformOp(new AffineTransform(cos, sin, -sin, cos, 0, 0), AffineTransformOp.TYPE_BILINEAR); } public BufferedImage filter(BufferedImage src, BufferedImage dst) { return transform.filter(src, dst); } } 

如果忽略旋转90度的倍数(sin()和cos()无法正确处理的情况),真的很简单。 该解决方案的问题在于,它围绕图片左上角的(0,0)坐标点进行变换,而不是围绕图片中心的正常预期。 所以我在我的filter中添加了一些内容:

  public BufferedImage filter(BufferedImage src, BufferedImage dst) { //don't let all that confuse you //with the documentation it is all (as) sound and clear (as this library gets) AffineTransformOp moveCenterToPointZero = new AffineTransformOp( new AffineTransform(1, 0, 0, 1, (int)(-(src.getWidth()+1)/2), (int)(-(src.getHeight()+1)/2)), AffineTransformOp.TYPE_BILINEAR); AffineTransformOp moveCenterBack = new AffineTransformOp( new AffineTransform(1, 0, 0, 1, (int)((src.getWidth()+1)/2), (int)((src.getHeight()+1)/2)), AffineTransformOp.TYPE_BILINEAR); return moveCenterBack.filter(transform.filter(moveCenterToPointZero.filter(src,dst), dst), dst); } 

我在这里的想法是,forms改变矩阵应该是单位矩阵(是正确的英语单词吗?)和移动整个画面的向量是最后2个条目。 我的解决方案首先使图片变得更大然后再变小(这并不重要 – 原因未知!!! )并且还将图片的3/4左右切掉(重要的是 – 原因可能是图片是移动到“从(0,0)到(宽度,高度)”图像尺寸的合理范围之外。

通过所有的数学,我没有受过如此训练,计算机所做的所有错误以及其他一切都不能轻易进入我的脑海,我不知道该怎么走。 请给出建议。 我想围绕它的中心旋转图片,我想了解AffineTransformOp。

如果我正确理解您的问题,您可以转换为原点,旋转和平移,如本例所示。

当您使用AffineTransformOp ,此示例可能更适合。 特别要注意最后指定的第一个应用顺序,其中连接了操作; 他们不是可交换的。