gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 19:53:43 24 4
gpt4 key购买 nike

我有一个 BufferedImage i,我想得到 Color来自该图像的某个像素包括 alpha 值。使用 xy 坐标标识像素。

这是我尝试过的:

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

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

我做错了什么?

提前致谢

最佳答案

您正在使用的单参数 Color 构造函数会丢弃 alpha 信息。改用双参数版本并为 hasalpha 传入 true:

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

相关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.

关于java - bufferedImage.getRGB(x, y) 不产生 alpha,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10726594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com