[英]Get 0-255 values from DataBufferByte.getData for each color
我正在尝试使用以下代码分析像素
BufferedImage image;
try {
image = ImageIO.read(new File(fileName));
} catch (IOException e) {
e.printStackTrace();
return null;
}
final byte[] pixels = ((DataBufferByte)
image.getRaster().getDataBuffer()).getData();final int pixelLength = 4;
for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) {
byte alpha = pixels[pixel];
byte blue = pixels[pixel + 1];
byte green = pixels[pixel + 2];
byte red = pixels[pixel + 3];
//analyze colors
col++;
if (col == width) {
col = 0;
row++;
}
}
我最简单的例子我有一个有两个像素的图像,一个有透明度,一个没有。
像素1:红色,透明度
HEX: FF0000, Alpha 140, (RGB: 255, 0, 0)
像素2:没有透明度的绿松石
HEX: 7FFFD8, Aplha 255, (RGB: 127, 255, 216)
上面的像素数组具有以下值: [-116, 0, 0, -1, -1, -40, -1, 127]
其中每个像素具有a,b,g,r(I的顺序)的四个字节认为)。
所以我的问题是,如何从上面的结果得到我用过的0-255个值:)
我不确定我是否理解了这个问题......但根据我的理解,这是一个简单的答案。
在不讨论两个补码的情况下 ,您可以浏览整个数组并使用以下规则进行转换:
这有帮助吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.