Java - 将缓冲图像保存到旋转 90 度的文件时出现问题

Java - Problem with saving buffered image to file rotates 90 degrees

我一直在尝试完成一种将 Color[][] 保存为 jpg 图像文件的方法,但是该解决方案会导致输出文件旋转 90 度,我已经尝试查找问题但确实如此对我来说不是很明显,其他有类似解决方案的人似乎也没有同样的问题。

非常感谢任何帮助!

private Color[][] image;  //  assume this field has already been populated

public  void saveImage() {
    BufferedImage saveImage = new BufferedImage(this.image.length, 
                                                this.image[0].length, 
                                                BufferedImage.TYPE_INT_RGB);
    for (int row = 0; row < this.image.length; row++) {
        for (int col = 0; col < this.image[row].length; col++) {
            saveImage.setRGB(row, col, this.image[row][col].getRGB());
        }
    }

    String fName = UIFileChooser.save();
    if (fName==null){return;}

    File toFile = new File(fName+".jpg");

    try {
        ImageIO.write(saveImage,"jpg", toFile);
    }catch (IOException e){UI.println("File save error: "+e);}
}

感谢帮忙,原来我只需要翻转尺寸和x/y坐标,下面是固定版本:

private Color[][] image;  //  assume this field has already been populated

public  void saveImage() {
    BufferedImage saveImage = new BufferedImage(this.image[0].length, 
                                                this.image.length, 
                                                BufferedImage.TYPE_INT_RGB);
    for (int row = 0; row < this.image.length; row++) {
        for (int col = 0; col < this.image[row].length; col++) {
            saveImage.setRGB(col, row, this.image[row][col].getRGB());
        }
    }

    String fName = UIFileChooser.save();
    if (fName==null){return;}

    File toFile = new File(fName+".jpg");

    try {
        ImageIO.write(saveImage,"jpg", toFile);
    }catch (IOException e){UI.println("File save error: "+e);}
}

BufferedImage.setRGB方法的签名是public void setRGB​(int x, int y, int rgb)

X 在前,然后是 Y。row 等于 ycol 等于 x

因此将该行更改为:

saveImage.setRGB(col, row, this.image[row][col].getRGB());