Well, I've had some time off and have been to the races (and was up by £10 on the day), sorted out the boy's 3rd birthday (involved a Thomas the Tank Engine Cake roughly the size of a Nissan Micra and me dressing up as Scooby Doo) and being able to get more than 6 hours sleep a night four nights in a row.
Anyway, today's offering is a little SpyCamera application. Now I've had a sneaking suspicion the boss and his wife share clinches in the office and this shocks my (and I'm sure yours) moral and ethical values to the *very* core. So the natural thing to do is to capture them in the act and...
- Blackmail them or...
- Blackmail them - but still release the photos on the web.
Now fine, they’re married so in effect they have nothing to worry about. But anything apart from Missionary position and that's it.
So - to the sting. I've got a little digital camera that acts as a web camera on my desk - all I need to do is develop an application that can communicate with the camera, take and retrieve the photos periodically and either...
- save the photo or...
- send the photo via email to somewhere else.
To do this - I've decided to operate the device as a digital camera rather than a web camera (e.g. streaming video). If you wanted to use the device as a webcam (or have only got a webcam) - I suggest you check out VFW (Video For Windows) and periodically save a frame to the clipboard or even DirectShow if you've got the bottle. So the following code should work with most Digital Cameras - but the purposes of this application, I've used a Creative PC880 Digital Camera because that's all I've got.
What I've used in the WIA (Windows Imaging Acquisition) 2.0 Library to communicate with the camera. Now as many of you already know, the WIA 1.0 library was released with XP free of charge as a more accessible interface to devices such as Webcams, Scanners and Digital Cameras. The 2.0 library (available as a free download here) expands on the previous version with a more intuitive scripting languages and more functionality. The download also includes the SDK and has examples on a load of example scenarios for use in a variety of languages. To use the library in .NET, simply install the library onto your machine and add the library (DLL) as a reference to your project.
So to the application, most of its commented - but it acts as a taskbar icon (NotifyIcon) that when expanded will show the last photo taken. The main process of the application runs off a timer that simply sends the appropriate command to the camera to take a photo. The camera, once it’s taken its photo generates an event that is captured in our application. We then retrieve the photo from the camera, generate an event to display the image, save the image and in this case send the image to a specified email address. Note: for this you will require a valid SMTP server.
The most interesting element of the application is WIA side of things and assuming your camera *can* accessed by WIA (which most digital cameras should) then you should be OK.
Screenshot (see it does work!)
- WIA 2.0 is supposed to only work with XP SP1 or above. Nut I've got a sneaky suspicion that there is no reason that a .NET application with the InterOp.WIA assembly as a private assembly should not work. You never know and if you do try it, drop me a line with the results.
- The email send works, however, with my copy of the application it screws up with the attachment. I know the image is being saved correctly and I had success in the past sending emails with attachments in this way.
- On my machine the time being taking and receiving a photo back can be around the 5 to 10 second mark. Not brilliant. In addition, sending multiple commands is not a good idea either - hence the '_processing' flag.
- Don't use this code with a camera that already has photos on it. The cleanup code removes all photo files from the memory (card) so back them up first.
- This is NOT production code.
And that's it. You can use this application for less unsavoury requirements such as this, however, I don't recommend we go down that whole 'Ricky Martin, Teenage Girl and tin of dog food' route. I've provided the source within a ZIP archive for simplicity sake, which you can find below.
Have fun - M