java bufferedImage.getRGB(x, y) 不产生 alpha

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10726594/
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 02:16:31  来源:igfitidea点击:

bufferedImage.getRGB(x, y) does not yield alpha

javacolorsrgbbufferedimage

提问by Rapti

I've got a BufferedImage iand I'd like to get the Colorfrom a certain pixel of that imageincluding the alpha value. The pixel is identified using xand ycoordinates.

我有一个 BufferedImage i,我想从包括 alpha 值在内的某个像素中获取颜色。使用和坐标标识像素。imagexy

Here's what I tried:

这是我尝试过的:

Color c = new Color(i.getRGB(x, y));

For some reason, the new color object contains the correct RGB but the alpha gets lost.

出于某种原因,新的颜色对象包含正确的 RGB,但 alpha 丢失了。

What am I doing wrong?

我究竟做错了什么?

Thanks in advance

提前致谢

回答by matts

The single-parameter Colorconstructor you're using discards alpha information. Use the two-parameter version instead and pass in truefor hasalpha:

Color您使用的单参数构造函数会丢弃 alpha 信息。改用两个参数版本并传入truefor hasalpha

Color c = new Color(i.getRGB(x, y), true);

The relevant Javadoc:

相关的Javadoc

Color(int rgb)

Creates an opaque sRGB color with the specified combined RGB value consisting of the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7.

Color(int rgba, boolean hasalpha)

Creates an sRGB color with the specified combined RGBA value consisting of the alpha component in bits 24-31, the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7.

Color(int rgb)

使用指定的组合 RGB 值创建不透明的 sRGB 颜色,该值由位 16-23 中的红色分量、位 8-15 中的绿色分量和位 0-7 中的蓝色分量组成。

Color(int rgba, boolean hasalpha)

使用指定的组合 RGBA 值创建 sRGB 颜色,该值由位 24-31 中的 alpha 分量、位 16-23 中的红色分量、位 8-15 中的绿色分量和位 0-7 中的蓝色分量组成。

回答by Basilio German

The Alpha is not lost.

阿尔法没有丢失。

You must use either the (int,boolean) constructor which takes the pixel information and specify if it has alpha values with the boolean or the constructor that takes 4 values, Red,Green,Blue, and Alpha.

您必须使用 (int,boolean) 构造函数来获取像素信息并指定它是否具有布尔值的 alpha 值,或者使用 4 个值(红色、绿色、蓝色和 Alpha)的构造函数。

the (int,int,int,int) constructor information from the JavaDoc

来自JavaDoc的 (int,int,int,int) 构造函数信息

To shorten code, you can always replace all of the following code with Color color = new Color(i.getRGB(x, y), true);which tells the color constructor that the pixel information does contain Alpha value.

为了缩短代码,您始终可以替换以下所有代码,Color color = new Color(i.getRGB(x, y), true);这些代码 告诉颜色构造函数像素信息确实包含 Alpha 值。

the (int,boolean) constructor information from the JavaDoc

来自JavaDoc的 (int,boolean) 构造函数信息

Note that sometimes when retrieving alpha the following way might return -1, in which case this means it loops back to 255, so you must perform 256-1 to get the actual alpha value. this snippet demonstrates how to load an image and get the color of that image on certain coordinates, in this case, 0,0. The following example can show you how to retrieve each color value including alpha, and reconstructing it to a new Color.

请注意,有时在检索 alpha 时,以下方式可能会返回 -1,在这种情况下,这意味着它会循环回 255,因此您必须执行 256-1 才能获得实际的 alpha 值。此代码段演示了如何加载图像并在特定坐标(在本例中为 0,0)上获取该图像的颜色。以下示例可以向您展示如何检索包括 alpha 在内的每个颜色值,并将其重建为新的颜色。

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;

public class main {

    public static void main(String [] args){
        BufferedImage image = null;
        try {
            image = ImageIO.read(new URL("image.png"));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        int pixel = image.getRGB(0, 0);

        int alpha = (pixel >> 24) & 0xff;
        int red = (pixel >> 16) & 0xff;
        int green = (pixel >> 8) & 0xff;
        int blue = (pixel >> 0) & 0xff;

        Color color1 = new Color(red, green, blue, alpha);

        //Or use
        Color color2 = new Color(image .getRGB(0, 0), true);

    }
   }

BufferedImages getRGB(int,int) JavaDocas you can see as it says:

BufferedImages getRGB(int,int) JavaDoc,正如您所看到的:

Returns an integer pixel in the default RGB color model (TYPE_INT_ARGB) and default sRGB colorspace. Color conversion takes place if this default model does not match the image ColorModel. There are only 8-bits of precision for each color component in the returned data when using this method.

Returns an integer pixel in the default RGB color model (TYPE_INT_ARGB) and default sRGB colorspace. Color conversion takes place if this default model does not match the image ColorModel. There are only 8-bits of precision for each color component in the returned data when using this method.

which means the Alpha value is also retrieved.

这意味着也会检索 Alpha 值。