[英]PixelGrabber.getPixels returns values out of the range 0-255
I need to load an image, and get the values of the pixels of the image across breadth and width. 我需要加载图像,并获取图像像素的宽度和宽度值。 I know I need to use PixelGrabber and Image but I'm unsure of how to do it.
我知道我需要使用PixelGrabber和Image,但是我不确定该怎么做。 My code till now.
到目前为止,我的代码。 (Assuming all the required libraries are imported and this in the try part)
(假设所有必需的库都已导入,并且在try部分中)
File Image1 = new File("i1.jpg");
img1 = ImageIO.read(Image1);
int img1w, img1h;
PixelGrabber grabimg1 = new PixelGrabber(img1, 0, 0, -1, -1, false);
if (grabimg1.grabPixels())
{
img1w = grabimg1.getWidth();
img1h = grabimg1.getHeight();
int[] data = (int[]) grabimg1.getPixels();
for (int i= 0; i < img1h; i++)
{
for (int j= 0; j < img1w; j++)
{
System.out.println(data[i*img1w + j]);
}
System.out.println("\n");
}
}
Printing this prints out values from -1 to -16777216 , while I would like values from 0 - 255. Would be thankful for any help. 打印此命令会打印出从-1到-16777216的值,而我希望从0-255的值。感谢您的帮助。
The JavaDoc for PixelGrabber contains a way to convert between int and the actual RGB values: 用于PixelGrabber的JavaDoc包含一种在int和实际RGB值之间转换的方法:
int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel ) & 0xff;
Alternatively you can use the ColorModel as returned by getColorModel()
. 另外,您可以使用
getColorModel()
返回的ColorModel。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.