Well, when I want to examine or display an image one pixel at a time, I first getRaster() on the BufferedImage, and then getDataBuffer() from it, and then you can getData() from that. You will end up with a single array of all of the pixels in your image. But what I like to do is then convert that to a 2-dimensional array, so I can just pull out or change a pixel using the X,Y coordinates in the image.
Here’s an example:
// load the image
BufferedImage myImage = ImageIO.read(this.getClass().getResource("myImage.jpg"));
// retrieve the pixel array from the BufferedImage
int[] pixelArray = ((DataBufferInt)myImage.getRaster().getDataBuffer()).getData();
// create a new 2-dimensional array the size of the image
int[][] myImageArray = new int[myImage.getWidth()][myImage.getHeight()];
// loop through each pixel in the pixelArray and store in the appropriate [x][y] spot in the new 2-dimensional array
for (int x = 0; x < myImage.getWidth(); x++) {
int[] columnPixels = new int[myImage.getHeight()];
for (int y = 0; y < myImage.getHeight(); y++) {
columnPixels[y] = pixelArray[(y * myImage.getWidth()) + x];
}
myImageArray[x] = columnPixels;
}
You may have to first convert your BufferedImage to TYPE_INT_RGB, or else when you cast to a DataBufferInt you may get an exception.
But in doing this, you now have an array that contains your entire image that you can now call with any x,y coordinates and either retrieve that pixel for display, or change that pixel. Or what’s also useful, you can retrieve just one “slice” of the image at a time. Let’s say you access it like: myImageArray[2]; This will return an array of all of the pixels in the 3rd column of the image.
I do this to preload and prepare all of the images for a level in my ray-casting type game.