Java BufferedImage: How to get the RGB value of each image pixel

I'm just getting started with image parsing in Java, but right away I need to do something that’s relatively hard: find a small image inside of a larger image. I’ve run into some initial problems so I’m writing small tests as I go along.

In this article I’ll share the results of a first test I’ve written to walk through all the pixels in an image (a Java BufferedImage) and print out their RGB (technically ARGB) values.

Java Image/BufferedImage example

Here’s my initial Java BufferedImage example code. I think everything is straightforward, with the possible the exception of the bitwise operator stuff where I convert a Java int into the RGB/ARGB values the int represents. If you haven’t used code like that before it can be a bit surprising.

As final comment, notice how easy the Java ImageIO class makes it to open a JPG file. That’s very nice:

package com.devdaily.imagetests;

import java.awt.Component;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;

public class JavaWalkBufferedImageTest1 extends Component {

  public static void main(String[] foo) {
    new JavaWalkBufferedImageTest1();
  }

  public void printPixelARGB(int pixel) {
    int alpha = (pixel >> 24) & 0xff;
    int red = (pixel >> 16) & 0xff;
    int green = (pixel >> 8) & 0xff;
    int blue = (pixel) & 0xff;
    System.out.println("argb: " + alpha + ", " + red + ", " + green + ", " + blue);
  }

  private void marchThroughImage(BufferedImage image) {
    int w = image.getWidth();
    int h = image.getHeight();
    System.out.println("width, height: " + w + ", " + h);

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        System.out.println("x,y: " + j + ", " + i);
        int pixel = image.getRGB(j, i);
        printPixelARGB(pixel);
        System.out.println("");
      }
    }
  }

  public JavaWalkBufferedImageTest1() {
    try {
      // get the BufferedImage, using the ImageIO class
      BufferedImage image = 
        ImageIO.read(this.getClass().getResource("WhiteSpot.jpg"));
      marchThroughImage(image);
    } catch (IOException e) {
      System.err.println(e.getMessage());
    }
  }

}

Java BufferedImage example - output

I won't add any further description of this class here, but if anyone has any specific questions I'll be glad to answer them.

FWIW, here are the first few lines of output from this example BufferedImage program. In generating this example output, I'm walking through a small image that is mostly white pixels on a black background, but as you'll see in the output it's not all black and white; there are shades of gray where the image tool I'm using did a little dithering to make things look nice. Here's the initial output from my program and sample JPG image file:

width, height: 16, 12
x,y: 0, 0
argb: 255, 0, 0, 0

x,y: 1, 0
argb: 255, 0, 0, 0

x,y: 2, 0
argb: 255, 10, 10, 10

x,y: 3, 0
argb: 255, 50, 50, 50

x,y: 4, 0
argb: 255, 97, 97, 97

x,y: 5, 0
argb: 255, 138, 138, 138

x,y: 6, 0
argb: 255, 167, 167, 167

x,y: 7, 0
argb: 255, 176, 176, 176

x,y: 8, 0
argb: 255, 182, 182, 182

x,y: 9, 0
argb: 255, 176, 176, 176

x,y: 10, 0
argb: 255, 161, 161, 161

x,y: 11, 0
argb: 255, 123, 123, 123

x,y: 12, 0
argb: 255, 78, 78, 78

x,y: 13, 0
argb: 255, 21, 21, 21

x,y: 14, 0
argb: 255, 0, 0, 0

x,y: 15, 0
argb: 255, 0, 0, 0

x,y: 0, 1
argb: 255, 0, 0, 0

x,y: 1, 1
argb: 255, 22, 22, 22

x,y: 2, 1
argb: 255, 96, 96, 96

(the rest of the program output has been omitted)