相反,您应该旋转图像并对其进行绘画,例如... public BufferedImage rotateImage() { double rads = Math.toRadians(RotationOfImage.value); double sin = Math.abs(Math.sin(rads)); double cos = Math.abs(Math.cos(rads)); int w = transparentImage.getWidth(); int h = transparentImage.getHeight(); int newWidth = (int) Math.floor(w * cos + h * sin); int newHeight = (int) Math.floor(h * cos + w * sin); BufferedImage rotated = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = rotated.createGraphics(); AffineTransform at = new AffineTransform(); at.translate((newWidth - w) / 2, (newHeight - h) / 2); at.rotate(Math.toRadians(RotationOfImage.value), w / 2, h / 2); g2d.setTransform(at); g2d.drawImage(transparentImage, 0, 0, this); g2d.setColor(Color.RED); g2d.drawRect(0, 0, newWidth - 1, newHeight - 1); g2d.dispose();}然后您可以将其绘制为类似... @Overrideprotected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); BufferedImage rotated = rotateImage(); int x = (getWidth() - rotated.getWidth()) / 2; int y = (getHeight() - rotated.getHeight()) / 2; g2d.drawImage(rotated, x, y, this); g2d.dispose();}现在,您可以对此进行优化,因此只有在角度改变时才生成图像的旋转版本,但我会留给您 ps-我尚未对此进行测试,但是它基于此问题 I am trying to rotate a buffered Image and return the buffered Image(rotated image) using getImage() method. Rotation of image is happening but while saving it save the image as such without rotation of image.Initialization:private BufferedImage transparentImage;PaintComponent: AffineTransform at = new AffineTransform();at.rotate(Math.toRadians(RotationOfImage.value));Graphics2D g2d = (Graphics2D) g;g2d.drawImage(transparentImage, at, null);repaint();A method to return the rotated buffered Image. public BufferedImage getImage(){ return transparentImage; } 解决方案 Basically, you're rotating the Graphics context of the component and painting the image to it, which will have no effect on the original image.Instead, you should be rotating the image and the painting it, for example...public BufferedImage rotateImage() { double rads = Math.toRadians(RotationOfImage.value); double sin = Math.abs(Math.sin(rads)); double cos = Math.abs(Math.cos(rads)); int w = transparentImage.getWidth(); int h = transparentImage.getHeight(); int newWidth = (int) Math.floor(w * cos + h * sin); int newHeight = (int) Math.floor(h * cos + w * sin); BufferedImage rotated = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = rotated.createGraphics(); AffineTransform at = new AffineTransform(); at.translate((newWidth - w) / 2, (newHeight - h) / 2); at.rotate(Math.toRadians(RotationOfImage.value), w / 2, h / 2); g2d.setTransform(at); g2d.drawImage(transparentImage, 0, 0, this); g2d.setColor(Color.RED); g2d.drawRect(0, 0, newWidth - 1, newHeight - 1); g2d.dispose();}Then you could paint it doing something like...@Overrideprotected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); BufferedImage rotated = rotateImage(); int x = (getWidth() - rotated.getWidth()) / 2; int y = (getHeight() - rotated.getHeight()) / 2; g2d.drawImage(rotated, x, y, this); g2d.dispose();}Now, you could optimise this, so you only generate a rotated version of the image when the angle has changed, but I'll leave that up to youps- I've not tested this, but it's based on this question 这篇关于如何将旋转的缓冲图像保存在另一个缓冲图像中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-31 03:28