轉:java提取圖片中的像素

本文轉自: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]);
            }
        }
 }

  

PixelGrabber

public PixelGrabber(Image img,
                    int x,
                    int y,
                    int w,
                    int h,
                    int[] pix,
                    int off,
                    int scansize)
建立一個 PixelGrabber 對象,以從指定圖像將像素矩形部分 (x, y, w, h) 抓取到給定的數組中。以默認的 RGB ColorModel 形式將像素存儲到數組中。像素 (i, j)((i, j) 處於矩形 (x, y, w, h) 內)的 RGB 數據存儲在數組中的  pix[(j - y) * scansize + (i - x) + off] 位置處。

 

參數:
img - 從中檢索像素的圖像
x - 從圖像中進行檢索的像素矩形左上角 x 座標,其相對於默認(未縮放)圖像大小
y - 從圖像中進行檢索的像素矩形左上角 y 座標
w - 要檢索的像素矩形的寬度
h - 要檢索的像素矩形的高度
pix - 用於保存從圖像中檢索的 RGB 像素的整數數組
off - 數組中存儲第一個像素的偏移量
scansize - 數組中一行像素到下一行像素之間的距離
另請參見:
ColorModel.getRGBdefault()

PixelGrabber

public PixelGrabber(ImageProducer ip,
                    int x,
                    int y,
                    int w,
                    int h,
                    int[] pix,
                    int off,
                    int scansize)
建立一個 PixelGrabber 對象,以從指定 ImageProducer 所生成的圖像中將像素矩形部分 (x, y, w, h) 抓取到給定的數組中。以默認的 RGB ColorModel 形式將像素存儲到數組中。像素 (i, j)((i, j) 處於矩形 (x, y, w, h) 內)的 RGB 數據存儲在數組中的  pix[(j - y) * scansize + (i - x) + off] 位置處。

 

參數:
ip - 生成圖像的  ImageProducer,從該圖像中檢索像素
x - 從圖像中進行檢索的像素矩形左上角 x 座標,其相對於默認(未縮放)圖像大小
y - 從圖像中進行檢索的像素矩形左上角 y 座標
w - 要檢索的像素矩形的寬度
h - 要檢索的像素矩形的高度
pix - 用於保存從圖像中檢索的 RGB 像素的整數數組
off - 數組中存儲第一個像素的偏移量
scansize - 數組中一行像素到下一行像素之間的距離
另請參見:
ColorModel.getRGBdefault()

PixelGrabber

public PixelGrabber(Image img,
                    int x,
                    int y,
                    int w,
                    int h,
                    boolean forceRGB)
建立一個 PixelGrabber 對象,以從指定的圖像中抓取像素矩形部分 (x, y, w, h)。若是每次調用 setPixels 都使用相同的 ColorModel,則像素以原 ColorModel 形式存儲,不然像素將以默認 RGB ColorModel 形式存儲。若是 forceRGB 參數爲 true,則像素將老是以默認 RGB ColorModel 形式存儲。不管是哪一種狀況,PixelGrabber 都會分配一個緩衝區來保存這些像素。若是 (w < 0) 或 (h < 0),則它們默認爲傳遞信息時保存的源數據的寬度和高度。

 

參數:
img - 要從中檢索圖像數據的圖像
x - 從圖像中進行檢索的像素矩形左上角 x 座標,其相對於默認(未縮放)圖像大小
y - 從圖像中進行檢索的像素矩形左上角 y 座標
w - 要檢索的像素矩形的寬度
h - 要檢索的像素矩形的高度
forceRGB - 若是老是應該將像素轉換爲默認 RGB ColorModel,則爲 true

grabPixels

public boolean grabPixels()
                   throws InterruptedException
請求 Image 或 ImageProducer 開始傳遞像素,並等待傳遞完相關矩形中的全部像素。

 

返回:
若是成功抓取了像素,則返回 true;在停止、有錯誤或超時的狀況下返回 false
拋出:
InterruptedException - 另外一個線程中斷了此線程。
相關文章
相關標籤/搜索