旋转BufferedImage时出现的问题

10

我在使用Java的AffineTransform类旋转图像时遇到了一些问题。

我有一个用于创建旋转(90度)图像副本的方法:

private BufferedImage createRotatedCopy(BufferedImage img, Rotation rotation) {
    int w = img.getWidth();
    int h = img.getHeight();

    BufferedImage rot = new BufferedImage(h, w, BufferedImage.TYPE_INT_RGB);

    double theta;
    switch (rotation) {
        case CLOCKWISE:
            theta = Math.PI / 2;
            break;
        case COUNTERCLOCKWISE:
            theta = -Math.PI / 2;
            break;
        default:
            throw new AssertionError();
    }

    AffineTransform xform = AffineTransform.getRotateInstance(theta, w / 2, h / 2);
    Graphics2D g = (Graphics2D) rot.createGraphics();
    g.drawImage(img, xform, null);
    g.dispose();

    return rot;
}

Rotation是一个简单的枚举类型,其值包括NONE(无旋转)、CLOCKWISE(顺时针旋转)和COUNTERCLOCKWISE(逆时针旋转)。

我的问题症状在这里显示:

http://perp.se/so/rotate_problems.html

所以,旋转本身没有问题,但生成的图像没有锚定到正确的坐标(或者说没有按照正确的方式进行锚定)。由于我一开始就不知道自己在做什么(我的线性代数很弱),所以我不知道如何自己解决这个问题。

我尝试了一些随意调整AffineTransform实例的方法,但这并没有帮助我(当然)。我尝试过谷歌搜索(和搜索SO),但我看到的所有示例基本上都使用与我相同的方法...但对我来说并没有用。

感谢提供建议。


1
.NET的等效问题:https://dev59.com/43E95IYBdhLWcg3wn_b2 - finnw
4个回答

18
如果必须将变换表示为单个旋转,则锚点取决于旋转方向:要么是 (w/2, w/2),要么是 (h/2, h/2)
但更简单的方式可能是通过 translate; rotate; translate 来表示,例如:
AffineTransform xform = new AffineTransform();
xform.translate(0.5*h, 0.5*w);
xform.rotate(theta);
xform.translate(-0.5*w, -0.5*h);

还要考虑使用getQuadrantRotateInstance而不是getRotateInstance


我明白你的意思,我也尝试过那种方法。然而问题仍然存在。它们仍然被绘制在目标区域的“外面”,虽然从水平上来说是在“另一侧”。如果不清楚我的意思,我可以提供更多的截图。 - perp
@perp,你是对的,已经修复了。我测试了新版本,它可以正常工作。 - finnw
运行得非常好!我现在认为我对情况有了更好的理解。谢谢! - perp
如果有人需要帮助的话:在应用此解决方案后,我的图像仍然部分裁剪,但只在一个轴上。问题不在于变换,而是由于我使用了错误的图像类型。所以也要考虑这个问题 :) - ARRG

3

如果您只需要90度旋转,可以避免使用AffineTransform:

public BufferedImage rotate90DX(BufferedImage bi) {
    int width = bi.getWidth();
    int height = bi.getHeight();
    BufferedImage biFlip = new BufferedImage(height, width, bi.getType());
    for(int i=0; i<width; i++)
        for(int j=0; j<height; j++)
            biFlip.setRGB(height-1-j, width-1-i, bi.getRGB(i, j));
    return biFlip;
}

这还可以避免矩形图像的边缘被切割掉。
来自:http://snippets.dzone.com/posts/show/2936

3
提醒一下,强制访问RGB像素值会使Java2D中的图像加速管道失效。这种方法可能会得到您想要的结果,但速度会慢得多。 - Riyad Kalla
我测试了这段代码,它不仅可以旋转图像,还能改变左右(就像在镜子中一样)。 - Radon8472

1

你可以尝试另一种方法,从图像创建一个图标,然后使用旋转图标

或者你可以尝试我在Sun论坛上找到的这段旧代码:

import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import javax.imageio.*;
import javax.swing.*;

public class RotateImage {
    public static void main(String[] args) throws IOException {
        URL url = new URL("https://blogs.oracle.com/jag/resource/JagHeadshot-small.jpg");
        BufferedImage original = ImageIO.read(url);
        GraphicsConfiguration gc = getDefaultConfiguration();
        BufferedImage rotated1 = tilt(original, -Math.PI/2, gc);
        BufferedImage rotated2 = tilt(original, +Math.PI/4, gc);
        BufferedImage rotated3 = tilt(original, Math.PI, gc);
        display(original, rotated1, rotated2, rotated3);
    }

    public static BufferedImage tilt(BufferedImage image, double angle, GraphicsConfiguration gc) {
        double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
        int w = image.getWidth(), h = image.getHeight();
        int neww = (int)Math.floor(w*cos+h*sin), newh = (int)Math.floor(h*cos+w*sin);
        int transparency = image.getColorModel().getTransparency();
        BufferedImage result = gc.createCompatibleImage(neww, newh, transparency);
        Graphics2D g = result.createGraphics();
        g.translate((neww-w)/2, (newh-h)/2);
        g.rotate(angle, w/2, h/2);
        g.drawRenderedImage(image, null);
        return result;
    }

    public static GraphicsConfiguration getDefaultConfiguration() {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        return gd.getDefaultConfiguration();
    }

    public static void display(BufferedImage im1, BufferedImage im2, BufferedImage im3, BufferedImage im4) {
        JPanel cp = new JPanel(new GridLayout(2,2));
        addImage(cp, im1, "original");
        addImage(cp, im2, "rotate -PI/2");
        addImage(cp, im3, "rotate +PI/4");
        addImage(cp, im4, "rotate PI");

        JFrame f = new JFrame("RotateImage");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setContentPane(cp);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    static void addImage(Container cp, BufferedImage im, String title) {
        JLabel lbl = new JLabel(new ImageIcon(im));
        lbl.setBorder(BorderFactory.createTitledBorder(title));
        cp.add(lbl);
    }
}

0

我不知道这是否是你的问题。

AffineTransform xform = AffineTransform.getRotateInstance(theta, w / 2, h / 2);

为什么不试试?
AffineTransform xform = AffineTransform.getRotateInstance(theta);

或者

g.transform(AffineTransform.getRotateInstance(theta));
g.drawImage(img, 0, 0, w/2, h/2, null, null);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接