0

我不确定从哪里开始,但是有没有一种方法可以使用 Java 逐行扫描图像以获取特定颜色,并将所有位置传递到 ArrayList 中?

4

2 回答 2

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));
于 2013-05-12T18:59:17.980 回答
0

尝试使用PixelGrabber. 它接受Imageor ImageProducer

这是改编自文档的示例:

 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));
      }
 }
于 2013-05-12T18:59:11.740 回答