19

Can anybody explain how to get an array of rgb value from a BufferedImage?

I have a grey scale image in a BufferedImage and need to extract an array of 0 to 255 values that describe the image.
I know the BufferedImage is correct because I can save it to PNG. However, if I use int[] dataBuffInt = ((DataBufferInt) heightMap.getDataBuffer()).getData(); I get a bunch of huge negative numbers.

I have searched for a while and seen some references to shifting some values (post) but don't really understand what they are saying.

Basically I want to go from a BufferedImage to an array of 0 to 255 RBG values.
I'm not sure I explained myself properly, plaese ask for more details is needed.

Edit:
@Garbage Thanks for the tip. I tried int[] dataBuffInt = heightMap.getRGB(0, 0, heightMap.getWidth(), heightMap.getHeight(), null, 0, heightMap.getWidth()); But get the same result.
@Greg Kopff The result is 2 and it was set to TYPE_INT_ARGB

Community
  • 1
  • 1
kotoko
  • 599
  • 2
  • 6
  • 22
  • What is the result of BufferedImage.getType()? – Greg Kopff Jun 04 '12 at 11:04
  • 3
    Did you try http://docs.oracle.com/javase/6/docs/api/java/awt/image/BufferedImage.html#getRGB%28int,%20int,%20int,%20int,%20int[],%20int,%20int%29 – Garbage Jun 04 '12 at 11:12
  • @Garbage Thanks for the tip. I tried int[] dataBuffInt = heightMap.getRGB(0, 0, heightMap.getWidth(), heightMap.getHeight(), null, 0, heightMap.getWidth()); But get the same result. – kotoko Jun 04 '12 at 14:15

1 Answers1

30

You get negative numbers since the int value you get from one of the pixels are composed by red, green, blue and alpha. You need to split the colors to get a value for each color component.

The simplest way to do this is to create a Color object and use the getRed, getGreen and getBlue (aswell as getAlpha) methods to get the components:

public static void main(String... args) throws Exception {

    BufferedImage image = ImageIO.read(
         new URL("http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png"));

    int w = image.getWidth();
    int h = image.getHeight();

    int[] dataBuffInt = image.getRGB(0, 0, w, h, null, 0, w); 

    Color c = new Color(dataBuffInt[100]);

    System.out.println(c.getRed());   // = (dataBuffInt[100] >> 16) & 0xFF
    System.out.println(c.getGreen()); // = (dataBuffInt[100] >> 8)  & 0xFF
    System.out.println(c.getBlue());  // = (dataBuffInt[100] >> 0)  & 0xFF
    System.out.println(c.getAlpha()); // = (dataBuffInt[100] >> 24) & 0xFF
}

Outputs:

173
73
82
255
dacwe
  • 43,066
  • 12
  • 116
  • 140
  • Thank you so much! Just to check, to find this for all the points in the image I have to do some loops, there is no other more efficient way, right? – kotoko Jun 04 '12 at 14:31
  • Changed the example to use data returned by `int[] getRGB(...)`. The data is returned in `ARGB` mode so you can extract the color components yourself but hey, always profile first instead of makeing premature optimizations. :) – dacwe Jun 04 '12 at 14:33
  • Added a "faster" way to get the same thing using bitfiddling! :) – dacwe Jun 04 '12 at 14:37
  • hi all, is the average rgb of the image obtained in the above code @kotoko – Ashish Augustine Feb 09 '14 at 06:42