在java中扫描特定颜色的图像

时间:2013-05-12 18:54:12

标签: java arraylist

我不知道从哪一步开始,但有没有办法可以使用Java逐行扫描特定颜色的图像,并将所有位置传递给ArrayList?

2 个答案:

答案 0 :(得分: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));

答案 1 :(得分: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));
      }
 }