Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

java - How to Change ImageIcon's Black Pixels to White Pixels?

I know I could just change it in a photo manipulation software, but I want to learn to do it programmatically so I can change it to any color I wish.


First off, I'd like to say that I've been searching for the solution around two hours and I couldn't find one that works for me, or one that deals with my exact problem.

I've downloaded some icons from the internet and they're originally black with transparent background, which is good for menu bars and stuff. But, they're hard to notice on my tool bar and I want to change the black color on those icons to white color. Here's an edited screenshot of what I'm trying to achieve and here's a screenshot of what I achieve. (Sorry for links, I need at least 10 reputation to post images.)

Here's my Utility class that's responsible for the failed work:

public final class Utility{
    public static ImageIcon replaceIconColor(ImageIcon icon, Color oldColor, Color newColor){
        BufferedImage image = iconToImage(icon);

        for(int y = 0; y < image.getHeight(); y++){
            for(int x = 0; x < image.getWidth(); x++){
                Color pixel = new Color(image.getRGB(x, y));
                if((pixel.getRed() == oldColor.getRed()) && (pixel.getGreen() == oldColor.getGreen()) && (pixel.getBlue() == oldColor.getBlue()) && (pixel.getAlpha() == oldColor.getAlpha())){
                    image.setRGB(x, y, newColor.getRGB());
                }
            }
        }

        return new ImageIcon(image);
    }

    public static BufferedImage iconToImage(ImageIcon icon){
        return Resources.loadImage(icon.getDescription());
    }
}

I'm not sure if you need resource loading class code, but I thought it could only help you to understand my problem fully and to be able to help me the best of your abilty. So, here's my Resources class code snippet:

public static ImageIcon loadImageIcon(String fileName){
    URL imageURL = Resources.class.getResource("/Resources/Images/" + fileName);
    ImageIcon imageIcon = new ImageIcon(imageURL);
    imageIcon.setDescription(fileName);
    return imageIcon;
}

public static BufferedImage loadImage(String fileName){
    URL imageURL = Resources.class.getResource("/Resources/Images/" + fileName);
    BufferedImage image = null;

    try{
        image = ImageIO.read(imageURL);
    }catch(IOException e){
        e.printStackTrace();
    }

    return image;
}

My apologies if there actually is a solution somewhere on the internet for this, but I couldn't find it. Well, that's all. I think I was specific enough.

Thank you in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Two approaches are common:

  • Loop through the BufferedImage using getRGB() and setRGB() as needed, for example.

  • Use a LookupOp, as shown in the examples cited here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...