本文轉自:http://www.infosys.tuwien.ac.at/teaching/courses/WebEngineering/References/java/docs/api/java/awt/image/PixelGrabber.htmlhtml
PixelGrabber 類實現能夠附加在 Image 或 ImageProducer 對象上得到圖像像素子集的 ImageConsumer。下面是一個示例:java
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,則爲 true
public boolean grabPixels() throws InterruptedException
InterruptedException
- 另外一個線程中斷了此線程。