Java 将图片旋转90度时,我做错了什么
我完全被这个问题难住了。这是我将阵列旋转90度的逻辑: 例如:Java 将图片旋转90度时,我做错了什么,java,arrays,image,ppm,Java,Arrays,Image,Ppm,我完全被这个问题难住了。这是我将阵列旋转90度的逻辑: 例如: 1 2 3 4 5..... ^ 2 6 8 7 4..... | 6 4 9 8 0..... | .....THEN..... ------> 8 3 0 5 9..... | 如果我想把它旋转90度,我想从最下面的一行向上读取数组,然后在右边增加一个宽度。因此,阵列将是 8 6 2 1 3 4 6 2 0 9 8 3 5 8 7 4 9 0 4 5 现在我将这个逻辑应用到我的像素阵列中,并将图
1 2 3 4 5..... ^
2 6 8 7 4..... |
6 4 9 8 0..... | .....THEN..... ------>
8 3 0 5 9..... |
如果我想把它旋转90度,我想从最下面的一行向上读取数组,然后在右边增加一个宽度。因此,阵列将是
8 6 2 1
3 4 6 2
0 9 8 3
5 8 7 4
9 0 4 5
现在我将这个逻辑应用到我的像素阵列中,并将图片旋转90度
以下是我的Java方法代码:
public void rotate90(){
Pixel[][] rotated = new Pixel[Iwidth][Iheight];
int Ch = Iheight-1, Cw = 0;
while (Cw < Iwidth){
while (Ch > -1){
rotated[Cw][Iheight - Ch - 1] = new Pixel(Image[Ch][Cw].getRed(), Image[Ch][Cw].getGreen(), Image[Ch][Cw].getBlue());
Ch--;
}
Cw++;
Ch = Iheight-1;
}
Image = rotated;
int temp = Iheight;
Iheight = Iwidth;
Iwidth = temp;
}
我有一个
imageWriter.println(Iheight + " " + Iwidth);
在我的写作方法中,愚蠢的我-
感谢@Marcelo和@rolfl的帮助。发现了问题
我的代码一直都是正确的,我有一个愚蠢的1行代码,把我搞砸了这应该可以满足您的需要:
public void rotate90() {
Pixel[][] rotated = new Pixel[iWidth][iHeight];
for (int x = 0; x < iWidth; x++) {
for (int y = 0; y < iHeight; y++) {
rotated[x][y] = image[iWidth- y - 1][x];
}
}
image = rotated;
int temp = iHeight;
iHeight = iWidth;
iWidth = temp;
}
public void rotate90(){
像素[][]旋转=新像素[iWidth][iHeight];
对于(int x=0;x
我还建议你看一看。这将有助于提高代码的可读性。嗯,Marcelo在这方面比我做得好,但我的程序实际上运行 首先,要么实现一个
clone()
方法,要么为Pixel
创建一个新的构造函数,该构造函数将现有像素“复制”
作为位置的函数,这种旋转更容易处理。。。。我让它在这里使用字符串值而不是像素
public static void main(String[] args) {
final int width = 5, height = 3;
String[][] values = new String[height][width];
for (int c = 0; c < width; c++) {
for (int r = 0; r < height; r++) {
values[r][c] = String.format("%3d", r * width + c);
}
}
for (String[] row : values) {
System.out.println(Arrays.toString(row));
}
System.out.println();
int nheight = width;
int nwidth = height;
String[][] rotate = new String[nheight][nwidth];
for (int c = 0; c < nwidth; c++) {
for (int r = 0; r < nheight; r++) {
rotate[r][c] = values[height - 1 - c][r];
}
}
for (String[] row : rotate) {
System.out.println(Arrays.toString(row));
}
}
扔掉它,用仿射变换器。我看不出有什么明显的错误。这幅画是怎么乱码的?你确定问题不在于显示代码吗?我也是这么想的。但当我尝试调用rotate90两次时,它将产生正确的180度旋转。和调用它4次一样,它会产生相同的原始图像我不能肯定,但有一个模式,我不能用语言来描述它。下面是一个图片示例。vs当我尝试调用rotate90()两次时,它会为像素[][]的行生成rotated=new Pixel[iHeight][iWidth];实际上,我有意交换iHeight和iWidth的位置,因为原始图像数组是Image[iHeight][iWidth]。因此,当我尝试您的代码时,这将导致arrayoutofboundsexception@user1862452我在做假设,因为没有明确的解释,我改变了你交换的高度和宽度。试试看。你确定使用“rotated[x][y]=image[iWidth-y-1][x];”时,我将iWidth更改为iHeight,它停止给出-1错误。不幸的是,它仍在输出相同的乱码图像/用我的代码试试看。如果您有用于显示图像的代码,我们甚至可以看到它是否有问题。是的,我尝试使用您的方法,我的输出与我的相同,但iWidth除外,我用iHeight替换了iWidth
public static void main(String[] args) {
final int width = 5, height = 3;
String[][] values = new String[height][width];
for (int c = 0; c < width; c++) {
for (int r = 0; r < height; r++) {
values[r][c] = String.format("%3d", r * width + c);
}
}
for (String[] row : values) {
System.out.println(Arrays.toString(row));
}
System.out.println();
int nheight = width;
int nwidth = height;
String[][] rotate = new String[nheight][nwidth];
for (int c = 0; c < nwidth; c++) {
for (int r = 0; r < nheight; r++) {
rotate[r][c] = values[height - 1 - c][r];
}
}
for (String[] row : rotate) {
System.out.println(Arrays.toString(row));
}
}
[ 0, 1, 2, 3, 4]
[ 5, 6, 7, 8, 9]
[ 10, 11, 12, 13, 14]
[ 10, 5, 0]
[ 11, 6, 1]
[ 12, 7, 2]
[ 13, 8, 3]
[ 14, 9, 4]