Take photo with laptop webcam when lid is opened

by sf Email

Recent news reports of a school in the USA causing controversy with its pupil issued laptops being used to spy on them. Made me think that taking a snap image when laptop lid is opened could be a cool pseudo security feature to catch if a snooper uses a laptop without permission. Would at least be a use for an otherwise unused laptop camera. How to do it though?

Brief search of the web turned up a howto for making the computer play a different sound for opening and closing the lid, something easily adapted to my needs.

To take the photo I used a command-line application called fswebcam which can be installed through the Synaptic Package Manager. fswebcam is a neat and simple webcam application with several useful configuration options. Once installed you can take snapshots with the example command:

fswebcam --no-banner --delay 1 -save ~/Desktop/%Y%m%d%H%M%S.jpeg

The above example is configured to turn off the overlay banner in the photo, delays the camera by 1sec before taking a photo (to make sure light is adjusted etc) and saves the jpeg image file to the desktop with a time/date stamp. Using a time stamp like this allows multiply files to be saved instead of over writing the same file each time which would be useless.

Now that photos can be successively taken the next thing to do is set up the system (I'm using Ubuntu 9.10 64bit) to take a shot whenever the lid is opened. This is where the "Advanced Configuration and Power Interface" (ACPI) is employed.
ACPI has a file called lid.sh which is located in Ubuntu under /etc/acpi, this script is run whenever the lid of the laptop is opened or closed. Opening this script file (sudo gedit /etc/acpi/lid.sh) in a text editor will show what commands are run when lid is opened or closed.

I added the following commands near the top of the file.

test -f /usr/share/acpi-support/state-funcs || exit 0
# take camera shot when lid opens
grep -q closed /proc/acpi/button/lid/LID/state
if [ $? != 0 ]
then
fswebcam --delay 1 --no-banner -save /home/yourusername/Pictures/LID_OPEN_CAPTURES/%Y%m%d%H%M%S.jpeg
fi

The command that grabs information about the state of the lid is the line:
grep ?q closed /proc/acpi/button/lid/LID/state

This line is followed by an if statement. This statement is used to discover if the lid has been opened or closed. So if [ $? != 0 ] (if not zero) means the lid has been opened so then do the following command...

fswebcam --delay 1 --no-banner -save /home/yourusername/Pictures/LID_OPEN_CAPTURES/%Y%m%d%H%M%S.jpeg

Which is the one that tells fswebcam to take the photo. Note because this script is run by the system (root) we have to use the full path of where to store the photos instead of the ~/ home short cut. (least that is the way I got it to work with my limited linux understanding).

The rest of the lid.sh script is left as is. Once saved it should be ready to work. Close the lid, open lid and camera LED will briefly light and the photo should be stored in the specified folder. That's it!

fswebcam can be configured to run further commands so it could in theory be used to trigger for example an ftp script which uploads the photo to a webserver, send as an email or sms alert to a phone etc.

Trackback address for this post

Trackback URL (right click and copy shortcut/link location)

No feedback yet

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)