I have a piece of code, and what it does is find the colors of the image (or section of the screen), and if the color of RG and B is greater than 127, then it puts 1 in the corresponding position of the 2D int array. Here is the segment that I have now, but it is obviously very slow. Is there a better way to do this?
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());} }
There should be a faster way to do this using the above values. b
is an int array, and it is initialized in this segment. r
is the robot that I use to find the color of a pixel on the screen. x
and y
should be clear.
Thanks to Micker! This is the end result I got based on your answer:
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());} }
-1 in the if else expression apparently finds the value of the black pixels as I wanted.
source share