Advertisement
Guest User

my code

a guest
Apr 26th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. import java.awt.Robot; //java library that lets us take screenshots
  2. import java.awt.AWTException;
  3. import java.awt.event.InputEvent;
  4. import java.awt.image.BufferedImage;
  5. import java.awt.Rectangle;
  6. import java.awt.Dimension;
  7. import processing.serial.*; //library for serial communication
  8.  
  9.  
  10. Serial port; //creates object "port" of serial class
  11. Robot robby; //creates object "robby" of robot class
  12.  
  13. void setup()
  14. {
  15. println(Serial.list());
  16. port = new Serial(this, Serial.list()[4],9600); //set baud rate
  17. size(100, 100); //window size (doesn't matter)
  18. try //standard Robot class error check
  19. {
  20. robby = new Robot();
  21. }
  22. catch (AWTException e)
  23. {
  24. println("Robot class not supported by your system!");
  25. exit();
  26. }
  27. }
  28.  
  29. void draw()
  30. {
  31. int pixel; //ARGB variable with 32 int bytes where
  32. //sets of 8 bytes are: Alpha, Red, Green, Blue
  33. float r=0;
  34. float g=0;
  35. float b=0;
  36.  
  37. int skipValue = 2;
  38. int x = screen.width; //possibly displayWidth
  39. int y = screen.height; //possible displayHeight instead
  40.  
  41. //get screenshot into object "screenshot" of class BufferedImage
  42. BufferedImage screenshot = robby.createScreenCapture(new Rectangle(new Dimension(x,y)));
  43. //1368*928 is the screen resolution
  44.  
  45.  
  46. int i=0;
  47. int j=0;
  48. //I skip every alternate pixel making my program 4 times faster
  49. for(i =0;i<x; i=i+skipValue){
  50. for(j=0; j<y;j=j+skipValue){
  51. pixel = screenshot.getRGB(i,j); //the ARGB integer has the colors of pixel (i,j)
  52. r = r+(int)(255&(pixel>>16)); //add up reds
  53. g = g+(int)(255&(pixel>>8)); //add up greens
  54. b = b+(int)(255&(pixel)); //add up blues
  55. }
  56. }
  57. int aX = x/skipValue;
  58. int aY = y/skipValue;
  59. r=r/(aX*aY); //average red
  60. g=g/(aX*aY); //average green
  61. b=b/(aX*aY); //average blue
  62.  
  63. //println(r+","+g+","+b);
  64.  
  65. // filter values to increase saturation
  66. float maxColorInt;
  67. float minColorInt;
  68.  
  69. maxColorInt = max(r,g,b);
  70. if(maxColorInt == r){
  71. // red
  72. if(maxColorInt < (225-20)){
  73. r = maxColorInt + 20;
  74. }
  75. }
  76. else if (maxColorInt == g){
  77. //green
  78. if(maxColorInt < (225-20)){
  79. g = maxColorInt + 20;
  80. }
  81. }
  82. else {
  83. //blue
  84. if(maxColorInt < (225-20)){
  85. b = maxColorInt + 20;
  86. }
  87. }
  88.  
  89. //minimise smallest
  90. minColorInt = min(r,g,b);
  91. if(minColorInt == r){
  92. // red
  93. if(minColorInt > 20){
  94. r = minColorInt - 20;
  95. }
  96. }
  97. else if (minColorInt == g){
  98. //green
  99. if(minColorInt > 20){
  100. g = minColorInt - 20;
  101. }
  102. }
  103. else {
  104. //blue
  105. if(minColorInt > 20){
  106. b = minColorInt - 20;
  107. }
  108. }
  109.  
  110.  
  111. port.write(0xff); //write marker (0xff) for synchronization
  112. port.write((byte)(r)); //write red value
  113. port.write((byte)(g)); //write green value
  114. port.write((byte)(b)); //write blue value
  115. delay(10); //delay for safety
  116.  
  117. background(r,g,b); //make window background average color
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement