Java getRGB无法读取正确的像素

时间:2018-10-02 14:10:22

标签: java

我只想读取基本2x2像素 png文件的RGB值,但是它返回了错误的RGB值。例如,x = 2y = 2处的像素为黑色,因此它应返回0,0,0,但它以RGB值返回白色,因此我需要将图像尺寸本身扩展到更高的分辨率而不是2x2,否则它将无法识别x = 1y = 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);

    }
}

0 个答案:

没有答案