在java中将图像旋转90度
我已设法将图像旋转180 degrees
但希望将其clockwise
旋转90 degrees
有人可以编辑我的代码,以便通过解释进行此操作。 谢谢。
private void rotateClockwise() { if(currentImage != null){ int width = currentImage.getWidth(); int height = currentImage.getHeight(); OFImage newImage = new OFImage(width, height); for(int y = 0; y < height; y++) { for(int x = 0; x < width; x++) { newImage.setPixel( x, height-y-1, currentImage.getPixel(x, y)); } } currentImage = newImage; imagePanel.setImage(currentImage); frame.pack(); } }
使用此方法。
/** * Rotates an image. Actually rotates a new copy of the image. * * @param img The image to be rotated * @param angle The angle in degrees * @return The rotated image */ public static Image rotate(Image img, double angle) { double sin = Math.abs(Math.sin(Math.toRadians(angle))), cos = Math.abs(Math.cos(Math.toRadians(angle))); int w = img.getWidth(null), h = img.getHeight(null); int neww = (int) Math.floor(w*cos + h*sin), newh = (int) Math.floor(h*cos + w*sin); BufferedImage bimg = toBufferedImage(getEmptyImage(neww, newh)); Graphics2D g = bimg.createGraphics(); g.translate((neww-w)/2, (newh-h)/2); g.rotate(Math.toRadians(angle), w/2, h/2); g.drawRenderedImage(toBufferedImage(img), null); g.dispose(); return toImage(bimg); }
取自我的ImageTool类。
希望能帮助到你。
看看http://docs.oracle.com/javase/tutorial/2d/index.html和http://docs.oracle.com/javase/6/docs/api/java/awt/Graphics2D.html#旋转%28double%29以及此post在java中旋转图像