9

我正在寻找旋转图像。我有一个JInternalFrame包含一个JLabel. 标签包含图像。图像旋转后,我需要调整内部框架的大小。我目前拥有的代码旋转图像,但图像边缘周围有黑色并且它偏离中心。对于如何解决这个问题,有任何的建议吗?

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
}
4

4 回答 4

18

您需要使用三角函数来确定正确的宽度/高度,使用透明度来防止黑色区域,我认为变换是错误的,这使它偏离中心。

试试这个:

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/

于 2010-11-11T16:36:44.803 回答
4

您可以尝试使用Rotated Icon

于 2010-11-11T16:20:33.527 回答
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);
}    
于 2019-06-25T10:58:55.240 回答
0

如果您更改以下内容是否有帮助:

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

到:

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

于 2010-11-11T16:26:52.930 回答