1
當前我正在使用此代碼(我發現here)在Java中旋轉圖像。代碼運行良好,但我對旋轉圖像的質量不滿意。你們可以幫我改進它,或者你能推薦一個好的Java圖像旋轉庫嗎?改善圖像旋轉質量
public static BufferedImage rotate(BufferedImage image, float angle)
{
float radianAngle = (float) Math.toRadians(angle) ;
float sin = (float) Math.abs(Math.sin(radianAngle));
float cos = (float) Math.abs(Math.cos(radianAngle));
int w = image.getWidth() ;
int h = image.getHeight();
int neww = (int) Math.round(w * cos + h * sin);
int newh = (int) Math.round(h * cos + w * sin);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
BufferedImage result = gc.createCompatibleImage(neww, newh, Transparency.TRANSLUCENT);
Graphics2D g = result.createGraphics();
//---------------------------------------------------------------------
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.translate((neww-w)/2, (newh-h)/2);
g.rotate(radianAngle, w/2, h/2);
//---------------------------------------------------------------------
g.drawRenderedImage(image, null);
g.dispose();
return result;
}