title: Getting pixel data from an image using java
body: <p>I'm trying to get the pixel rgb values from a <code>64 x 48</code> bit image. I get some values but nowhere near the <code>3072 (= 64 x 48)</code> values that I'm expecting. I also get:</p>
  
  <pre><code>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
  at sun.awt.image.ByteInterleavedRaster.getDataElements(ByteInterleavedRaster.java:301)
  at java.awt.image.BufferedImage.getRGB(BufferedImage.java:871)
  at imagetesting.Main.getPixelData(Main.java:45)
  at imagetesting.Main.main(Main.java:27)
  </code></pre>
  
  <p>I can't find the out of bounds error...</p>
  
  <p>Here's the code:</p>
  
  <pre><code>package imagetesting;
  
  import java.io.IOException;
  import javax.imageio.ImageIO;
  import java.io.File;
  import java.awt.image.BufferedImage;
  
  
  
  public class Main {
  
  public static final String IMG = "matty.jpg";
  
  public static void main(String[] args) {
  
      BufferedImage img;
  
      try {
          img = ImageIO.read(new File(IMG));
  
          int[][] pixelData = new int[img.getHeight() * img.getWidth()][3];
          int[] rgb;
  
          int counter = 0;
          for(int i = 0; i &lt; img.getHeight(); i++){
              for(int j = 0; j &lt; img.getWidth(); j++){
                  rgb = getPixelData(img, i, j);
  
                  for(int k = 0; k &lt; rgb.length; k++){
                      pixelData[counter][k] = rgb[k];
                  }
  
                  counter++;
              }
          }
  
  
      } catch (IOException e) {
          e.printStackTrace();
      }
  
  }
  
  private static int[] getPixelData(BufferedImage img, int x, int y) {
  int argb = img.getRGB(x, y);
  
  int rgb[] = new int[] {
      (argb &gt;&gt; 16) &amp; 0xff, //red
      (argb &gt;&gt;  8) &amp; 0xff, //green
      (argb      ) &amp; 0xff  //blue
  };
  
  System.out.println("rgb: " + rgb[0] + " " + rgb[1] + " " + rgb[2]);
  return rgb;
  }
  
  }
  </code></pre>