2013-05-12 73 views
0

我不確定從哪一個開始,但是有沒有一種方法可以使用Java按行掃描特定顏色的圖像,並通過所有的位置和ArrayList?在java中掃描特定顏色的圖像

+0

另請參見[平滑鋸齒狀路徑](http://stackoverflow.com/questions/7218309/smoothing-a-jagged-path)。 – 2013-05-13 04:19:47

回答

2

你能嗎?是。這是如何:

ArrayList<Point> list = new ArrayList<Point>(); 
    BufferedImage bi= ImageIO.read(img); //Reads in the image 

    //Color you are searching for 
    int color= 0xFF00FF00; //Green in this example 
    for (int x=0;x<width;x++) 
     for (int y=0;y<height;y++) 
      if(bi.getRGB(x,y)==color) 
       list.add(new Point(x,y)); 
+0

好的,簡單的解決方案 – 2013-05-12 19:40:57

+0

很高興喜歡它 – Jason 2013-05-12 19:43:05

0

嘗試使用PixelGrabber。它接受ImageImageProducer

下面是改編自文檔的例子:

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 void handleSinglePixel(int x, int y, int pixel) { 
     int target = 0xFFABCDEF; // or whatever 
     if (pixel == target) { 
      myArrayList.add(new java.awt.Point(x, y)); 
     } 
}