1

Which is the best way to store images in an array? I'm using ImageIO and I now wish to store them in an array.

2
  • Do you want the array to hold, byte for byte, the data in the image file? Commented Dec 22, 2011 at 14:42
  • I want to store all the pixels of the image in an array or list Commented Dec 22, 2011 at 14:50

2 Answers 2

5

PixelGrabber is what you want.

Image img = null;
try {
    img = ImageIO.read(new File("img.png"));
} catch (IOException e) {
    e.printStackTrace();
}
int w = img.getWidth(null);
int h = img.getHeight(null);
int[] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(img, 0, 0, w, h, pixels, 0, w);
try {
    pg.grabPixels();
} catch (InterruptedException e) {
    System.err.println("interrupted waiting for pixels!");
    return;
}
if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
    System.err.println("image fetch aborted or errored");
    return;
}
for (int i = 0; i < pixels.length; i++)
    System.out.println(pixels[i]);
Sign up to request clarification or add additional context in comments.

2 Comments

Xm this is the easiest way? I mean how i could store my image in a 3d array for every colour? I want to compute the histogram for every color finally.
Can ask sth else? What is goin to be the format of the pixels values? I got numbers like -14277082 this stands for the triplette fo RGB??
3

If you read the whole images in memory, you can store them in List<byte[]> or List<BufferedImage>.

Otherwise you can simply store their paths as List<String>.

If you really need an array, you can instead use BufferedImage[], or use list.toArray(..)

1 Comment

I wan to store the whole images so as to processing them. I use this command to BufferedImage image = ImageIO.read(new File(); to read them. Then next step is to just store image in an List?

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.