Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello....i wanted a little help on how to read an pre-existing image in the form of a pixel RGB matrix in java. i tried on the net bt couldnt gt anything conclusive.
Posted

talisman.anant wrote:
i tried on the net bt couldnt gt anything conclusive.


What about all these[^]?
 
Share this answer
 
You can get the Raster out of an image and then get the pixels out of that.

Here's an (unoptimized) example:

package sandbox;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Main {

    public static void main(String[] args) {
        try {
            BufferedImage image = ImageIO.read(new File("c:\\alien.jpg"));

            Raster raster = image.getRaster();
            int[][] imagePixels = new int[image.getWidth()][image.getHeight()];
            for(int x = 0; x < image.getWidth(); ++x) {
                for(int y = 0; y < image.getHeight(); ++y) {

                    int pixel = 0;
                    int[] pixelData = raster.getPixel(x, y, new int[4]);

                    for(int i = 0; i < pixelData.length; ++i) {
                        pixel |= pixelData[i] << (i * 8);
                    }
                    //System.out.println(x + ", " + y + " = " + pixel);
                }
            }

        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}


Hope this helps,
Fredrik
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900