java 获取图片像素_转:java提取图片中的像素
本文轉自: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]);
}
}
}
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?- 數組中一行像素到下一行像素之間的距離另請參見:
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?- 數組中一行像素到下一行像素之間的距離另請參見:
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,則為 truegrabPixels
public boolean grabPixels()
throws InterruptedException
請求 Image 或 ImageProducer 開始傳遞像素,并等待傳遞完相關矩形中的所有像素。返回:如果成功抓取了像素,則返回 true;在中止、有錯誤或超時的情況下返回 false拋出:
總結
以上是生活随笔為你收集整理的java 获取图片像素_转:java提取图片中的像素的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python-Opencv实现魔方边缘识
- 下一篇: windows 启动后台进程