如何水平镜像图像?

时间:2015-03-23 01:37:19

标签: java

我是一名高中生。我想水平镜像一个图像。我已经想出了垂直镜像它的方法,但我不知道如何开始这个任务。

我复制并粘贴了上一个方法中的代码,并且正在寻找可编辑的内容,但到目前为止还没有运气。

public void mirrorHorizontal()
  {
      Pixel[][] pixels = this.getPixels2D();
      Pixel leftPixel = null;
      Pixel rightPixel = null;
      int width = pixels[0].length;
      for (int row = 0; row < pixels.length; row++)
      {
         for (int col = 0; col < width / 2; col++)
         {
            leftPixel = pixels[row][col];
            rightPixel = pixels[row][width - 1 - col];
            leftPixel.setColor(rightPixel.getColor());
         }
      }
  }

如何更改此代码以水平镜像图像?

1 个答案:

答案 0 :(得分:0)

这样做吗?

public void mirrorHorizontal()
      {
          Pixel[][] pixels = this.getPixels2D();
          Pixel topPixel = null;
          Pixel bottomPixel = null;

      int height = pixels.length;
      int width = pixels[0].length;

      for (int row = 0; row < height / 2; row++)
      {
         for (int col = 0; col < width; col++)
         {
            topPixel = pixels[row][col];
            bottomPixel = pixels[height - 1 - row][width];
            topPixel.setColor(bottomPixel.getColor());
         }
      }
  }
相关问题