如何将旋转的缓冲图像保存在另一个缓冲图像中? [英] How to save rotated buffered image in another buffered image?

查看:81
本文介绍了如何将旋转的缓冲图像保存在另一个缓冲图像中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试旋转缓冲的Image,并使用getImage()方法返回缓冲的Image(旋转的图像).图像发生旋转,但是在保存图像时会保存图像,而不会旋转图像.

初始化:

private BufferedImage transparentImage;

PaintComponent:

AffineTransform at = new AffineTransform();
at.rotate(Math.toRadians(RotationOfImage.value));
Graphics2D g2d = (Graphics2D) g;

g2d.drawImage(transparentImage, at, null);
repaint();

一种返回旋转的缓冲图像的方法.

 public BufferedImage getImage(){
     return transparentImage;
 }

解决方案

基本上,您正在旋转组件的Graphics上下文并为其绘制图像,这对原始图像没有影响.

相反,您应该旋转图像并对其进行绘画,例如...

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();
}

然后您可以将其绘制为类似...

@Override
protected 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...

@Override
protected 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 you

ps- I've not tested this, but it's based on this question

这篇关于如何将旋转的缓冲图像保存在另一个缓冲图像中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆