How to get the array of pixel values for an image in java using getRGB

帅比萌擦擦* 提交于 2019-12-12 03:11:27

问题


I'm new to image processing in java. Actually what I'm trying to do is to save all the pixel values of an image into an array rgbArray[], and the problem is that I'm getting the same values in all the indexes of an array i.e. all the indexes of the array have the same value. A part of code is given below:

int[] rgbArray=new int[w*h];     // Array to store the Pixel values
BufferedImage buffer = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 
buffer.getRGB(0, 0, w, h, rgbArray, 0, w );
for(int i=0;i<w*h;i++)
 {
  System.out.println("rgbArray["+i+"] = "+ rgbArray[i]);
 }

The output which i'm getting is -16777216 for all indexes in rgbArray. How is it possible to have the same value for each and every pixel of the image? And how do I get the correct pixel value??


回答1:


Since you do not provide any values for the BufferedImage, every pixel defaults to alpha = 255, red = 0, green = 0, and blue = 0; Put all of those into 1 int and you get -16777216. I got this from:

int val = buffer.getRGB(5, 23);
int a = (0xff000000 & val) >>> 24;
int r = (0x00ff0000 & val) >> 16;
int g = (0x0000ff00 & val) >> 8;
int b = (0x000000ff & val);
System.out.println("a " + a + " r " + r + " g " + g + " b " + b);

Which produces a 255 r 0 g 0 b 0.



来源:https://stackoverflow.com/questions/8856569/how-to-get-the-array-of-pixel-values-for-an-image-in-java-using-getrgb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!