Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 有没有更快的方法来获取屏幕截图中的颜色?_Java_Image_Image Processing_Screenshot_Awtrobot - Fatal编程技术网

Java 有没有更快的方法来获取屏幕截图中的颜色?

Java 有没有更快的方法来获取屏幕截图中的颜色?,java,image,image-processing,screenshot,awtrobot,Java,Image,Image Processing,Screenshot,Awtrobot,我有一段代码,它所做的是找到图像(或屏幕部分)的颜色,如果RG和B颜色大于127,则将a 1放在2D int数组的相应位置。这是我现在看到的片段,但显然非常慢。有更好的方法吗 private void traceImage(){ try{ Robot r = new Robot(); b = new int[470][338]; for(int y = 597; y < 597+469; y++) {

我有一段代码,它所做的是找到图像(或屏幕部分)的颜色,如果RG和B颜色大于127,则将a 1放在2D int数组的相应位置。这是我现在看到的片段,但显然非常慢。有更好的方法吗

private void traceImage(){
    try{
        Robot r = new Robot();
        b = new int[470][338];
        for(int y = 597; y < 597+469; y++)
        {
            for(int x = 570; x < 570+337; x++)
            {
                if(r.getPixelColor(x,y).getRed() > 127 &&
                   r.getPixelColor(x,y).getGreen() > 127 && 
                   r.getPixelColor(x,y).getBlue() > 127)
                {
                    b[y-597][x-570] = 1;
                }
                else
                {
                    b[y-597][x-570] = 0;
                }
            }
        }
    }
    catch(Exception ex){System.out.println(ex.toString());}
}
if-else语句中的-1显然找到了我想要的黑色像素值。

您应该使用它一次性捕获整个子图像

然后,您将能够更快地查询子图像中的各个像素,例如使用

有趣的是,您的特定操作有一个非常快速的按位实现:只需执行
((rgb&0x808080)==0x808080)

private void traceActionPerformed(java.awt.event.ActionEvent evt) {
    try{
        Robot r = new Robot();
        BufferedImage image = r.createScreenCapture(new Rectangle(597,570,470,337));
        b = new int[image.getWidth()][image.getHeight()];
        for(int y=0;y<image.getWidth();y++){
            for(int x=0;x<image.getHeight();x++){
                if(image.getRGB(y,x)==-1){
                    b[y][x]=0;
                }
                else{
                    b[y][x]=1;                 
                }
            }
        }
        System.out.println("Done");
    }
    catch(Exception ex){System.out.println(ex.toString());}
}