0

I loaded a binary image and I want to convert it to 2D array, in particular int[][]:

public int[][] ImageToArray(String pathImage) throws IOException {

    File file = new File(pathImage);
    BufferedImage bufferedImage = ImageIO.read(file);

    int width = bufferedImage.getWidth();
    int height = bufferedImage.getHeight();
    int[][] imageArray = new int[width][height];

    return imageArray;}

But when I run my source code I get an exception:

Caused by: javax.imageio.IIOException: Can't read input file!

Can you help me?

Dinko Pehar
  • 5,454
  • 4
  • 23
  • 57
Leon Zak
  • 13
  • 6
  • And what shall each int value represent? The pixel color, as RGBA? – Florian Albrecht Jan 14 '19 at 15:00
  • Possible duplicate of [Java - get pixel array from image](https://stackoverflow.com/questions/6524196/java-get-pixel-array-from-image) – Ricola Jan 14 '19 at 15:01
  • 1
    You can get the RGB as a 1D array using [`BufferedImage::getRGB`](https://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html#getRGB(int,%20int,%20int,%20int,%20int[],%20int,%20int)), then convert to a 2D array of your specified width and height. – khelwood Jan 14 '19 at 15:02
  • 3
    Given *"IIOException: Can't read input file!"* the title would better read.. *"How to **load** image from file?"*. You are at least one step short of trying to get the pixel data. – Andrew Thompson Jan 14 '19 at 15:07
  • @FlorianAlbrecht I should insert int[][] in a method – Leon Zak Jan 14 '19 at 15:09
  • 2
    As to solving the `IIOException`.. First concentrate on getting a valid `File` object. Try `File f = new File(pathImage); System.out.println("Exists: " + f.exists());`. Don't proceed until it returns `true`. General tip: For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Also share the file system path to the actual file. – Andrew Thompson Jan 14 '19 at 15:13
  • @AndrewThompson I load the image with a path defined in the source – Leon Zak Jan 14 '19 at 15:14
  • 1
    *"a path defined in the source"* That is (part of the reason) why I suggested you post an MCVE / SSCCE. That way, we could see **exactly** what the value of `pathImage` is! As it is, now we can only guess. (And I for one, have better things to do, and people to help who can follow simple instructions.) – Andrew Thompson Jan 14 '19 at 15:17
  • Reading this question now, I saw @LeonZak never asked anything again. For anyone reading this, don't be harsh to new users. Cheers ! – Dinko Pehar Sep 26 '19 at 09:44

1 Answers1

0

If you want to get all pixels as 2D array(matrix), you can use:

File file = new File(pathImage); // Be sure to read input file, you have error reading it.
BufferedImage bufferedImage = ImageIO.read(file);
WritableRaster wr = bufferedImage.getRaster();

Then the usage of that matrix would be simple as:

for (int i = 0; i < wr.getWidth(); i++) {
    for (int j = 0; j < wr.getHeight(); j++) {      
        int pixel = wr.getSample(i, j, 0); // the sample in the specified band for the pixel at the specified coordinate.
    }
}

There are other methods for getting and setting a pixel, be sure to read docs. Hope this helps.

Dinko Pehar
  • 5,454
  • 4
  • 23
  • 57