Accessing the webcam from inside a Java Applet

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.

The demo recognizes QR codes and displays the corresponding text in a textbox in the browser. It’s possible to call a Javascript function from inside the applet, so you can send your results to the browser, which is then free to do anything it wants with it, including making an AJAX request to fetch information based on the QR code.

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.

 

Posted in Tech | 25 Comments

25 Responses to Accessing the webcam from inside a Java Applet

  1. Yves says:

    Thanks, just when I was beginning to give up on Java being able to access the web-camera I find this little gem :). Are there any additional libraries I need to download to get the Eclipse project working?

  2. randy says:

    I believe everything’s included. Note that you will probably have to host it somewhere to get it to work. When I tried it on Windows the applet wouldn’t load the required jar files, even if they were placed in the same folder as the html file. Since the webcam component is a Swing component you can just add it to a JFrame and use that for testing.

  3. Jeffry says:

    Hi, could you also provide tutorial to run it on desktop ? That would be great. Thank you.

    Jeffry

  4. randy says:

    Sorry, that’d be out of the scope of this article. (Read: I can’t be bothered :P) It’s fairly easy to do though, just check the applet source code for how it calls the webcam and put it in a JFrame instead.

  5. Michael says:

    Hi, I’ve read the code and realized some of the key classes you use, particular for accessing the local webcam, are from com.googlecode.javacv.OpenCVFrameGrabber. is there any particular reason you did so?
    Also, do you know how to create a DataSource type for the grabbed images in your code so I can use RTP (from JMF) to stream the webcam video?

    wonderful work, by the way

  6. randy says:

    Hi Michael, that’s a very interesting idea, but I’m afraid I haven’t worked with JMF a lot so I can’t help you there. As for the OpenCVFrameGrabber, I’m not sure I understand your question. I use it because that’s the entry point to OpenCV from Java, which gets you access to the webcam.

  7. kris says:

    Hi, Thanks for your tut, which is absolutely helpful. I got a question about i need to capture webcam image by clicking a button and i need to pass it to the jsp. So could please advice where exactly i need to add this changes in your app,

    Thanks,
    Kris

  8. Kirill says:

    In the function cvCreateCameraCapture(i) I see
    ———————————————————————
    Exception in thread “Thread-4″ java.lang.UnsatisfiedLinkError: Unable to load library ‘cxcore’: The specified module
    ———————————————————————

    How to Fix that ?

  9. Sumit Khanna says:

    Hey i got the same excepiton
    ————————————————————————
    Exception in thread “Thread-4″ java.lang.UnsatisfiedLinkError: Unable to load library ‘cxcore’: The specified module
    ————————————————————————

    Can you tell why is this exception coming??

  10. ket says:

    I’ve been searching for years only to learn that PHP and Ajax can also do this job. And for screen capture, Javascript and HTML5 can be easily make it with a breeze too.

  11. Hello! I’m coding an webapp where I need to get a fingerprint from a device, and I’m trying to do this based on your solution. But I’m getting some trouble accessing the files through JNA. Firstly, I’ve permission troubles. Secondly, I’m getting a exception “java.lang.NoClassDefFoundError” saying that Java cannot initialize com.sun.jna.Native. Can you help me to configure the jars and permissions to put this to run? Thanks (:

  12. Bhooshan says:

    Hello Randy,
    I am able to access webcam using applet, but when it showed to the QR code nothing is happening, is there button to capture the code or it will automatically take care of everything once the it read QR code.

  13. Shilpi says:

    I am unable to use this applet through MAC systems. Please let me know how I can use this applet to record video through MAC systems.

    • Andras says:

      Hi Shilpi,

      this could be because Randy’s code only download windows specific dependencies. WebcamApplet.java line 56.
      private final static String[] LIBS = { … }

      You can change this list to Mac OS specific ones but in this case you should update DEFAULT_DOWNLOAD_PATH as well.

      • Shilpi says:

        Thank you Andras, but where do I find the dependent library for MAC machine…..please help…

  14. Murat says:

    I downloaded project via this link http://colorfulwolf.com/dev/cam/WebcamApplet.zip
    but in local web-site form does not working
    open demo.html but browser can not open

  15. Unfortunately this applet does not work in my case. Have no idea why (I use Java 7, maybe this is the root cause). It shows some progress bars informing me about files being downloaded, but after this I see only blank white screen… Nothing happens and camera don’t start.

    Therefore, I’ve created my own Applet demo of how to use PC webcam in Java Applet (it does not use OpenCV / JavaCV !!!). Source code can be found in the examples for my Webcam Capture project https://github.com/sarxos/webcam-capture/tree/master/webcam-capture-examples/webcam-capture-applet

    And here you can find working example http://webcam-capture.sarxos.pl/examples/applet/

    I tested it on my home PC running JDK 1.7 and at my work PC running JDK 1.6. In both cases it works perfectly.

    Cheers!

    • Hassan Arif says:

      Dear respected,

      I am also trying to run this example. But my compiled applet is not running. Can you help me how to run this example, so that I can proceed with my work?

      Kind regards,
      Hassan Arif

  16. Fabian says:

    You made my day dude. Thanks for your project, it really helped me!

  17. Khushbu says:

    I am unable to run it.. Please post where and what settings to be done in order to execute it
    Thank you

  18. mlong says:

    Hey i got the same excepiton

    Exception in thread “Thread-4″ java.lang.UnsatisfiedLinkError: Unable to load library ‘cxcore’: The specified module
    I tried to correct it by : properties my project,java build path,libraries and add native library location *.dll
    but i can’t fix it :| help me

  19. maha says:

    i got green images
    why ?

  20. Ivan says:

    Demo page 404? please fix it!!

  21. webcamcover says:

    So what do these guys do by hacking our cam? I mean, is it just like

    recording us or hidden camera, right? I’m so much worried about the

    women who would be an easy prey for these shitty hackers. Anyway,

    found something that would be really helpful for us, see here:

    http://www.eyebloc.com

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>