Sunday, April 21, 2013

Ambient Lighting Monitor with Android and Java

Fascinated by Arduino and Raspberry Pi recently. Found a cool application called "Ambient Lighting" which calculates the light on your monitor and changes the color of the LED accordingly. The effect is real time cinema-like movie watching experience.

Note that this video is stolen from http://brunql.github.io/ambilight/


Most of them are using LED with Arduino controller. None of the people think of using the screen of the mobile phone or tablet as a light source (they are probably too dim for the desired effect which I found out later).

This is a quick hack during the weekend, firstly take a screenshot of the current screen. It was easier than I thought using the Java built-in function of java.awt.Robot, createScreenCapture(). After that get all the Red, Green and Blue of each pixels and calculate the average color for the current screen. Ratios of the color according to the different positions such as main color on the top right or bottom left portion of the screen are ignored as my light source is limited (1 or 2 android phone) and could not display different color on each spot.

After that, I need a simple way to send the color data to the android phone. I got lazy and just use Simple Framework to serve HTTP response on configured port. For example, every time the phone send request to http://my_ip:7788, it would return the calculated average color for current screen. To make it easier, I return a simple HTML code to set the background color of the whole page with the calculated RGB value. The page will reload every 1 second using HTTP "META refresh" tag. Reload the page more frequently with javascript should result in a more realistic ambient lighting.

The result is less satisfactory than had been anticipated. The screen of the mobile phone is not as bright as expected even after manually tuning it to the maximum brightness. Probably can improve the effect after tuning the calculated color to make it look brighter. Position information should make multiple phones and tablets act as the "LED", albeit much more expensive. Just for the sake of fun as I have a couple of Android devices hanging around.

I have published the code on bitbucket and feel free to have a look if you are looking to waste some time to enhance your movie watching experience. =)

My code on bitbucket
Ambient light

Reference:
http://brunql.github.io/ambilight/
https://code.google.com/p/boblight/
https://code.google.com/p/light-pack/
http://learn.adafruit.com/adalight-diy-ambient-tv-lighting/download-and-install