5

类似于以下内容......除了让它工作:

public void seeBMPImage(String BMPFileName) throws IOException {
BufferedImage image = ImageIO.read(getClass().getResource(BMPFileName));

    int[][] array2D = new int[66][66];

for (int xPixel = 0; xPixel < array2D.length; xPixel++)
    {
        for (int yPixel = 0; yPixel < array2D[xPixel].length; yPixel++)
        {
            int color = image.getRGB(xPixel, yPixel);
            if ((color >> 23) == 1) {
                array2D[xPixel][yPixel] = 1;
            } else {
                array2D[xPixel][yPixel] = 1;
            }
        }
    }
}
4

2 回答 2

5

我会用这个:

public void seeBMPImage(String BMPFileName) throws IOException {
    BufferedImage image = ImageIO.read(getClass().getResource(BMPFileName));

    int[][] array2D = new int[image.getWidth()][image.getHeight()];

    for (int xPixel = 0; xPixel < image.getWidth(); xPixel++)
        {
            for (int yPixel = 0; yPixel < image.getHeight(); yPixel++)
            {
                int color = image.getRGB(xPixel, yPixel);
                if (color==Color.BLACK.getRGB()) {
                    array2D[xPixel][yPixel] = 1;
                } else {
                    array2D[xPixel][yPixel] = 0; // ?
                }
            }
        }
    }

它向您隐藏了 RGB 的所有细节,并且更易于理解。

于 2013-06-10T00:15:19.290 回答
0

mmirwaldt的代码已经在正确的轨道上。

但是,如果您希望数组直观地表示图像:

public void seeBMPImage(String BMPFileName) throws IOException {
    BufferedImage image = ImageIO.read(getClass().getResource(BMPFileName));

    int[][] array2D = new int[image.getHeight()][image.getWidth()]; //*

    for (int xPixel = 0; xPixel < image.getHeight(); xPixel++) //*
    {
        for (int yPixel = 0; yPixel < image.getWidth(); yPixel++) //*
        {
            int color = image.getRGB(yPixel, xPixel); //*
            if (color==Color.BLACK.getRGB()) {
                array2D[xPixel][yPixel] = 1;
            } else {
                array2D[xPixel][yPixel] = 0; // ?
            }
        }
    }
}

当您使用简单的二维数组循环打印数组时,它遵循输入图像中的像素位置:

    for (int x = 0; x < array2D.length; x++)
    {
        for (int y = 0; y < array2D[x].length; y++)
        {
            System.out.print(array2D[x][y]);
        }
        System.out.println();
    }

注意:修改的行用 标记//*

于 2015-07-07T03:32:35.867 回答