Search code examples
javabufferedimagejavax.imageio

What will be the value of int a = bufferedImageSource.getRGB(x,y)?


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.


Solution

  • 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);