Java 中的映像旋轉

來源:互聯網
上載者:User
 Java 中的映像旋轉

 

    通過AffineTransform.getScaleInstance(double sx, double sy) 可以得到
一個執行旋轉操作的AffineTransform 執行個體,再用它做參數,構造一
AffineTransformOp 對象,再調用其filter(BufferedImage src,BufferedImage dst)
方法,即可用來對BufferedImage做旋轉變換。
 

 
    但是,AffineTransformOp的filter方法,其返回的BufferedImage的座標,並
不一定與getBounds2D(BufferedImage)返回的矩形的座標相同。如果矩形左上方的坐
標為負,那部分就不會畫出來。只有座標非負的部分在目標BufferedImage中畫出。所以
單單直接調用此函數做旋轉變換,會使映像的一些部分丟失。必須使源映像在旋轉後座標都
非負,這樣才可以得到完全的映像。
 

 
 
    可在做旋轉之前,先對源映像做適當的平移,使源映像的座標足夠大,旋轉之後都非負。
 3.2 所示,w, h 分別為源映像的寬,高。旋轉後恰好可以包含整個映像的豎直矩形尺
寸為:
 

 

Width  = h * | sin(theta) |  + w * | cos(theta) |.

 

 

 

Height = h * | cos(theta) | +w * | sin(theta) |,

 

 

 

 

 

 

圖 . 映像旋轉

 

   

 這樣,只要先做平移       

 

[   1    0    (Width-w)/2   ]

 [   0    1    (Height-h)/2  ]

[   0    0    1              ]

 

 

即可。而這可通過AffineTransform.getTranslateInstance( (Width - wh)/2,( Height - h)/2 )

得到。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.