【问题标题】:Java image rotationJava图像旋转
【发布时间】:2014-01-27 21:03:49
【问题描述】:

你能评论这段代码吗?我了解一些部分,但不是全部。

这段代码是将图像逆时针旋转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);
}

【问题讨论】:

  • 基本上由于Swing坐标系以y = 0开头,所以必须翻转x坐标才能旋转。

标签: java image image-processing image-rotation


【解决方案1】:

给定以下坐标轴

      y
      | o
-x ------- x
      |
     -y

要旋转图像,您需要将轴从 Y 更改为 X,从 X 更改为 -Y

      y
      | 
-x ------- x
      | o
     -y

代码 bufferImgOut.setRGB(y,destY, px); 将每个点 (x,y) 分配给 (y,-x) 然后代码 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)

希望对你有帮助

【讨论】:

  • 非常感谢!我现在明白了!!
猜你喜欢
  • 2022-01-09
  • 2012-08-23
  • 2013-09-05
  • 2015-09-16
  • 1970-01-01
  • 2018-08-24
  • 1970-01-01
相关资源
最近更新 更多