旋转图像在另一个图像上

时间:2014-03-26 07:11:40

标签: java image swing rotation image-rotation

我有一种方法,当用户输入旋转图像的度数时,我想旋转图像。方法就是这样做的,但事实是它正在旋转图像,以便新图像位于旧图像之上(我不想让它做;我只想要新图像)。 RotateTest只是该类的缩写形式,其中ButtonListener类中所需的所有方法都应与旋转图像相关。

public class RotateTest {
    public Rotate() {
        try {
            String path = "default.jpg";
            File imageFile = new File(path);
            imageURL = imageFile.toURI().toURL();
            image = ImageIO.read(imageURL);
            this.imageLabel = new JLabel(imageLabel);
        } catch (IOException e) { }
    }

    public void setAngle(double angle) {
        this.angle = angle;
    }

    public void setImage(BufferedImage img) {
        this.image = img;
    }

private class ButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        boolean doAgain = true;
        Artsy artsy = new Artsy();
    if(event.getSource() == rotate) {
            //need something for cancel!
            do {
                String angleString = JOptionPane.showInputDialog("Please enter your angle in degrees.");
                double angle = Double.parseDouble(angleString);
                if(angle < 0) {
                    angle = 360 + angle;
                }
                setAngle(getAngle() + angle);
                setImage(artsy.doRotate(image, angle));
                revalidate();
                repaint();
                System.out.println("The angle is " + getAngle());
            } while(JOptionPane.OK_OPTION == 1);
        }
        else {
            if(doAgain) {
                setImage(artsy.doRotate(image, 360 - getAngle()));
                doAgain = false;
                setAngle(0);
            }
            revalidate();
            repaint();
            System.out.println("The angle is " + getAngle());
        }
}

}

这是另一个使用旋转图像的方法的类:

public class Artsy {
    public BufferedImage doRotate(BufferedImage src, double angle) {    
        angle = Math.toRadians(angle);
        Graphics2D g = (Graphics2D) src.getGraphics();      
        int w = src.getWidth();
        int h = src.getHeight();
        AffineTransform trans = new AffineTransform();
        trans.rotate(angle, w / 2, h / 2);
        AffineTransformOp scaleOp = new AffineTransformOp(trans, AffineTransformOp.TYPE_BILINEAR);
        g.drawImage(scaleOp.filter(src, null), 0, 0, null);
        g.dispose();
        return src;
    }
}

谢谢!

1 个答案:

答案 0 :(得分:0)

你的代码对我来说似乎是对的,我看不出明显的错误。尽管如此,以下功能对我的旋转图像起作用,因此它也应该是一个解决方案:

public static BufferedImage rotate(BufferedImage srcImage, double angle)
{
    double sin = Math.abs(Math.sin(Math.toRadians(angle))), cos = Math.abs(Math.cos(Math.toRadians(angle)));

    int originWidth = srcImage.getWidth(), originHeight = srcImage.getHeight();

    int newWidth = (int) Math.floor(originWidth * cos + originHeight * sin), newHeight = (int) Math.floor(originHeight * cos + originWidth * sin);

    BufferedImage newImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = newImage.createGraphics();

    g.translate((newWidth - originWidth) / 2, (newHeight - originHeight) / 2);
    g.rotate(Math.toRadians(angle), originWidth / 2, originHeight / 2);
    g.drawImage(srcImage, 0, 0, null);
    g.dispose();

    return newImage;
}

辅助功能:

/**
 * Converts an Icon to an Image
 */
public static Image iconToImage(Icon icon) {
    if (icon instanceof ImageIcon) {
        return ((ImageIcon) icon).getImage();
    }
    else {
        int w = icon.getIconWidth();
        int h = icon.getIconHeight();
        GraphicsEnvironment ge =
                GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gd.getDefaultConfiguration();
        BufferedImage image = gc.createCompatibleImage(w, h);
        Graphics2D g = image.createGraphics();
        icon.paintIcon(null, g, 0, 0);
        g.dispose();
        return image;
    }
}

JLabels图标顺时针旋转90度的示例用法:

BufferedImage buImg = new BufferedImage(imageLabel.getIcon().getIconWidth(), imageLabel.getIcon().getIconHeight(), BufferedImage.TYPE_INT_ARGB);
buImg.getGraphics().drawImage(iconToImage(imageLabel.getIcon()), 0, 0, null);
imageLabel.setIcon(new ImageIcon(rotate(buImg, 90)));