简体   繁体   English

使用RGBA中的Alpha来使图像不透明

[英]Make image's opacity using alpha in RGBA

I tried to make a simple program which load an image, make it blue effect, and also make it half transperant. 我试图制作一个简单的程序来加载图像,使其具有蓝色效果,并使其半透明。 I'm doing it by running on the image pixels and change the RGB's blue value and alpha value. 我通过在图像像素上运行并更改RGB的蓝色值和alpha值来实现。 I successed to make a nice blue effect to the image. 我成功为图像制作了漂亮的蓝色效果。 But I couldn't manage to change the image opacity. 但是我无法改变图像的不透明度。 It seems that no matter how I change the alpha value of the pixels, it doesn't effect the image. 似乎无论我如何更改像素的alpha值,它都不会影响图像。

Here is my code: 这是我的代码:

try {
    image1 = ImageIO.read(new File("image1.png"));
} catch(IOException e) {e.printStackTrace();}


for(int x=0;x<image1.getWidth();x++) {
    for(int y=0;y<image1.getHeight();y++) {
        int rgb = image1.getRGB(x, y);
        Color color = new Color(rgb);
        int r = color.getRed();
        int g = color.getGreen();
        int b = color.getBlue();
        int a = color.getAlpha();

        System.out.println(a);
        a= 100;
        if(b<155)
            b+=100;
        else
            b=255;

        color = new Color(r,g,b,a);

        image1.setRGB(x, y, color.getRGB());
    }
}

UPDATE: I also tried this. 更新:我也尝试过。 still doesn't work: 仍然不起作用:

for(int x=0;x<image1.getWidth();x++) {
    for(int y=0;y<image1.getHeight();y++) {
        int rgb = image1.getRGB(x, y);
        Color color = new Color(rgb,true);
        int r = color.getRed();
        int g = color.getGreen();
        int b = color.getBlue();
        int a = color.getAlpha();

        a= 100;
        if(b<155)
            b+=100;
        else
            b=255;


        rgb = rgb | b;
        rgb = rgb & 0x33ffffff;


        image1.setRGB(x, y, rgb);
    }
}

Use an AlphaComposite : 使用AlphaComposite

BufferedImage img = //some code here
BufferedImage imgClone = //some clone of img, but let its type be BufferedImage.TYPE_INT_ARGB
Graphics2D imgCloneG = imgClone.createGraphics();
imgCloneG.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_IN, 0.5f));
imgCloneG.drawImage(img, 0, 0, null);
//imgClone is now img at half alpha

imgClone can be made like this: imgClone可以这样制作:

...
imgClone = new BufferedImage(img.getWidth(), img.getHeight(), 
                             BufferedImage.TYPE_INT_ARGB);
Graphics2D imgCloneG = imgClone.createGraphics();
imgCloneG.drawImage(img, 0, 0, null);
imgCloneG.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_IN, 0.5f));
...

The Color constructor that takes only an int creates an opaque color (with alpha 255). 仅采用intColor构造函数将创建不透明的颜色(alpha 255)。 When you take out the alpha again with getAlpha() , it's always 255 here. 当您再次使用getAlpha()取出alpha时,此处始终为255。

Relevant Java Color code, from line 413 (at least in Java 1.7): 来自第413行的相关Java Color代码(至少在Java 1.7中):

public Color(int rgb) {
    value = 0xff000000 | rgb;
}

The most significant 8 bytes are set, overwriting whatever alpha component was there. 设置了最高8个字节,覆盖那里的任何alpha分量。

To preserve the alpha component, you need to use the Color constructor that takes an int for the values and a boolean for whether it has an alpha component. 为了保留alpha分量,您需要使用Color构造函数 ,该构造函数的值采用int boolean表示是否具有alpha分量。

Color color = new Color(rgb, true);

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

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