Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting pixel data from an image using java

Tags:

java

rgb

I'm trying to get the pixel rgb values from a 64 x 48 bit image. I get some values but nowhere near the 3072 (= 64 x 48) values that I'm expecting. I also get:

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)

I can't find the out of bounds error...

Here's the 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 < img.getHeight(); i++){
            for(int j = 0; j < img.getWidth(); j++){
                rgb = getPixelData(img, i, j);

                for(int k = 0; k < 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 >> 16) & 0xff, //red
    (argb >>  8) & 0xff, //green
    (argb      ) & 0xff  //blue
};

System.out.println("rgb: " + rgb[0] + " " + rgb[1] + " " + rgb[2]);
return rgb;
}

}
like image 567
Matt Avatar asked Oct 22 '09 00:10

Matt


People also ask

How do pixels work in Java?

An image contains a two dimensional array of pixels. It is actually the value of those pixels that make up an image. Usually an image could be color or grayscale. In Java, the BufferedImage class is used to handle images.

Can Java image processing?

Java provides immediate access to the image pixels and color information and allows conversions and image processing.

How can we get an RGB of a pixel?

Retrieving the pixel contents (ARGB values) of an image −Get the pixel value at every point using the getRGB() method. Instantiate the Color object by passing the pixel value as a parameter. Get the Red, Green, Blue values using the getRed(), getGreen() and getBlue() methods respectively.


2 Answers

This:

for(int i = 0; i < img.getHeight(); i++){
    for(int j = 0; j < img.getWidth(); j++){
        rgb = getPixelData(img, i, j);

Does not match up with this:

private static int[] getPixelData(BufferedImage img, int x, int y) {

You have i counting the rows and j the columns, i.e. i contains y values and j contains x values. That's backwards.

like image 80
John Kugelman Avatar answered Sep 19 '22 12:09

John Kugelman


This works too:

BufferedImage img = ImageIO.read(file);

int[] pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
like image 24
Mithusayel Murmu Avatar answered Sep 20 '22 12:09

Mithusayel Murmu