Java getRGB doesnt read correct pixels

0

I just want to read the RGB values of a basic 2x2 pixels png file, but it returns the wrong RGB values. For example, the pixel at x = 2 and y = 2 was black, so it should return 0,0,0, but it returns white as RGB values and i need to extend the image size itself to a higher resolution than 2x2, otherwise it wont recognize any pixels except x = 1 and y = 1

public class Image
{
    public static void main(String[] args)
    {

        BufferedImage img = null;
        File f = null;

        //read image
        try{
          f = new File("image.png");
          img = ImageIO.read(f);
        }catch(IOException e){
          System.out.println(e);
        }

        int x = 2;
        int y = 2;

        int clr = img.getRGB(x, y);
        int red = (clr & 0x00ff0000) >> 16;
        int green = (clr & 0x0000ff00) >> 8;
        int blue = clr & 0x000000ff;
        System.out.println("Red Color value = " + red);
        System.out.println("Green Color value = " + green);
        System.out.println("Blue Color value = " + blue);

    }
}
java
asked on Stack Overflow Oct 2, 2018 by Alzameista • edited Oct 2, 2018 by Praveen

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0