给定灰度中的图像,如何获得该位置灰度的像素值?
它始终以-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);
}
}
}发布于 2011-09-14 00:55:58
您正在创建一个新的空白映像并将其分配给您的类变量:
imgMazeImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);这些像素是用默认值创建的,其逻辑是,它们在打印时都具有相同的颜色(黑色),因为您还没有在任何像素中操作该颜色。
此外,如果宽度不等于高度,则代码可能会失败。根据您的for循环,我沿宽度运行,j沿高度运行。因此,你应该改变
int temp = imgMazeImage.getRGB(j, i);至
int temp = imgMazeImage.getRGB(i, j);https://stackoverflow.com/questions/7409796
复制相似问题