【问题标题】:BufferedImage - Gray ScaleBufferedImage - 灰度
【发布时间】:2011-11-16 14:39:27
【问题描述】:

给定一张灰度的图像,我如何获得该位置的灰度像素值?

这始终将 temp 输出为 -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);
        }
    }
}

【问题讨论】:

    标签: java bufferedimage grayscale


    【解决方案1】:

    您正在创建一个新的空白图像并将其分配给您的类变量:

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

    像素是使用默认值创建的,并且在您打印它们的阶段它们都具有相同的颜色(黑色),因为您尚未在任何像素中操作颜色,因此它们是合乎逻辑的。

    此外,如果宽度不等于高度,您的代码可能会失败。根据您的 for 循环,i 沿宽度运行,j 沿高度运行。因此,你应该改变

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

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

    【讨论】:

      猜你喜欢
      • 2011-12-19
      • 2011-06-13
      • 1970-01-01
      • 2018-05-22
      • 2017-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多