2011-04-22 64 views
2

我有一個以PNG格式編碼的位圖。如何確定黑色在哪裏是白色?BufferImage:如何確定像素顏色

  final int[] p = new int[1]; 
      int iter = 0; 
      for (int i = 0; i < image.getWidth(); i++) { 
       for (int j = 0; j < image.getHeight(); i++) { 
        pixels[iter] = image.getData().getPixel(i, j, p)[0]; 
        iter++; 
       } 
      } 

返回我總是每個像素1。

回答

0
BufferedImage image = ImageIO.read(file); 
// Getting pixel color by position x=100 and y=40 
int clr= image.getRGB(100,40); 
int red = (clr & 0x00ff0000) >> 16; 
int green = (clr & 0x0000ff00) >> 8; 
int blue = clr & 0x000000ff; 

紅色,綠色和藍色時,像素的RGB值在x = 100且y = 40

0

根據圖像的編碼方式,像素被打包成一個整數Sa,例如BufferedImage.TYPE_4BYTE_ABGR,高位字節是alpha,接着是藍色,綠色,然後是紅色。