如何将ImageIcon的黑色像素更改为白色像素? [英] How to Change ImageIcon's Black Pixels to White Pixels?

查看:243
本文介绍了如何将ImageIcon的黑色像素更改为白色像素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以在照片处理软件中更改它,但我想学习以编程方式做,所以我可以更改为任何颜色我希望。






首先,我想说,我一直在寻找解决方案大约两个小时,我找不到一个为我工作,或一个处理我的确切问题。



我从互联网上下载了一些图标,他们原本是黑色的透明背景,这是很好的菜单栏和东西。但是,他们很难注意到我的工具栏,我想把这些图标上的黑色改为白色。以下是我要实现的屏幕截图这里是我实现的屏幕截图。 (



这是我的Utility类,负责失败的工作: / p>

  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());
}
}



我不知道是否需要资源加载类代码,但我认为它只能帮助你充分了解我的问题,并能够帮助我最好的你的能力。所以,这里是我的资源类代码片段:

  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;
}

我的道歉,如果实际上在互联网上有一个解决方案,我找不到它。好吧,这就是。


解决方案

两种方法是常见的:




  • 循环通过 BufferedImage 根据需要使用 getRGB() setRGB() ://stackoverflow.com/a/11556441/230513>示例。


  • 使用 LookupOp 此处的示例中。



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!

解决方案

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.

这篇关于如何将ImageIcon的黑色像素更改为白色像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆