如何使用 Java 二维数组水平镜像图像?
How to mirror an image horizontally using Java 2D Arrays?
大家好我遇到了一个我似乎无法弄清楚的问题,而且类似的帖子与我使用的结构不同。因此,我真的很感激一些指导。来看看我的水平镜像方法:
public void mirrorHorizontal(){
Pixel[][] pixels = this.getPixels2D();
Pixel topPixel = null;
Pixel bottomPixel = null;
int width = pixels[0].length;
for (int row = 0; row < pixels.length / 2; row++){
for (int col = 0; col < width; col++){
bottomPixel = pixels[row][col];
topPixel = pixels[row][col];
bottomPixel.setColor(topPixel.getColor());
}
} }
让我们确定提供此代码是为了使用和修改,所以我会尽力解释。
在这个 for 循环中,我所做的是通过将 pixels.length 减去 2 来将行切成两半,这样我就可以在顶部获得顶部图片,在底部获得一张图片。
for (int row = 0; row < pixels.length / 2; row++){
所以我认为这是我的问题:
bottomPixel = pixels[row][col];
topPixel = pixels[row][col];
我不知道在 bottomPixel 里面放什么 [row][col]
我相信它必须呈现如下内容:
- 1 2 3 4
- 5 6 7 8
- 5 6 7 8
- 1 2 3 4
但是我不确定如何修改它来实现它。
这使用两个修改过的 for 循环和不同的值来实现与 AngryDuck 翻转多维数组 java 类似的效果。
因此在您的示例中,bottomPixel
和 topPixel
完全相同。
如果我正确理解你的问题,这将解决你的问题:
for (int row = 0; row < pixels.length / 2; row++){
for (int col = 0; col < pixels[0].length; col++){
topPixel = pixels[row][col];
bottomPixel = pixels[(pixels.length - 1) - i][col];
bottomPixel.setColor(topPixel.getColor());
}
}
我更改了 bottomPixel
的行坐标,因此它将采用底部的镜像坐标。但这仅在您的图像恰好有 pixels.length / 2
行时才有效。
大家好我遇到了一个我似乎无法弄清楚的问题,而且类似的帖子与我使用的结构不同。因此,我真的很感激一些指导。来看看我的水平镜像方法:
public void mirrorHorizontal(){
Pixel[][] pixels = this.getPixels2D();
Pixel topPixel = null;
Pixel bottomPixel = null;
int width = pixels[0].length;
for (int row = 0; row < pixels.length / 2; row++){
for (int col = 0; col < width; col++){
bottomPixel = pixels[row][col];
topPixel = pixels[row][col];
bottomPixel.setColor(topPixel.getColor());
}
} }
让我们确定提供此代码是为了使用和修改,所以我会尽力解释。
在这个 for 循环中,我所做的是通过将 pixels.length 减去 2 来将行切成两半,这样我就可以在顶部获得顶部图片,在底部获得一张图片。
for (int row = 0; row < pixels.length / 2; row++){
所以我认为这是我的问题:
bottomPixel = pixels[row][col];
topPixel = pixels[row][col];
我不知道在 bottomPixel 里面放什么 [row][col]
我相信它必须呈现如下内容:
- 1 2 3 4
- 5 6 7 8
- 5 6 7 8
- 1 2 3 4
但是我不确定如何修改它来实现它。
这使用两个修改过的 for 循环和不同的值来实现与 AngryDuck 翻转多维数组 java 类似的效果。
因此在您的示例中,bottomPixel
和 topPixel
完全相同。
如果我正确理解你的问题,这将解决你的问题:
for (int row = 0; row < pixels.length / 2; row++){
for (int col = 0; col < pixels[0].length; col++){
topPixel = pixels[row][col];
bottomPixel = pixels[(pixels.length - 1) - i][col];
bottomPixel.setColor(topPixel.getColor());
}
}
我更改了 bottomPixel
的行坐标,因此它将采用底部的镜像坐标。但这仅在您的图像恰好有 pixels.length / 2
行时才有效。