2011-04-19 128 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