Update 2013: to everyone reading this, this article is old! Links are broken and the method described is probably no longer the best way to access your webcam in Java. Please do not ask me for help or support. I cannot help you. You have to figure stuff out for yourself, that’s the best way to learn.
These days there’s not a lot of Java applets left on the interwebs. Off the top of my head, the only one I can think of that I still use is the one on keepvid.com, and I’m mighty suspicious of it every time I run it. This article is by no means a pro-applet one, although I do believe they still have their uses. Whether or not accessing the webcam is a valid usage is something I’ll leave you to decide.
Some background info: I researched the wonderful OpenCV library a while back when looking into image recognition. It has a Java port called JavaCV which allows you to access the webcam in Java. Another personal project of mine dealt with QR codes, so I felt compelled to somehow combine the two into a single technolurgic monstrosity. The result was a bit hacked together, but it works. So I thought I’d share it.
Get the source code here: https://github.com/rheide/WebcamApplet
Or just get the Eclipse project from here: http://colorfulwolf.com/dev/cam/WebcamApplet.zip
Lastly, for a demonstration, go here: http://colorfulwolf.com/dev/cam/demo.html
Here’s how it works. JavaCV uses a library called JNA (Java Native Access) to directly access the OpenCV libraries on the client’s computer. But for that to work, the libraries must first be present. The applet, after starting, downloads the OpenCV libraries to a temp folder, and then sets the system property jna.library.path to include that temp folder. Then the webcam can be initialized and accessed via JavaCV.
The whole download is quite heavy. The jar libraries weigh in at about 2 megabyte, and the opencv native libraries are another 2.7 megabyte (compressed). I made it so that the opencv libraries are not downloaded again after the first run, which helps a bit. Still, it’s a long way from just doing a simple API call.
Any feedback is appreciated of course. Feel free to fork the github code and play with it, as I probably won’t have time to work on this in the future.