在 Java 中更改 png 非透明部分的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16054596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:45:22  来源:igfitidea点击:

Change color of non-transparent parts of png in Java

javaimagecolorspngtransparency

提问by 4ndro1d

I am trying to automatically change the color for a set of icons. Every icon has a white filled layer and the other part is transparent. Here is an example: (in this case it's green, just to make it visible)

我正在尝试自动更改一组图标的颜色。每个图标都有一个白色填充层,另一部分是透明的。这是一个例子:(在这种情况下它是绿色的,只是为了让它可见)

icon search

图标搜索

I tried to do the following:

我尝试执行以下操作:

private static BufferedImage colorImage(BufferedImage image) {
        int width = image.getWidth();
        int height = image.getHeight();

        for (int xx = 0; xx < width; xx++) {
            for (int yy = 0; yy < height; yy++) {
                Color originalColor = new Color(image.getRGB(xx, yy));
                System.out.println(xx + "|" + yy + " color: " + originalColor.toString() + "alpha: "
                        + originalColor.getAlpha());
                if (originalColor.equals(Color.WHITE) && originalColor.getAlpha() == 255) {
                    image.setRGB(xx, yy, Color.BLUE.getRGB());
                }
            }
        }
        return image;
    }

The problem I have is that every pixel I get has the same value:

我的问题是我得到的每个像素都具有相同的值:

32|18 color: java.awt.Color[r=255,g=255,b=255]alpha: 255

So my result is just a colored square. How can I achieve to change the color of the non-transparent parts only? And why is it, that all pixels have even the same alpha value? I guess that's my main problem: That the alpha value isn't read correctly.

所以我的结果只是一个彩色方块。我怎样才能实现只改变非透明部分的颜色?为什么所有像素都具有相同的 alpha 值?我想这是我的主要问题:无法正确读取 alpha 值。

采纳答案by 4ndro1d

The problem is, that

问题是,那

Color originalColor = new Color(image.getRGB(xx, yy));

discards all the alpha values. Instead you have to use

丢弃所有的 alpha 值。相反,你必须使用

 Color originalColor = new Color(image.getRGB(xx, yy), true);

to keep alpha values available.

保持 alpha 值可用。

回答by MadProgrammer

Why it doesn't work, I don't know, this will.

为什么它不起作用,我不知道,这会。

This changes all the pixles to blue, maintaining their alpha values...

这会将所有像素更改为蓝色,保持它们的 alpha 值......

enter image description here

enter image description here

import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class TestColorReplace {

    public static void main(String[] args) {
        try {
            BufferedImage img = colorImage(ImageIO.read(new File("NWvnS.png")));
            ImageIO.write(img, "png", new File("Test.png"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private static BufferedImage colorImage(BufferedImage image) {
        int width = image.getWidth();
        int height = image.getHeight();
        WritableRaster raster = image.getRaster();

        for (int xx = 0; xx < width; xx++) {
            for (int yy = 0; yy < height; yy++) {
                int[] pixels = raster.getPixel(xx, yy, (int[]) null);
                pixels[0] = 0;
                pixels[1] = 0;
                pixels[2] = 255;
                raster.setPixel(xx, yy, pixels);
            }
        }
        return image;
    }
}