Java图像旋转

Java图像旋转,java,image,image-processing,image-rotation,Java,Image,Image Processing,Image Rotation,你能评论一下这个代码吗?我理解一些部分,但不是全部 此代码用于将图像逆时针旋转90度: public static void rotate(String originalImage, String convertedImage) throws Exception { BufferedImage bufferImg = ImageIO.read(new File(originalImage)); BufferedImage bufferImgOut = new BufferedIm

你能评论一下这个代码吗?我理解一些部分,但不是全部

此代码用于将图像逆时针旋转90度:

public static void rotate(String originalImage, String convertedImage) throws Exception {
    BufferedImage bufferImg = ImageIO.read(new File(originalImage));
    BufferedImage bufferImgOut = new BufferedImage(bufferImg.getWidth(),bufferImg.getHeight(), bufferImg.getType()); 
    for( int x = 0; x < bufferImg.getWidth(); x++ ) {
        for( int y = 0; y < bufferImg.getHeight(); y++ ) {
            int px = bufferImg.getRGB(x, y);
            int destY = bufferImg.getWidth() - x - 1; //what does this line do? 
            bufferImgOut.setRGB(y,destY, px);
        }
    }
    File outputfile = new File(convertedImage);
    ImageIO.write(bufferImgOut, "png", outputfile);
}
publicstaticvoidrotate(stringoriginalimage,stringconvertedimage)引发异常{
BufferedImage bufferImg=ImageIO.read(新文件(原始图像));
BufferedImage bufferImgOut=新的BufferedImage(bufferImg.getWidth(),bufferImg.getHeight(),bufferImg.getType());
对于(int x=0;x
给定以下轴

      y
      | o
-x ------- x
      |
     -y
要旋转图像,需要将轴从Y更改为X,并从X更改为-Y

      y
      | 
-x ------- x
      | o
     -y
代码
bufferImgOut.setRGB(y,destY,px)
将每个点(x,y)指定给(y,-x),然后code
int destY=bufferImg.getWidth()-x-1
表示-x,但图像不支持负轴,然后在正轴上再次平移。-1只是由于java索引(从0到宽度)

换言之:

y          x           x
| o        | o         | o
---- x     ---- -y     ---- y
original    (y, - x )   (y, Width() - x - 1)

我希望它有帮助

基本上,因为摇摆坐标系从顶部的y=0开始,你必须翻转x坐标才能旋转。非常感谢!我现在明白了!!