Creating an Ambilight System (using Robot.createScreenCapture). How Can I stop the mouseblinks?

edited March 2014 in Questions about Code

Some Background information :

Hello my name is RenV and I'm a student trying to make a little hobbyproject.

I want to make an ambilight using this : blinkstick I want to create an Ambilight app that calculates the average color of my screen and then changes the color of my blinkstick accordingly.

The Blinkstick comes with software for ambilight itself, but it doesn't work. So I thought of just programming it myself.

This is my code :

import blinkstick.*;
import java.awt.image.BufferedImage;
import java.awt.SystemTray;
import java.awt.GraphicsEnvironment;
import java.awt.GraphicsDevice;
import java.awt.DisplayMode;
import java.awt.Rectangle; 
import java.awt.Robot;
import java.awt.AWTException;
//import java.awt.Color;

int colr;
PImage screenShot;
BlinkStick device;
GraphicsEnvironment ge;
GraphicsDevice[] gs;
DisplayMode mode;
Rectangle bounds;
BufferedImage desktop;

void setup() 
{
  size(100,100);
  frameRate( 30 );

  device = BlinkStick.findFirst();
  if (device == null) println("Not found...");  

  ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  gs = ge.getScreenDevices();
  mode = gs[0].getDisplayMode();
  bounds = new Rectangle(0, 0, mode.getWidth(), mode.getHeight());

}

void draw () 
{
    screenShot = getScreen();

    setAmbiColor();

    device.setColor(colr);

}

PImage getScreen() 
{

  desktop = new BufferedImage(mode.getWidth(), mode.getHeight(), BufferedImage.TYPE_INT_RGB);

  try 
  {
    desktop = new Robot(gs[0]).createScreenCapture(bounds);
  }
  catch(AWTException e) 
  {
    //System.err.println("Screen capture failed.");
  }

  return (new PImage(desktop));
}

void setAmbiColor()
{
  screenShot.loadPixels();
  int r = 0, g = 0, b = 0;
  for (int i=0; i<screenShot.pixels.length; i++) 
  {
    color c = screenShot.pixels[i];
    r += c>>16&0xFF;
    g += c>>8&0xFF;
    b += c&0xFF;
  }
  r /= screenShot.pixels.length;
  g /=screenShot.pixels.length;
  b /= screenShot.pixels.length;

  colr = color(r,g,b);
  print("original :  " + colr ); 
 // colr*=-1; //Inverse Color
  if(r<150 && g<150 && b<150)
  { 
    changeSaturation(2f);
  }
}

void changeSaturation(float change) 
{
  float R = red(colr);
  float G = green(colr);
  float B  = blue(colr);
  float  P=sqrt(
  (R)*(R)*0.299+
  (G)*(G)*0.587+
  (B)*(B)*0.114 ) ;

  R=P+((R)-P)*change;
  G=P+((G)-P)*change;
  B=P+((B)-P)*change; 
  int r = Math.round(R);
  int g = Math.round(G);
  int b = Math.round(B);
  colr = color(r,g,b);
  println(" adjusted :  " + colr); 
}

It works but :

  1. it uses a lot of resources
  2. the mouse blinks everytime a screenshot is made
  3. I would like to minimize my app to a tasktray

I would really appreciate any kind of help. Do you think it's possible to do this in processing 2?

Thanks a lot.

Answers

  • "it uses a lot of resources"
    Do you really need to make 30 screenshots per second?

    "the mouse blinks everytime a screenshot is made"
    Perhaps related to the remark above.

    "I would like to minimize my app to a tasktray"
    Look for Java-specific solutions.

  • edited March 2014
    1. _"Do you really need to make 30 screenshots per second?"_ Maybe not but atleast 10-15fps to watch a movie without much noticable lag.
    2. I need an alternative to the whole take a screenshot, and calculate an average color of 3.686.400 pixels (2560x1440).
    3. My question basically is how can I calculate the average color using a different method then taking screenshots, that is maybe more efficient ?
  • edited March 2014 Answer ✓

    Just take the color at some (maybe 20) randomly/uniformly distributed screen coordinates (with Robot.getPixelColor(x, y)) and average the resulting colors.

Sign In or Register to comment.