aspose file tools*
The moose likes Java in General and the fly likes JMF application not detecting web camera Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "JMF application not detecting web camera" Watch "JMF application not detecting web camera" New topic
Author

JMF application not detecting web camera

Manish Sridharan
Ranch Hand

Joined: Jul 19, 2005
Posts: 64
Hi!
I am facing couple of problem regarding JMF........
Some information about my Computer..
I am using windows2003 operating system...
2.2 ghz intel 82845G chipset
512 MB Ram
JDK 1.5 and JMF2.1.1e
I am using largan chameleon digital camera...

all class path and path are already set ...
my application is compiling perfectly...

i have also installed vfw video capture driver provided by videum company.. since i learned that JMF not supports WDM DRIVERS...

Now when executed jmfinit.exe it prints folloowing lines...

C:\Program Files\JMF2.1.1e\bin>jmfinit
Could not create DirectSound
DirectSound Capture Supported = true
JavaSound Capture Supported = false
Found device Microsoft WDM Image Capture
Querying device. Please wait...
Found device Videum Video Capture
Querying device. Please wait...

then the it terminates....

I think this part wrks fine

after that i ran my application ....

here ,

Vector list = CaptureDeviceManager.getDeviceList ( null );

list.size() comes as 0 .... no device is detected by application..
and the application terminates...


Now please suggest me where am i going wrong ?
I am posting the code below

import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
import javax.media.*;
import javax.media.format.*;
import javax.media.util.*;
import javax.media.control.*;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;

import com.sun.image.codec.jpeg.*;



import com.sun.media.protocol.vfw.VFWCapture;

public class JWebCam extends JFrame
implements WindowListener,
ComponentListener
{
protected final static int MIN_WIDTH = 320;
protected final static int MIN_HEIGHT = 240;

protected static int shotCounter = 1;

protected JLabel statusBar = null;
protected Component visualComponent = null;

protected JToolBar toolbar = null;
protected MyToolBarAction formatButton = null;
protected MyToolBarAction captureButton = null;

protected Player player = null;
protected CaptureDeviceInfo webCamDeviceInfo = null;
protected MediaLocator ml = null;
protected Dimension imageSize = null;
protected FormatControl formatControl = null;

protected VideoFormat currentFormat = null;
protected Format[] videoFormats = null;
protected MyVideoFormat[] myFormatList = null;

/* --------------------------------------------------------------
* Constructor
* -------------------------------------------------------------- */

public JWebCam ( String frameTitle )
{
super ( frameTitle );

try
{
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
}
catch ( Exception cnfe )
{
System.out.println ("Note : Cannot load look and feel settings");
}

addWindowListener ( this );
addComponentListener ( this );

getContentPane().setLayout ( new BorderLayout() );

statusBar = new JLabel ("Initialising...");
statusBar.setBorder ( new EtchedBorder() );

getContentPane().add ( statusBar, BorderLayout.SOUTH );

setSize ( 320, 260 ); // default size...

setVisible ( true );
}

/* --------------------------------------------------------------
* Initialise
*
* @returns true if web cam is detected
* -------------------------------------------------------------- */

public boolean initialise ( )
throws Exception
{
return ( initialise ( autoDetect() ) );
}

/* -------------------------------------------------------------------
* Initialise
*
* @params _deviceInfo, specific web cam device if not autodetected
* @returns true if web cam is detected
* ------------------------------------------------------------------- */

public boolean initialise ( CaptureDeviceInfo _deviceInfo )
throws Exception
{
webCamDeviceInfo = _deviceInfo;

if ( webCamDeviceInfo != null )
{
statusBar.setText ( "Connecting to : " + webCamDeviceInfo.getName() );

try
{
// setUpMenu ( );

setUpToolBar();

getContentPane().add ( toolbar, BorderLayout.NORTH );

ml = webCamDeviceInfo.getLocator();
if ( ml != null )
{
player = Manager.createRealizedPlayer ( ml );
if ( player != null )
{
formatControl = (FormatControl)player.getControl ( "javax.media.control.FormatControl" );

videoFormats = webCamDeviceInfo.getFormats();

setFormat ( (VideoFormat)videoFormats[0] );

// player.start();

visualComponent = player.getVisualComponent();
getContentPane().add ( visualComponent, BorderLayout.CENTER );

invalidate();
pack();

setSize ( imageSize.width, imageSize.height + statusBar.getHeight() + toolbar.getHeight() );

myFormatList = new MyVideoFormat[videoFormats.length];
for ( int i=0; i<videoFormats.length; i++ )
{
myFormatList[i] = new MyVideoFormat ( (VideoFormat)videoFormats[i] );
}
}
}
else
{
statusBar.setText ( "No Media Locator for : " + webCamDeviceInfo.getName() );
}

return ( true );
}
catch ( IOException ioEx )
{
statusBar.setText ( "Connecting to : " + webCamDeviceInfo.getName() );

return ( false );
}
catch ( NoPlayerException npex )
{
statusBar.setText ("Cannot create player");
return ( false );
}
catch ( CannotRealizeException nre )
{
statusBar.setText ( "Cannot realize player");
return ( false );
}
}
else
{
System.out.println("I am null ");
return ( false );
}
}



/* -------------------------------------------------------------------
* Dynamically create menu items
*
* @returns the device info object if found, null otherwise
* ------------------------------------------------------------------- */

public void setFormat ( VideoFormat selectedFormat )
{
if ( formatControl != null )
{
player.stop();

statusBar.setText ( "Format : " + selectedFormat );

imageSize = selectedFormat.getSize();

formatControl.setFormat ( selectedFormat );

currentFormat = selectedFormat;

setSize ( imageSize.width, imageSize.height + statusBar.getHeight() + toolbar.getHeight() );

player.start();
}
else
{
System.out.println ("Visual component not an instance of FormatControl");
statusBar.setText ( "Visual component cannot change format" );
}
}

protected void setUpToolBar ( )
{
toolbar = new JToolBar();

formatButton = new MyToolBarAction ( "Format", "BtnFormat.jpg" );
captureButton = new MyToolBarAction ( "Capture", "BtnCapture.jpg" );

if ( formatButton != null )
{
toolbar.add ( formatButton );
}
toolbar.add ( captureButton );

getContentPane().add ( toolbar, BorderLayout.NORTH );
}


protected void toolbarHandler ( MyToolBarAction actionBtn )
{
if ( actionBtn == formatButton )
{
// JOptionPane.showConfirmDialog ( this, "Format Dialog" );

Object selected = JOptionPane.showInputDialog (this,
"Select Video format",
"Capture format selection",
JOptionPane.INFORMATION_MESSAGE,
null,//Icon icon,
myFormatList, // videoFormats,
currentFormat );

if ( selected != null )
{
setFormat ( ((MyVideoFormat)selected).format );
}
}
else if ( actionBtn == captureButton )
{
Image photo = grabFrameImage ( );
MySnapshot snapshot = new MySnapshot ( photo, new Dimension ( imageSize ) );
}
}




/* -------------------------------------------------------------------
* autoDetects the first web camera in the system
*
* @returns the device info object if found, null otherwise
* ------------------------------------------------------------------- */

public CaptureDeviceInfo autoDetect ( )
{
Vector list = CaptureDeviceManager.getDeviceList ( null );
CaptureDeviceInfo devInfo = null;
System.out.println("i am in autodetect ");

if ( list != null )
{
String name;
System.out.println("i am in autodetect if condition . size is "+list.size());

for ( int i=0; i >< list.size(); i++ )
{
devInfo = (CaptureDeviceInfo)list.elementAt ( i );
name = devInfo.getName();
System.out.println("Device Name : "+name);

if ( name.startsWith ("vfw:") )
{
break;
}
}

if ( devInfo != null && devInfo.getName().startsWith("vfw:") )
{
return ( devInfo );
}
else
{
for ( int i = 0; i < 10; i++ )
{
try
{
name = VFWCapture.capGetDriverDescriptionName ( i );
if (name != null && name.length() > 1)
{
devInfo = com.sun.media.protocol.vfw.VFWSourceStream.autoDetect ( i );
if ( devInfo != null )
{
return ( devInfo );
}
}
}
catch ( Exception ioEx )
{
// ignore errors detecting device
statusBar.setText ( "AutoDetect failed : " + ioEx.getMessage() );
}
}
return ( null );
}
}
else
{
System.out.println("i am in autodetect else null");
return ( null );
}
}


/* -------------------------------------------------------------------
* deviceInfo
*
* @note outputs text information
* ------------------------------------------------------------------- */

public void deviceInfo ( )
{
if ( webCamDeviceInfo != null )
{
Format[] formats = webCamDeviceInfo.getFormats();

if ( ( formats != null ) && ( formats.length > 0 ) )
{
}

for ( int i=0; i<formats.length; i++ )
{
Format aFormat = formats[i];
if ( aFormat instanceof VideoFormat )
{
Dimension dim = ((VideoFormat)aFormat).getSize();
System.out.println ("Video Format " + i + " : " + formats[i].getEncoding() + ", " + dim.width + " x " + dim.height );
}
else
{
System.out.println ("Format " + i + " : " + formats[i].getEncoding() );
}
}
}
else
{
System.out.println ("Error : No web cam detected");
}

}

/* -------------------------------------------------------------------
* grabs a frame's buffer from the web cam / device
*
* @returns A frames buffer
* ------------------------------------------------------------------- */

public Buffer grabFrameBuffer ( )
{
if ( player != null )
{
FrameGrabbingControl fgc = (FrameGrabbingControl)player.getControl ( "javax.media.control.FrameGrabbingControl" );
Buffer buf = fgc.grabFrame();

return ( buf );
}
else
{
return ( null );
}
}

/* -------------------------------------------------------------------
* grabs a frame's buffer, as an image, from the web cam / device
*
* @returns A frames buffer as an image
* ------------------------------------------------------------------- */

public Image grabFrameImage ( )
{
Buffer buffer = grabFrameBuffer();
if ( buffer != null )
{
// Convert it to an image
BufferToImage btoi = new BufferToImage ( (VideoFormat)buffer.getFormat() );
Image image = btoi.createImage ( buffer );

return ( image );
}
else
{
System.out.println ("Error : Buffer grabbed is null");
return ( null );
}
}


/* -------------------------------------------------------------------
* Closes and cleans up the player
*
* ------------------------------------------------------------------- */

public void playerClose ( )
{
if ( player != null )
{
player.close();
player.deallocate();
player = null;
}
}

public void windowClosing ( WindowEvent e )
{
playerClose();
System.exit ( 1 );
}

public void componentResized(ComponentEvent e)
{
Dimension dim = getSize();
boolean mustResize = false;

if ( dim.width >< MIN_WIDTH )
{
dim.width = MIN_WIDTH;
mustResize = true;
}

if ( dim.height < MIN_HEIGHT )
{
dim.height = MIN_HEIGHT;
mustResize = true;
}

if ( mustResize )
setSize ( dim );
}

public void windowActivated ( WindowEvent e ) { }

public void windowClosed ( WindowEvent e ) { }

public void windowDeactivated ( WindowEvent e ){}

public void windowDeiconified ( WindowEvent e ){}

public void windowIconified ( WindowEvent e ){}

public void windowOpened ( WindowEvent e ){}

public void componentHidden(ComponentEvent e) { }

public void componentMoved(ComponentEvent e){}

public void componentShown(ComponentEvent e){}

protected void finalize( ) throws Throwable
{
playerClose();
super.finalize();
}


class MyToolBarAction extends AbstractAction
{
public MyToolBarAction ( String name, String imagefile )
{
super ( name, new ImageIcon ( imagefile ) );
}

public void actionPerformed ( ActionEvent event )
{
toolbarHandler ( this );
}
};


class MyVideoFormat
{
public VideoFormat format;

public MyVideoFormat ( VideoFormat _format )
{
format = _format;
}

public String toString ( )
{
Dimension dim = format.getSize();
return ( format.getEncoding() + " [ " + dim.width + " x " + dim.height + " ]" );
}
};



class MySnapshot extends JFrame
{
protected Image photo = null;
protected int shotNumber;


public MySnapshot ( Image grabbedFrame, Dimension imageSize )
{
super ( );

shotNumber = shotCounter++;
setTitle ( "Photo" + shotNumber );

photo = grabbedFrame;

setDefaultCloseOperation ( WindowConstants.DO_NOTHING_ON_CLOSE );

int imageHeight = photo.getWidth ( this );
int imageWidth = photo.getHeight ( this );

setSize ( imageSize.width, imageSize.height );

final FileDialog saveDialog = new FileDialog ( this, "Save JPEG", FileDialog.SAVE );
final JFrame thisCopy = this;
saveDialog.setFile ( "Photo" + shotNumber );

addWindowListener ( new WindowAdapter()
{
public void windowClosing ( WindowEvent e )
{
saveDialog.show();

String filename = saveDialog.getFile();

if ( filename != null )
{
if ( saveJPEG ( filename ) )
{
JOptionPane.showMessageDialog ( thisCopy, "Saved " + filename );
setVisible ( false );
dispose();
}
else
{
JOptionPane.showMessageDialog ( thisCopy, "Error saving " + filename );
}
}
else
{
setVisible ( false );
dispose();
}
}
}
);

setVisible ( true );
}


public void paint ( Graphics g )
{
g.drawImage ( photo, 0, 0, getWidth(), getHeight(), this );
}


/* -------------------------------------------------------------------
* Saves an image as a JPEG
*
* @params the image to save
* @params the filename to save the image as
* ------------------------------------------------------------------- */

public boolean saveJPEG ( String filename )
{
boolean saved = false;
BufferedImage bi = new BufferedImage ( photo.getWidth(null),
photo.getHeight(null),
BufferedImage.TYPE_INT_RGB );
Graphics2D g2 = bi.createGraphics();
g2.drawImage ( photo, null, null );
FileOutputStream out = null;

try
{
out = new FileOutputStream ( filename );
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder ( out );
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam ( bi );
param.setQuality ( 1.0f, false );
encoder.setJPEGEncodeParam ( param );

encoder.encode ( bi );
out.close();
saved = true;
}
catch ( Exception ex )
{
System.out.println ("Error saving JPEG : " + ex.getMessage() );
}

return ( saved );
}

}// of MySnapshot


public static void main (String[] args )
{
try
{
JWebCam myWebCam = new JWebCam ( "TimeSlice Web Cam Capture" );

if ( myWebCam.initialise() )
{
// myWebCam.deviceInfo(); // outputs device info
}
else
{
System.out.println ("Web Cam not detected / initialised");
}
}
catch ( Exception ex )
{
ex.printStackTrace();
}
}
}







One more Question :- Does JMF2.1.1e window performance pack supports both vfw and wdm ?

Another Question :


when i installed the largan chameleon digital camera driver provided with camera .... then if i do jmfinit it creates the memory dump and system reboots...

Please help me in this regard


Gaurav Nigam


Manish S.
Krishna Latha Grandhi
Ranch Hand

Joined: Nov 08, 2005
Posts: 110
Hi gaurav,

as per my knowledge,

you have to run jmfinit and then jmfregistry

If your device is listed in jmfregistry devices..It will work..

Regards,
Krishna.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JMF application not detecting web camera