gpt4 book ai didi

java - 无法从各个 BufferedImage 像素获取正确的 alpha 值

转载 作者:行者123 更新时间:2023-12-01 22:31:50 25 4
gpt4 key购买 nike

我需要获取 RGBA 值,修改 RGB,然后再次设置 RGB(和 A)值。

但是,它似乎为所有像素返回 255,即使我知道我正在加载的图像中包含透明像素。

这是一个 SSCCE

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class TestImage {

public static void main(String args[]) {
try {
BufferedImage image = ImageIO.read(new File("/home/affy/Pictures/salamenceavatar.png"));

BufferedImage buff = new BufferedImage(
image.getWidth(),
image.getHeight(),
BufferedImage.TYPE_INT_ARGB
);

Graphics2D g = buff.createGraphics();
g.drawImage(image, 0, 0, null);

for(int x = 0; x < image.getWidth(); x++) {
for(int y = 0; y < image.getHeight(); y++) {
Color c = new Color(buff.getRGB(x, y));
int alpha = c.getAlpha();
System.out.println(alpha);
}
}
}catch (IOException e) {
e.printStackTrace();
}
}
}

最佳答案

使用Color(int, boolean)相反,传递 true 告诉它您想要从提供的打包 int

中提取 alpha 值
Color c = new Color(buff.getRGB(x, y), true);

来自 JavaDocs...

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. If the hasalpha argument is false, alpha is defaulted to 255.

Parameters:

rgba - the combined RGBA components
hasalpha - true if the alpha bits are valid; false otherwise

关于java - 无法从各个 BufferedImage 像素获取正确的 alpha 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27611884/

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