Converts a java.awt.Image into an array of pixels
import java.awt.Image;
import java.awt.image.ImageObserver;
import java.awt.image.PixelGrabber;
public final class Util {
/**
* Converts a java.awt.Image into an array of pixels
*/
public static int[] convertToPixels(Image img) {
int width = img.getWidth(null);
int height = img.getHeight(null);
int[] pixel = new int[width * height];
PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, pixel, 0, width);
try {
pg.grabPixels();
} catch (InterruptedException e) {
throw new IllegalStateException("Error: Interrupted Waiting for Pixels");
}
if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
throw new IllegalStateException("Error: Image Fetch Aborted");
}
return pixel;
}
}
Related examples in the same category