Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

1366 x 768 ambilight processing code

By: a guest on Jun 3rd, 2013  |  syntax: Java  |  size: 1.98 KB  |  views: 48  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //Developed by Rajarshi Roy
  2. import java.awt.Robot; //java library that lets us take screenshots
  3. import java.awt.AWTException;
  4. import java.awt.event.InputEvent;
  5. import java.awt.image.BufferedImage;
  6. import java.awt.Rectangle;
  7. import java.awt.Dimension;
  8. import processing.serial.*; //library for serial communication
  9.  
  10.  
  11. Serial port; //creates object "port" of serial class
  12. Robot robby; //creates object "robby" of robot class
  13. int monitorx = 1366; //must be even numbers
  14. int monitory = 768; //must be even
  15.  
  16. void setup()
  17. {
  18. port = new Serial(this, Serial.list()[0],9600); //set baud rate
  19. size(100, 100); //window size (doesn't matter)
  20. try //standard Robot class error check
  21. {
  22. robby = new Robot();
  23. }
  24. catch (AWTException e)
  25. {
  26. println("Robot class not supported by your system!");
  27. exit();
  28. }
  29. }
  30.  
  31. void draw()
  32. {
  33. int pixel; //ARGB variable with 32 int bytes where
  34. //sets of 8 bytes are: Alpha, Red, Green, Blue
  35. float r=0;
  36. float g=0;
  37. float b=0;
  38.  
  39. //get screenshot into object "screenshot" of class BufferedImage
  40. BufferedImage screenshot = robby.createScreenCapture(new Rectangle(new Dimension(monitorx,monitory)));
  41. //1368*928 is the screen resolution
  42.  
  43.  
  44. int i=0;
  45. int j=0;
  46. //I skip every alternate pixel making my program 4 times faster
  47. for(i =0;i<monitorx; i=i+2){
  48. for(j=0; j<monitory;j=j+2){
  49. pixel = screenshot.getRGB(i,j); //the ARGB integer has the colors of pixel (i,j)
  50. r = r+(int)(255&(pixel>>16)); //add up reds
  51. g = g+(int)(255&(pixel>>8)); //add up greens
  52. b = b+(int)(255&(pixel)); //add up blues
  53. }
  54. }
  55. r=r/((monitorx/2)*(monitory/2)); //average red (remember that I skipped ever alternate pixel)
  56. g=g/((monitorx/2)*(monitory/2)); //average green
  57. b=b/((monitorx/2)*(monitory/2)); //average blue
  58.  
  59.  
  60. port.write(0xff); //write marker (0xff) for synchronization
  61. port.write((byte)(r)); //write red value
  62. port.write((byte)(g)); //write green value
  63. port.write((byte)(b)); //write blue value
  64. delay(10); //delay for safety
  65.  
  66. background(r,g,b); //make window background average color
  67. }
clone this paste RAW Paste Data