Java 如何为这段代码生成for循环?(我需要检查图像的某个区域是否有某种颜色。)

Java 如何为这段代码生成for循环?(我需要检查图像的某个区域是否有某种颜色。),java,Java,根据,以下代码需要循环,以实现其预期的工作目的 public class GetPixelColor { public static void main(String args[]) throws IOException{ File file= new File("your_file.jpg"); BufferedImage image = ImageIO.read(file); // Getting pixel color by position x and y int

根据,以下代码需要循环,以实现其预期的工作目的

public class GetPixelColor
{
  public static void main(String args[]) throws IOException{
  File file= new File("your_file.jpg");
  BufferedImage image = ImageIO.read(file);
  // Getting pixel color by position x and y 
  int clr=  image.getRGB(x,y); 
  int  red   = (clr & 0x00ff0000) >> 16;
  int  green = (clr & 0x0000ff00) >> 8;
  int  blue  =  clr & 0x000000ff;
  System.out.println("Red Color value = "+ red);
  System.out.println("Green Color value = "+ green);
  System.out.println("Blue Color value = "+ blue);
  }
}

如何定义要使用for循环检查其颜色的图像区域?

您可以简单地使用两个嵌套for循环在图像中循环,如下所示:

public static void printPixelColors(BufferedImage img) {
        int imageWidth = img.getWidth();
        int imageHeight = img.getHeight();
        for (int y = 0; y < imageHeight; y++) {
            for (int x = 0; x < imageWidth; x++) {
                // Getting pixel color by position x and y 
                int clr = img.getRGB(x, y);
                int red = (clr & 0x00ff0000) >> 16;
                int green = (clr & 0x0000ff00) >> 8;
                int blue = clr & 0x000000ff;
                System.out.println("Red Color value = " + red);
                System.out.println("Green Color value = " + green);
                System.out.println("Blue Color value = " + blue);
            }
        }
 }
公共静态空白打印像素颜色(BuffereImage img){
int imageWidth=img.getWidth();
int imageHeight=img.getHeight();
对于(int y=0;y>16;
绿色整数=(clr&0x0000ff00)>>8;
int blue=clr&0x000000ff;
System.out.println(“红色颜色值=”+红色);
System.out.println(“绿色颜色值=”+绿色);
System.out.println(“蓝色值=”+蓝色);
}
}
}

要读取其颜色的区域是两个点之间的矩形p1(x1,y1),p2(x2,y2) 然后用两个嵌套的循环扫描这个矩形

for(int x=x1; x<=x2; x++)
   for(int y=y1; y<=y2; y++){
        // Getting pixel color by position x and y 
        int clr=  image.getRGB(x,y); 
        int  red   = (clr & 0x00ff0000) >> 16;
        int  green = (clr & 0x0000ff00) >> 8;
        int  blue  =  clr & 0x000000ff;
        System.out.println("Red Color value = "+ red);
        System.out.println("Green Color value = "+ green);
        System.out.println("Blue Color value = "+ blue);
   }
for(int x=x1;x16;
绿色整数=(clr&0x0000ff00)>>8;
int blue=clr&0x000000ff;
System.out.println(“红色颜色值=”+红色);
System.out.println(“绿色颜色值=”+绿色);
System.out.println(“蓝色值=”+蓝色);
}

我知道这不是原始问题的一部分,但你能帮我理解“int red=(clr&0x00ff0000)>>16”中的内容吗?如果我理解正确,“clr&0x00ff0000”是一个布尔值。它甚至被移位“>>16”。如何将其分配给整数?
&
是逻辑的
运算,布尔运算符是
&
,代码屏蔽了
clr
中的
红色
值,然后将其移位
>16