Java中如何旋转一张图片

9

我想要旋转一张图片。我有一个包含JLabelJInternalFrame。该标签包含图片。在图像被旋转后,我需要调整内部框架的大小。目前我的代码可以旋转图像,但是图像边缘有黑色并且偏离中心。请问有什么建议可以解决这个问题吗?

public void rotateIcon(int angle)
{
        int w = theLabel.getIcon().getIconWidth();
        int h = theLabel.getIcon().getIconHeight();
        int type = BufferedImage.TYPE_INT_RGB;  // other options, see api

        BufferedImage DaImage = new BufferedImage(h, w, type);
        Graphics2D g2 = DaImage.createGraphics();

        double x = (h - w)/2.0;
        double y = (w - h)/2.0;
        AffineTransform at = AffineTransform.getTranslateInstance(x, y);

        at.rotate(Math.toRadians(angle), w/2.0, h/2.0);
        g2.drawImage(new ImageIcon(getData()).getImage(), at, theLabel);
        g2.dispose();

        theLabel.setIcon(new ImageIcon(DaImage));
        this.setSize(DaImage.getWidth(),DaImage.getHeight()); //resize the frame
}

一般来说,旋转图像会改变宽度和高度(相对于X轴和Y轴)。我猜这可能导致了图像的“偏离中心”。为了解决这个问题,我必须计算新的大小并进行调整。至于黑边,这是一个很常见的情况,旋转函数无法处理alpha通道。也许这个链接对于定位会有所帮助:http://stackoverflow.com/questions/2056338/calculating-the-center-of-rotation-after-translation - Jere
1
请参见http://stackoverflow.com/questions/3420651。 - trashgod
4个回答

19
您需要使用三角函数来确定正确的宽度/高度,使用透明度来防止黑色区域出现,我认为变换是错误的,这使其偏离了中心。
请尝试以下操作:
public static BufferedImage rotate(BufferedImage image, double angle) {
    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);
    GraphicsConfiguration gc = getDefaultConfiguration();
    BufferedImage result = gc.createCompatibleImage(neww, newh, Transparency.TRANSLUCENT);
    Graphics2D g = result.createGraphics();
    g.translate((neww - w) / 2, (newh - h) / 2);
    g.rotate(angle, w / 2, h / 2);
    g.drawRenderedImage(image, null);
    g.dispose();
    return result;
}

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

来自http://flyingdogz.wordpress.com/2008/02/11/image-rotate-in-java-2-easier-to-use/


这真的很酷。不过我有一个问题。我的图像每次只需要旋转90度。如果我传入90作为角度,它实际上会旋转超过90度。 - user489041
1
这是使用弧度,所以首先使用Math.toRadians(degree)。 - Reverend Gonzo
我们能否摆脱屏幕设备的依赖? - user1050755

4

谢谢,我需要进一步研究这个。 - user489041

1

基于先前的示例,但实际上使用最近的JDK并在无头模式下工作:

public static BufferedImage rotate(BufferedImage image, double angle) {
    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);
    BufferedImage result = deepCopy(image, false);
    Graphics2D g = result.createGraphics();
    g.translate((neww - w) / 2, (newh - h) / 2);
    g.rotate(angle, w / 2, h / 2);
    g.drawRenderedImage(image, null);
    g.dispose();
    return result;
}

public static BufferedImage deepCopy(BufferedImage bi, boolean copyPixels) {
    ColorModel cm = bi.getColorModel();
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    WritableRaster raster = bi.getRaster().createCompatibleWritableRaster();
    if (copyPixels) {
        bi.copyData(raster);
    }
    return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}    

0

如果你将:

BufferedImage DaImage = new BufferedImage(height, width, type);

改为:

BufferedImage DaImage = new BufferedImage(**width, height**, type);

这样做有帮助吗?


是的,我来回切换它们,但仍然没有成功。 - user489041

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