我目前在读取 PNG 文件时遇到了 alpha 通道问题 ImageIO.read(...)
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(...)返回 aBufferedImage而不提供指定图像类型的选项。那么我该怎么做呢?任何帮助深表感谢。
提前致谢
墨色风雨
拉莫斯之舞
相关分类