views:

24590

answers:

8

How can I continuously capture images from a webcam?

I want to experiment with object recognition (by maybe using java media framework).

I was thinking of creating two threads

one thread:

  • Node 1: capture live image
  • Node 2: save image as "1.jpg"
  • Node 3: wait 5 seconds
  • Node 4: repeat...

other thread:

  • Node 1: wait until image is captured
  • Node 2: using the "1.jpg" get colors from every pixle
  • Node 3: save data in arrays
  • Node 4: repeat...
A: 

I believe the web-cam application software which comes along with the web-cam, or you native windows webcam software can be run in a batch script(windows/dos script) after turning the web cam on(i.e. if it needs an external power supply). In the bacth script , u can add appropriate delay to capture after certain time period. And keep executing the capture command in loop.

I guess this should be possible

-AD

goldenmean
A: 

Java usually doesn't like accessing hardware, so you will need a driver program of some sort, as goldenmean said. I've done this on my laptop by finding a command line program that snaps a picture. Then it's the same as goldenmean explained; you run the command line program from your java program in the takepicture() routine, and the rest of your code runs the same.

Except for the part about reading pixel values into an array, you might be better served by saving the file to BMP, which is nearly that format already, then using the standard java image libraries on it.

Using a command line program adds a dependency to your program and makes it less portable, but so was the webcam, right?

Karl
+2  A: 

I have used JMF on a videoconference application and it worked well on two laptops: one with integrated webcam and another with an old USB webcam. It requires JMF being installed and configured before-hand, but once you're done you can access the hardware via Java code fairly easily.

ArnauVP
A: 

There's a pretty nice interface for this in processing, which is kind of a pidgin java designed for graphics. It gets used in some image recognition work, such as that link.

Depending on what you need out of it, you might be able to load the video library that's used there in java, or if you're just playing around with it you might be able to get by using processing itself.

Dan Monego
+2  A: 

Here is a similar question with some - yet unaccepted - answers. One of them mentions FMJ as a java alternative to JMF.

rics
A: 

FMJ can do this, as can the supporting library it uses, LTI-CIVIL. Both are on sourceforge.

A: 

http://grack.com/downloads/school/enel619.10/report/java%5Fmedia%5Fframework.html

Using the Player with Swing

The Player can be easily used in a Swing application as well. The following code creates a Swing-based TV capture program with the video output displayed in the entire window:

import javax.media.*;
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import javax.swing.event.*;

public class JMFTest extends JFrame {
    Player _player;
    JMFTest() {
        addWindowListener( new WindowAdapter() {
            public void windowClosing( WindowEvent e ) {
                _player.stop();
                _player.deallocate();
                _player.close();
                System.exit( 0 );
            }
        });
          setExtent( 0, 0, 320, 260 );
        JPanel panel = (JPanel)getContentPane();
        panel.setLayout( new BorderLayout() );
        String mediaFile = "vfw://1";
        try {
            MediaLocator mlr = new MediaLocator( mediaFile );
            _player = Manager.createRealizedPlayer( mlr );
            if (_player.getVisualComponent() != null)
            panel.add("Center", _player.getVisualComponent());
            if (_player.getControlPanelComponent() != null)
            panel.add("South", _player.getControlPanelComponent());
        }
        catch (Exception e) {
            System.err.println( "Got exception " + e );
        }
    }

    public static void main(String[] args) {
        JMFTest jmfTest = new JMFTest();
        jmfTest.show();
    }
}
please reformat your answer... use the code markup button.
Trevor Harrison
A: 

Recommand using FMJ for multimedia relatived java app.

Rose