我在使用AffineTransform類的Java中旋轉圖像時遇到了一些問題。旋轉BufferedImage的問題
我有用於創建圖像的旋轉(90度)複製下面的方法:
private BufferedImage createRotatedCopy(BufferedImage img, Rotation rotation) {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage rot = new BufferedImage(h, w, BufferedImage.TYPE_INT_RGB);
double theta;
switch (rotation) {
case CLOCKWISE:
theta = Math.PI/2;
break;
case COUNTERCLOCKWISE:
theta = -Math.PI/2;
break;
default:
throw new AssertionError();
}
AffineTransform xform = AffineTransform.getRotateInstance(theta, w/2, h/2);
Graphics2D g = (Graphics2D) rot.createGraphics();
g.drawImage(img, xform, null);
g.dispose();
return rot;
}
旋轉是一個簡單枚舉與該值NONE,順時針方向和逆時針方向。
我的問題的症狀是在這裏顯示:
http://perp.se/so/rotate_problems.html
所以,旋轉工程確定,但由此產生的圖像不固定在正確的座標(或如何應該把它)。因爲我並不真正知道自己在做什麼(我的線性代數很弱),所以我不知道如何自己解決這個問題。
我試過一些隨機擺弄AffineTransform實例,但它並沒有幫助我(當然)。我嘗試過使用Google搜索(並搜索),但我所見過的所有示例基本上都使用與我一樣的方法...這對我不起作用。
感謝您的建議。
等效問題:http://stackoverflow.com/questions/2225363/c-rotate-bitmap-90-degrees – finnw 2010-02-13 10:46:30