1

Given an image file, say of PNG format, how to I get an array of int [r,g,b,a] representing the pixel located at row i, column j?

So far I am starting here:

private static int[][][] getPixels(BufferedImage image) {

    final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
    final int width = image.getWidth();
    final int height = image.getHeight();

    int[][][] result = new int[height][width][4];

    // SOLUTION GOES HERE....
}

Thanks in advance!

4
  • 1
    Get the pixel data from the image as a int, pass that to Color(int, boolean) Commented Dec 18, 2014 at 1:38
  • Can you please supply the code as an answer for acceptance? Commented Dec 18, 2014 at 1:42
  • "Get the pixel data from the image as a int, pass that to Color(int, boolean)": that's an old load of crap Commented Jul 27, 2019 at 14:06
  • @gpasch I think you will need to rephrase that comment, in order to make the polite and constructive part of it more obvious. Otherwise it just reads as "something mentioned nearly five years ago is not the most modern solution anymore" and could be mistaken for something completly free of information and possibly even rude. Commented Jul 27, 2019 at 14:25

3 Answers 3

4

You need to get the packed pixel value as an int, you can then use Color(int, boolean) to build a color object from which you can extract the RGBA values, for example...

private static int[][][] getPixels(BufferedImage image) {
    int[][][] result = new int[height][width][4];
    for (int x = 0; x < image.getWidth(); x++) {
        for (int y = 0; y < image.getHeight(); y++) {
            Color c = new Color(image.getRGB(i, j), true);
            result[y][x][0] = c.getRed();
            result[y][x][1] = c.getGreen();
            result[y][x][2] = c.getBlue();
            result[y][x][3] = c.getAlpha();
        }
    }
}

It's not the most efficient method, but it is one of the simplest

Sign up to request clarification or add additional context in comments.

1 Comment

result[y][x][3] = c.getAlpha();...?
2

BufferedImages have a method called getRGB(int x, int y) which returns an int where each byte is the components of the pixel (alpha, red, green and blue). If you dont want to do the bitwise operators yourself you can use Colors.getRed/Green/Blue methods by creating a new instance of Java.awt.Color with the int from getRGB.

You can do this in a loop to fill the three-dimensional array.

Comments

0

This is my code for this problem:

 File f = new File(filePath);//image path with image name like "lena.jpg"
 img = ImageIO.read(f);

 if (img==null) //if img null return
    return;
 //3d array [x][y][a,r,g,b]  
 int [][][]pixel3DArray= new int[img.getWidth()][img.getHeight()][4];
     for (int x = 0; x < img.getWidth(); x++) {
         for (int y = 0; y < img.getHeight(); y++) {

            int px = img.getRGB(x,y); //get pixel on x,y location

            //get alpha;
            pixel3DArray[x][y][0] =(px >> 24)& 0xff; //shift number and mask

            //get red
            pixel3DArray[x][y][1] =(px >> 16)& 0xff;


            //get green
            pixel3DArray[x][y][2] =(px >> 8)& 0xff;

            //get blue
            pixel3DArray[x][y][3] =(px >> 0)& 0xff;

         }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.