旋转图像90度在java

这个问题在这里已经有一个答案:> Java: Rotating Images2
我设法将图像旋转180度,但希望顺时针旋转90度,有人可以编辑我的代码,以便进行解释.谢谢.
private void rotateClockwise()
    {
        if(currentimage != null){
            int width = currentimage.getWidth();
            int height = currentimage.getHeight();
            OFImage newImage = new OFImage(width,height);
            for(int y = 0; y < height; y++) {
                for(int x = 0; x < width; x++) {
                    newImage.setPixel( x,height-y-1,currentimage.getPixel(x,y));
                }
        }
            currentimage = newImage;
            imagePanel.setimage(currentimage);
            frame.pack();
    }
    }

解决方法

使用此方法.
/**
 * Rotates an image. Actually rotates a new copy of the image.
 * 
 * @param img The image to be rotated
 * @param angle The angle in degrees
 * @return The rotated image
 */
public static Image rotate(Image img,double angle)
{
    double sin = Math.abs(Math.sin(Math.toradians(angle))),cos = Math.abs(Math.cos(Math.toradians(angle)));

    int w = img.getWidth(null),h = img.getHeight(null);

    int neww = (int) Math.floor(w*cos + h*sin),newh = (int) Math.floor(h*cos + w*sin);

    BufferedImage bimg = toBufferedImage(getEmptyImage(neww,newh));
    Graphics2D g = bimg.createGraphics();

    g.translate((neww-w)/2,(newh-h)/2);
    g.rotate(Math.toradians(angle),w/2,h/2);
    g.draWrenderedImage(toBufferedImage(img),null);
    g.dispose();

    return toImage(bimg);
}

取自我的ImageTool课.

希望有帮助

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...