Guest User

Untitled

a guest
Jan 18th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. import processing.serial.*;
  2.  
  3. import java.awt.*;
  4. import java.awt.image.*;
  5.  
  6. Serial myPort;
  7.  
  8. boolean can_send = false;
  9.  
  10. // Put the right screen size in here
  11. int screenW = 1920;
  12. int screenH = 1080;
  13.  
  14. // Preview window size
  15. int windowW = 100;
  16. int windowH = 100;
  17.  
  18. // Define the box where we want to capture and analyse colour from
  19. // The start point of the box (top and left coordinate)
  20. int boxL = 350; // That's the middle of the screen
  21. int boxT = 350;
  22. // Size of the box
  23. int boxW = 1220;
  24. int boxH = 380;
  25.  
  26. //How many pixels to skip while reading (the more you skip, it runs faster, but result might get worse)
  27. int pixelSkip = 2;
  28.  
  29. // Screen Area to be captured (usually the whole screen)
  30. Rectangle dispBounds;
  31. // creates object from java library that lets us take screenshots
  32. Robot bot;
  33.  
  34. void setup(){
  35. size(100, 100);
  36.  
  37. String portName = "COM6";
  38. myPort = new Serial(this, portName, 115200);
  39. // Create screenshot area
  40. dispBounds = new Rectangle(new Dimension(screenW,screenH));
  41. // Create Preview Window
  42.  
  43.  
  44. // Standard Robot class error check and Create screenshot Robot
  45. try {
  46. bot = new Robot();
  47. }
  48. catch (AWTException e) {
  49. println("Robot class not supported by your system!");
  50. exit();
  51. }
  52.  
  53.  
  54. }
  55.  
  56. void draw(){
  57.  
  58. //ARGB value of a pixel, contains sets of 8 bytes values for Alpha, Red, Green, Blue
  59. int pixel;
  60.  
  61. int r = 0;
  62. int g = 0;
  63. int b = 0;
  64.  
  65. // Take screenshot
  66. BufferedImage screenshot = bot.createScreenCapture(dispBounds);
  67.  
  68. // Pass all the ARGB values of every pixel into an array
  69. int[] screenData = ((DataBufferInt)screenshot.getRaster().getDataBuffer()).getData();
  70.  
  71. //Find the RGB values of the region we want
  72. for(int i = boxT; i < (boxT + boxH); i += pixelSkip){
  73. for(int j = boxL; j < (boxL + boxW); j += pixelSkip){
  74.  
  75. pixel = screenData[ i*screenW + j ];
  76. r += 0xff & (pixel>>16);
  77. g += 0xff & (pixel>>8 );
  78. b += 0xff & pixel;
  79.  
  80. }
  81. }
  82.  
  83. // take average RGB values.
  84. r = r / (boxH/pixelSkip * boxW/pixelSkip);
  85. g = g / (boxH/pixelSkip * boxW/pixelSkip);
  86. b = b / (boxH/pixelSkip * boxW/pixelSkip);
  87.  
  88. color rgb = color((short)r, (short)g, (short)b);
  89. fill(rgb);
  90. rect(0, 0, 100, 100);
  91.  
  92. myPort.write(str(r) + ":" + str(g) + ":" + str(b));
  93. println(str(r) + ":" + str(g) + ":" + str(b));
  94. can_send = false;
  95.  
  96.  
  97. delay(1000);
  98.  
  99.  
  100. }
Add Comment
Please, Sign In to add comment