2011-04-19 100 views
1

是否有一种简单的方法来围绕它的中心旋转图片?我首先使用了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度的倍数(不能被罪正确处理和cos的情况下,真正简单的( ))。该解决方案的问题在于,它围绕图片左上角的(0,0)坐标点进行变换,而不是围绕图片的中心,这通常是预期的。所以我加了一些东西到我的过滤器:

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); 
    } 

我的想法在这里是形式变化的矩阵应该是单位矩阵(是正确的英文单词?)是移动的全貌和矢量周围是最后2个条目。我的解决办法首先使得画面更大,然后再小的(其实并不重要,我的 - 原因不明!),并也减少周围画拿走的3/4(什么事情很多 - 原因可能是该图片移动到“从(0,0)到(宽度,高度)”图片尺寸标注的合理水平之外)。

通过我不是那么培养出来的所有数学和所有在计算,计算机是错误和其他一切并没有进入我的头这么容易,我不知道该怎么走的更远。请给出建议。我想围绕它的中心旋转图片,我想了解AffineTransformOp。

+0

'setToIdentity()'方法将矩阵设置为乘性身份。 – trashgod 2011-04-19 20:25:55

回答

2

如果我正确理解你的问题,你可以转化为原点,旋转和平移回,如本example

当您使用AffineTransformOp,这example可能会更加中肯。特别是,请注意最后指定的第一个应用操作的级联顺序;他们是而不是交换。

+0

+1两种不同的方法。 – camickr 2011-04-20 02:01:14