java - 使用 ImageIO.write 将 BufferedImage 保存为 PNG 文件后,颜色发生奇怪变化

标签 java image colors png

我一直在尝试制作一个Java程序,允许将BufferedImage中的某些像素颜色更改为其他颜色,但是正在绘制的颜色似乎覆盖在旧的颜色上那些。这就是我的意思:

Combined image of input.png and output.png

这是目前 .java 文件中的代码:

static BufferedImage image = null;
static File file = null;
static int height;
static int width;
static int[][][] pixelStorage;
static int pixel;
static int getPixelDataOutput = 0;
static Random random = new Random();

public static void main(String args[]) throws IOException {

    System.out.println("test");

    try {
        file = new File("C:\\Users\\kkosy\\Dev\\Java\\Random\\Images - Color Changer\\Images\\input.png");
    }catch(Exception exception) {
        System.out.println(exception);
    }

    image = ImageIO.read(file);

    height = image.getHeight();
    width = image.getWidth();

    pixelStorage = new int[height][width][4];

    for(int h = 0; h < height - 1; h++) {
        for(int w = 0; w < width - 1; w++) {
            savePixelData(w, h);
            if(getPixelData(w,h,"r") == 0){
                System.out.println();
                printPixelData(w,h,"Before -- ");
                setPixelData(w,h,255,0,0,255);
                printPixelData(w,h,"After -- ");
            }
        }
    }

    try {
        file = new File("C:\\Users\\kkosy\\Dev\\Java\\Random\\Images - Color Changer\\Images\\output.png");
        ImageIO.write(image, "jpg", file);
    }catch(Exception exception){
        System.out.println(exception);
    }

}

private static void savePixelData(int x, int y) {
    pixel = image.getRGB(x,y);
    pixelStorage[y][x][0] = (pixel >> 24) & 0xff;
    pixelStorage[y][x][1] = (pixel >> 16) & 0xff;
    pixelStorage[y][x][2] = (pixel >> 8) & 0xff;
    pixelStorage[y][x][3] = pixel & 0xff;
    //printPixelData(x,y,"");

}

private static void setPixelData(int x, int y, int alpha, int red, int green, int blue) {
    int setPixel = (alpha << 24) | (red << 16) | (green << 8) | (blue);
    image.setRGB(x, y, setPixel);
    image.setRGB(x, y, new Color(red,green,blue).getRGB());
}

private static void printPixelData(int x, int y, String arguments) {
    System.out.println(arguments + "" + pixelStorage[y][x][0] + " " + pixelStorage[y][x][1] + " " + pixelStorage[y][x][2] + " " + pixelStorage[y][x][3] + " ");
}

private static int getPixelData(int x, int y, String argb) {
    switch(argb) {
    case "a": {
        getPixelDataOutput = pixelStorage[y][x][0];
        break;
    }
    case "r": {
        getPixelDataOutput = pixelStorage[y][x][1];
        break;
    }
    case "g": {
        getPixelDataOutput = pixelStorage[y][x][2];
        break;
    }
    case "b": {
        getPixelDataOutput = pixelStorage[y][x][3];
        break;
    }
    }
    return getPixelDataOutput;
}

我不知道为什么它会输出这样的图像。也许是 setRGB() 或类似的东西。

最佳答案

您正在尝试创建 PNG 文件,但是这一行...

ImageIO.write(image, "jpg", file);

...正在尝试写入 JPEG。 根据 Unix file 命令,输出确实是 JPEG... 还有深青色像素的不良情况。

当我将 "jpg" 更改为 "png" 时,我得到了一个 output.png 文件,它看起来与 完全相同>input.png.

我不知道您想要输出的样子 (我正在使用 different input file ) 但这似乎比颜色错误的 JPEG-in-PNG 衣服更接近。

<小时/>

您的日志对于调试来说毫无用处,因为 printPixelData 始终打印 pixelStorage 中的值。 由于 setPixelData 仅更改 image 中的值,因此您始终会打印“之前”值两次,而不会打印“之后”值。

顺便说一句,所有这些静态变量使得跟踪程序的执行更加困难。 至少将你能做到的转移到方法中 (例如 heightwidthfile,它们从未在 main 之外使用), 并删除那些你根本不使用的 (如getPixelDataOutputrandom)。

<小时/>

可能不相关,但 setPixelData 方法设置一个像素,然后立即将其重置为其他值:

private static void setPixelData(
  int x, int y,
  int alpha, int red, int green, int blue
) {
    int setPixel = (alpha << 24) | (red << 16) | (green << 8) | (blue);
    image.setRGB(x, y, setPixel);
    // Why overwrite the value you just set?
    image.setRGB(x, y, new Color(red,green,blue).getRGB());
}

这似乎并没有改变任何事情—— (无论是否第一次调用 setRGB,我的测试图像看起来都一样 --- 但这可能不是您想要的。

关于java - 使用 ImageIO.write 将 BufferedImage 保存为 PNG 文件后,颜色发生奇怪变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45112192/

相关文章:

c++ - 如何访问 win32 缩略图的图像数据

java - 尝试使用字符数组解密字符串(Polybius Square)

java - "(a << 24 | b << 16 | c << 8 | d)"是什么意思?

python - Pygame随机显示多个​​图像

r - ggplot2 和 RcolorBrewer 具有 2 个值的定性调色板

java - 使用paintComponent以颜色为参数绘制图形

wpf - 如何将 System.Windows.Media.Color 对象序列化为 sRGB 字符串?

java Processbuilder - 执行不在 OS X 路径中的文件

java - 如何在 Pattern.compile ("\\R").splitAsStream(input) 中包含尾随空字符串?

python - 如何使用 Scikit-Image 库从 Python 中的 RGB 图像中提取绿色 channel ?