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.
-
Do you want the array to hold, byte for byte, the data in the image file?trumank– trumank2011-12-22 14:42:01 +00:00Commented Dec 22, 2011 at 14:42
-
I want to store all the pixels of the image in an array or listsnake plissken– snake plissken2011-12-22 14:50:04 +00:00Commented Dec 22, 2011 at 14:50
Add a comment
|
2 Answers
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]);
2 Comments
snake plissken
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.
snake plissken
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??
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
snake plissken
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?