如何读取.bmp文件以识别Java中的黑色像素

如何读取.bmp文件以识别Java中的黑色像素,java,image,file-io,bmp,Java,Image,File Io,Bmp,像下面这样的。。。除了让它发挥作用外: 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; xP

像下面这样的。。。除了让它发挥作用外:

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;
            }
        }
    }
}
public void seeBMPImage(字符串BMPFileName)引发IOException{
BuffereImage image=ImageIO.read(getClass().getResource(BMPFileName));
int[][]array2D=新int[66][66];
for(int-xPixel=0;xPixel>23)==1){
array2D[xPixel][yPixel]=1;
}否则{
array2D[xPixel][yPixel]=1;
}
}
}
}
我会用这个:

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; // ?
                }
            }
        }
    }
public void seeBMPImage(字符串BMPFileName)引发IOException{
BuffereImage image=ImageIO.read(getClass().getResource(BMPFileName));
int[][]array2D=new int[image.getWidth()][image.getHeight()];
对于(int-xPixel=0;xPixel

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

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; // ?
            }
        }
    }
}
public void seeBMPImage(字符串BMPFileName)引发IOException{
BuffereImage image=ImageIO.read(getClass().getResource(BMPFileName));
int[][]array2D=new int[image.getHeight()][image.getWidth()]//*
对于(int-xPixel=0;xPixel
使用简单的二维阵列循环打印阵列时,将遵循输入图像中的像素位置:

    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();
    }
for(int x=0;x

注意:修改后的行用
/*

标记,那么上面的代码有什么问题?如果((color>>23)==1),为什么要测试
?这将测试红色分量是否为128或更多。您可以简单地从RGB
int
值构造一个
Color
对象,并直接获取
红色
绿色
蓝色
值……为什么在if分支和else分支中都指定1?可能是个bug…为什么要自己从原始文件内容中读取位图?ImageIO为您完成此任务,您可以轻松地使用BuffereImage来处理它。对不起,我对你的问题感到困惑。你是对的,根据你的解释,我使用ImageIO的方法是正确的。我正在将ImageIO.read(文件)输入光栅以获取原始数据,这样我就可以读取每个像素。我要做的是分别保存每个BGR color int三个不同的int[]数组蓝色、绿色、红色;。但我不知道如何从光栅中读取数据