mmirwaldt code is already on the right track.
However, if you want the array to visually display the image:
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; // ? } } } }
When you print an array using a simple 2D array loop, it follows the placement of pixels in the input image:
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(); }
Note. Changed lines are marked //* .
source share