圖片旋轉做爲一個常見功能,實際使用中用處挺多,可是此次實現卻遇到了個小問題,記錄一二java
使用的幾個類code
Graphics2d
AffineTransform
利用Graphics2d的rotate方法來實現圖片旋轉,奇怪的是一直不生效,實現代碼以下orm
BufferedImage bufferedImage = ImageUtil.getImageByPath("bg.png"); Graphics2D g2d = bufferedImage.createGraphics(); g2d.rotate(Math.toRadians(90), bufferedImage.getWidth() >> 1, bufferedImage.getHeight() >> 1); g2d.dispose();
AffineTransform
方式BufferedImage bufferedImage = ImageUtil.getImageByPath("bg.png"); AffineTransform tx = new AffineTransform(); tx.rotate(0.5, bufferedImage.getWidth() / 2, bufferedImage.getHeight() / 2); AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR); bufferedImage = op.filter(bufferedImage, null);