标签:
本文转自:http://www.infosys.tuwien.ac.at/teaching/courses/WebEngineering/References/java/docs/api/java/awt/image/PixelGrabber.html
PixelGrabber 类实现可以附加在 Image 或 ImageProducer 对象上获得图像像素子集的 ImageConsumer。下面是一个示例:
public void handlesinglepixel(int x, int y, int pixel) {
int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel ) & 0xff;
// Deal with the pixel as necessary...
}
public void handlepixels(Image img, int x, int y, int w, int h) {
int[] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(img, x, y, 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 j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
handlesinglepixel(x+i, y+j, pixels[j * w + i]);
}
}
}
public PixelGrabber(Image img, int x, int y, int w, int h, int[] pix, int off, int scansize)
img - 从中检索像素的图像x - 从图像中进行检索的像素矩形左上角 x 坐标,其相对于默认(未缩放)图像大小y - 从图像中进行检索的像素矩形左上角 y 坐标w - 要检索的像素矩形的宽度h - 要检索的像素矩形的高度pix - 用于保存从图像中检索的 RGB 像素的整数数组off - 数组中存储第一个像素的偏移量scansize - 数组中一行像素到下一行像素之间的距离ColorModel.getRGBdefault()public PixelGrabber(ImageProducer ip, int x, int y, int w, int h, int[] pix, int off, int scansize)
ip - 生成图像的 ImageProducer,从该图像中检索像素x - 从图像中进行检索的像素矩形左上角 x 坐标,其相对于默认(未缩放)图像大小y - 从图像中进行检索的像素矩形左上角 y 坐标w - 要检索的像素矩形的宽度h - 要检索的像素矩形的高度pix - 用于保存从图像中检索的 RGB 像素的整数数组off - 数组中存储第一个像素的偏移量scansize - 数组中一行像素到下一行像素之间的距离ColorModel.getRGBdefault()public PixelGrabber(Image img, int x, int y, int w, int h, boolean forceRGB)
img - 要从中检索图像数据的图像x - 从图像中进行检索的像素矩形左上角 x 坐标,其相对于默认(未缩放)图像大小y - 从图像中进行检索的像素矩形左上角 y 坐标w - 要检索的像素矩形的宽度h - 要检索的像素矩形的高度forceRGB - 如果总是应该将像素转换为默认 RGB ColorModel,则为 truepublic boolean grabPixels()
throws InterruptedException
InterruptedException - 另一个线程中断了此线程。标签:
原文地址:http://www.cnblogs.com/bewolf/p/4660375.html