1

For some reason this code can't seem to find the built in webcam:

package com.lorenjz.smooth;

import java.awt.BorderLayout;
import javax.media.*;
import javax.media.protocol.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import javax.media.control.FormatControl;
import javax.media.format.VideoFormat;

public class test {
    public static void main(String[] args){
        test t = new test();
        t.getCam();
    }
    public void getCam(){
    try{ 
        /* Grab the default web cam*/
        MediaLocator ml = new MediaLocator("vfw://2");
        /* Create my data source */
        DataSource ds = Manager.createDataSource(ml);
        requestFormatResolution(ds);
        /* Create & start my player */
        Player p = Manager.createRealizedPlayer(ds);
        p.start();
        Thread.currentThread().sleep(1000);

        /* code for creating a JFrame and adding the visual component to it */
        JFrame jfrm=new JFrame("Testing Webcam");
                jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                if(p.getVisualComponent()!=null)jfrm.getContentPane().add(p.getVisualComponent());
                if(p.getControlPanelComponent()!=null)jfrm.getContentPane().add
                        (p.getControlPanelComponent(),BorderLayout.SOUTH);
                jfrm.pack();
                jfrm.setLocationRelativeTo(null);
                jfrm.setVisible(true);
                jfrm.setSize(320,240);
       }catch(Exception e){
        e.printStackTrace();        
      }
    }  
    public boolean requestFormatResolution(DataSource ds) {
        if (ds instanceof CaptureDevice) {
           FormatControl[] fcs = ((CaptureDevice) ds).getFormatControls();
            for (FormatControl fc : fcs) {
                Format[] formats = ((FormatControl) fc).getSupportedFormats();
               for (Format format : formats) {
                    if ((format instanceof VideoFormat) &&
                        (((VideoFormat)format).getSize().getHeight() <= 240) &&
                        (((VideoFormat)format).getSize().getWidth()  <= 320)) {
                            ((FormatControl) fc).setFormat(format);
                            return true;
                    }
                }
            }
        }
        return false;
    }
}

The console returns the following error:

javax.media.NoDataSourceException: Cannot find a DataSource for: vfw://0
at javax.media.Manager.createDataSource(Manager.java:1037)
at com.lorenjz.smooth.test.getCam(test.java:21)
at com.lorenjz.smooth.test.main(test.java:14)

I've tried to find a different argument to this line: MediaLocator ml = new MediaLocator("vfw://2"); but I have not been able.

Can anyone provide any recommendations?

Thanks,

Loren

Loren Zimmer
  • 482
  • 1
  • 6
  • 29

1 Answers1

0

AFAIK, "vfw://" is the scheme for "Video For Windows" objects... Obviously this is platform-specific.

You should use CaptureDeviceManager.getDeviceList(null) to see which devices are available on your system, and choose from these options.

onon15
  • 3,620
  • 1
  • 18
  • 22
  • that makes sense to me. when I run:`Vector devices = CaptureDeviceManager.getDeviceList(null);` devices comes back null. – Loren Zimmer Nov 11 '12 at 15:28
  • That's unfortunate. [Answers to another question](http://stackoverflow.com/questions/3750568/get-webcam-stream-on-mac-os-x-in-java) on SO claims that there's no MAC support. – onon15 Nov 11 '12 at 15:53
  • Ok I've tried the same thing in windows with the same results. Any thoughts as to where to go from here. – Loren Zimmer Nov 12 '12 at 19:08
  • The problem with JMF (javax.media) is that the project has been abandoned for almost 10 years now. I wouldn't put more effort into trying to make the capture work with JMF, but rather try to use other media libraries, such as [FMJ](http://fmj-sf.net/) or [vlcj](http://code.google.com/p/vlcj/). – onon15 Nov 13 '12 at 08:09