2011-09-13 44 views
3

鑑於灰度中的圖像,我將如何獲取該位置的灰度像素值?BufferedImage - Gray Scale

此輸出溫度始終爲-16777216(黑色)。

public void testMethod() 
{ 
    int width = imgMazeImage.getWidth(); 
    int height = imgMazeImage.getHeight(); 

    //Assign class variable as a new image with RGB formatting 
    imgMazeImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); 
    for(int i=0; i < width; i ++){ 
     for(int j=0; j < height; j++) 
     { 
      //Grab and set the colors one-by-one 
      inttemp = imgMazeImage.getRGB(j, i); 
      System.out.println(temp); 
     } 
    } 
} 

回答

2

要創建一個新的空白圖像,並將其分配給您的類變量:

imgMazeImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); 

的像素與默認值創建,其合乎邏輯的,它們都具有相同的顏色(黑色)在你打印它們的階段,因爲你還沒有操縱任何像素的顏色。

此外,如果寬度不等於高度,代碼可能會失敗。根據你的循環,我沿着寬度運行,並沿着高度運行。因此,你應該改變

int temp = imgMazeImage.getRGB(j, i); 

int temp = imgMazeImage.getRGB(i, j);