52

I need to access web camera using Java. This is what I want to do

  1. Access web cam

  2. Now the user can see web cam working because his face is visible on screen (have heard some libs are there which doesn't show the video output of webcam)

  3. when user click save button, take a snapshot and save it

I have tried number of ways to do this, from a long time.

  1. JMF - Now it is dead
  2. FMJ - Now it is dead too
  3. VLCJ - too much because I am not creating a music/video player and it expect VLC to be installed
  4. Xuggler - too much and hard work
  5. JMyron - didn't work
  6. JavaFX - I thought it could do it, but seems like it can't

I am even satisfied if the library is just ONLY doing the above mentioned, because that's enough for me. But I expect it to be simple too. Really great if it is not using DLLs, because it is not platform independent if it does. Really appreciate if it can DETECT the camera, without manually passing the camera name and other info as have do in VLCJ (because there might be thousands of camera brands, so I can't create a list of thousand elements in it). And, I am creating a desktop application, not web app.

If you know a library like this, please be kind enough to let me know. Other libraries (which might not suit to all of my requirements, but suits to the basic requirement) also welcome. Please help

Pulah Nandha
  • 768
  • 4
  • 23
PeakGen
  • 18,056
  • 71
  • 208
  • 385
  • There might be thousands of cameras? Really? Are you writing this for a security firm or something? Also, a list of thousands of elements isn't bad - you can always filter it on the UI or make it searchable somehow. – corsiKa Jul 14 '12 at 20:55
  • 4
    @corsiKa: No, I mean, if I am going to give it to other people, I can't modify the program each and everytime, giving their camera name. You know, there should be a way it should work with all (or at least list of) camera brands. In VLC, we don't pass the camera name, it identifies it. That's what I mean actually. – PeakGen Jul 14 '12 at 21:04

2 Answers2

79

I think the project you are looking for is: https://github.com/sarxos/webcam-capture (I'm the author)

There is an example working exactly as you've described - after it's run, the window appear where, after you press "Start" button, you can see live image from webcam device and save it to file after you click on "Snapshot" (source code available, please note that FPS counter in the corner can be disabled):

snapshot

The project is portable (WinXP, Win7, Win8, Linux, Mac, Raspberry Pi) and does not require any additional software to be installed on the PC.

API is really nice and easy to learn. Example how to capture single image and save it to PNG file:

Webcam webcam = Webcam.getDefault();
webcam.open();
ImageIO.write(webcam.getImage(), "PNG", new File("test.png"));
Bartosz Firyn
  • 2,524
  • 1
  • 19
  • 18
  • @Bartosz, Isn't this only local? How would you pass the data to another client directly to do webcam? – Pacerier Jan 31 '15 at 07:48
  • @Pacerier, you can either use IP camera driver to get data from remote webcam (if camera supports JPEG or MJPEG, demo available in source code) or expose image from local camera via WebcamStreamer (very limited but working web server that serves MJPEG stream). There is also example in source code demonstrating how to stream image frames as h.264 (just a proof-of-concept, though) using xuggler. – Bartosz Firyn Jan 31 '15 at 09:08
  • @BartoszFiryn, Is this how iTalki, Google Helpout, clarity.fm, LiveNinja, etc works? How would this work since about every firewall **blocks** traffic unless its http(s)? – Pacerier Feb 01 '15 at 13:58
  • @Pacerier, the MJPEG (and JPEG) is transmitted over HTTP(s). Community also reported that WebSockets can be used to transmit images to the front-end. Generally, Webcam Capture API has not been designed to be a streaming framework, but a simple and useful library to access the IP camera or UVC webcam. The goal is to give user possibility to access BufferedImage from camera, and what will user do with this image is the open question. It can be streamed with some other framework, saved to file, used on display, sent to server, etc. – Bartosz Firyn Feb 02 '15 at 12:31
  • @BartoszFiryn, The sizes of videos and images are magnitudes apart. Also, live video like webcam needs to be.... live. Static images have the luxury to afford 1 or 2 seconds or delay and users won't be much bothered. For webcams, how do you deal with latency and bandwidth? – Pacerier Feb 02 '15 at 14:48
  • 1
    @Pacerier, I do not deal with latency and bandwidth since this library is not intended for streaming over the internet. Please take a look on the project page and/or feel free to open new ticket if you have any additional questions so we do not spam stackoverflow with unnecessary comments. – Bartosz Firyn Feb 03 '15 at 12:51
  • 1
    This library actually seems to work (tested on Linux). Ported to JavaX [here](http://tinybrain.de/1008989) – Stefan Reich Jun 22 '17 at 17:45
  • Wow, this library is amazing. Thanks so much! Easiest I've used by far. Examples worked right out of the box and all I had to do was add the maven dependency and code sample. Note: If you put `webcam.getImage()` in a loop you can continually capture the current image. :) – Kenny Cason Nov 29 '17 at 02:04
  • @BartoszFiryn hi, i know this is an old thread, but can i do the reverse action with your framework? i want to feed my webcam with static images for some purposes – thekucays Dec 05 '18 at 18:31
  • somewhy doesn't work on ubuntu / atom cpu – user_x Dec 21 '20 at 11:42
10

This has been discussed on SO multiple times. Here are a few links to get you started:

SO: Capturing image from webcam in java?

SO: What is the best method to capture images from a live video device for use by a Java-based application?

openCVF applet: http://www.colorfulwolf.com/blog/2011/07/05/accessing-the-webcam-from-inside-a-java-applet/

config: http://ganeshtiwaridotcomdotnp.blogspot.in/2011/12/opencv-javacv-eclipse-project.html

Community
  • 1
  • 1
  • 1
    Thanks for the reply. So, are you suggesting JavaCV? – PeakGen Jul 14 '12 at 21:13
  • 1
    Another question, why most of JavaCV articles suggest to install "Microsoft Visual C++ Redistributable Packages" ? Just because we need to get the DLL or something else? – PeakGen Jul 14 '12 at 21:20