简体   繁体   English

如何更改Java缓冲图像的颜色

[英]How to change colors of a Java Buffered Image

I have a JavaBufferedImage. 我有一个JavaBufferedImage。 The foreground is Black and the background is transparent. 前景是黑色,背景是透明的。 I would like to recolor the image as Red. 我想将图像重新着色为红色。

I have read through other people's postings on this and tried using this code, but my Image winds up completey transparent when I run it. 我已经阅读了其他人的相关文章,并尝试使用此代码,但是运行该图像时,它的图像完全透明。

Does anyone have any ideas? 有人有什么想法吗? I am new to the Java 2D Image processing library. 我是Java 2D图像处理库的新手。 Thanks. 谢谢。

    imageIcon= new ImageIcon(getImageURL("/ImagesGun/GunBase.png"));
    gunBaseImage= Utilities.toBufferedImage(imageIcon.getImage());

    int red = 0x00ff0000;
    int green = 0x0000ff00;
    int blue = 0x000000ff;

    int width = gunBaseImage.getWidth();
    int height = gunBaseImage.getHeight();

    //Loop through the image and set the color to red
    for(int x = 0; x < width; x++){
        for(int y = 0; y < height; y++){
            long pixel = gunBaseImage.getRGB(x, y);
            if(pixel != 0){
                red = 0x00ff0000;

                gunBaseImage.setRGB(x,y,red);
            }

        }
    }

You are using a fully transparent value of red. 您正在使用完全透明的红色值。 The first entry in the color definition is the alpha value. 颜色定义中的第一项是alpha值。 If you want a fully opaque color you need to use ff as the first value. 如果要使用完全不透明的颜色,则需要使用ff作为第一个值。 Therefore your red should be 0xffff0000, your green 0xff00ff00 and so on. 因此,您的红色应该是0xffff0000,绿色应该是0xff00ff00,依此类推。 This also means that black is ff000000. 这也意味着黑色是ff000000。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM