我目前在用ImageIO.read(...)
读取PNG文件时遇到了一个alpha通道的问题
fileInputStream = new FileInputStream(path);
BufferedImage image = ImageIO.read(fileInputStream);
//Just copying data into an integer array
int[] pixels = new int[image.getWidth() * image.getHeight()];
image.getRGB(0, 0, width, height, pixels, 0, width);
但是,当按如下所示逐位移位尝试从像素数组中读取值时,alpha通道始终返回-1
int a = (pixels[i] & 0xff000000) >> 24;
int r = (pixels[i] & 0xff0000) >> 16;
int g = (pixels[i] & 0xff00) >> 8;
int b = (pixels[i] & 0xff);
//a = -1, the other channels are fine
通过谷歌搜索这个问题,我了解到BufferedImage
类型需要定义如下以允许alpha通道工作:
BufferedImage image = new BufferedImage(width, height BufferedImage.TYPE_INT_ARGB);
但是ImageIO.read(...)
返回一个BufferedImage
,没有提供指定图像类型的选项。那么我该怎么做呢?任何帮助都是非常感谢的。
提前感谢
发布于 2018-10-23 06:39:31
我认为问题在于你使用的是算术移位(>>
)而不是逻辑移位(>>>
)。因此,0xff000000 >> 24
变成了0xffffffff
(即-1
)
https://stackoverflow.com/questions/52938352
复制相似问题