-2

I am a bit confused exactly what value will be stored in the variable a. Please can someone explain me with an example

Thanks in advance.

Omkar
  • 45
  • 7
  • What part of the [Javadoc](https://docs.oracle.com/en/java/javase/15/docs/api/java.desktop/java/awt/image/BufferedImage.html#getRGB(int,int)) is not clear? – Jim Garrison May 04 '21 at 03:57
  • 1
    Did you see this stackoverflow answer? [Understanding Buffered Image](https://stackoverflow.com/questions/25761438/understanding-bufferedimage-getrgb-output-values) – Bishal Gautam May 04 '21 at 03:57
  • 1
    As per the doccumentation `Returns: an integer pixel in the default RGB color model and default sRGB colorspace.` Source: https://docs.oracle.com/javase/8/docs/api/java/awt/image/BufferedImage.html#getRGB-int-int- Try it and you will see what the result is. – sorifiend May 04 '21 at 03:57

1 Answers1

1

As BufferedImage.getRGB() docs says:

Returns an integer pixel in the default RGB color model (TYPE_INT_ARGB) and default sRGB colorspace. Color conversion takes place if this default model does not match the image ColorModel. There are only 8-bits of precision for each color component in the returned data when using this method.

To divide this int to a R,G,B values you can use bitwise operations:

BufferedImage bufferedImage = new BufferedImage(10, 10, 10);
int a = bufferedImage.getRGB(0, 0);

int red = (a >> 16) & 255;
int green = (a >> 8) & 255;
int blue = (a) & 255;
System.out.println(a + " r:" + red + ", g:" + green + ", b:" + blue);