Use PixelGrabber class to acquire pixel data from an Image object
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.PixelGrabber;
public class Main {
static boolean isGreyscaleImage(PixelGrabber pg) {
return pg.getPixels() instanceof byte[];
}
public static void main(String args[]) throws Exception {
Image image = Toolkit.getDefaultToolkit().getImage("inFile.png");
PixelGrabber grabber = new PixelGrabber(image, 0, 0, -1, -1, false);
if (grabber.grabPixels()) {
int width = grabber.getWidth();
int height = grabber.getHeight();
if (isGreyscaleImage(grabber)) {
byte[] data = (byte[]) grabber.getPixels();
} else {
int[] data = (int[]) grabber.getPixels();
}
}
}
}
Related examples in the same category