0

I am using netbeans IDE 7.1.1 I'm making the application capture an image from a webcam, but my problem is that when I run that application first time it gives me proper output but when I run the same application again, it gives me this error:

java.io.IOException: Could not connect to capture device
javax.media.NoDataSourceException: Error instantiating class: com.sun.media.protocol.vfw.DataSource : java.io.IOException: Could not connect to capture device
    at javax.media.Manager.createDataSource(Manager.java:1012)

can anyone tell me why this is happening?

my code is:

             /* Grab the default web cam*/
             MediaLocator ml = new MediaLocator("vfw://0");

        /* Create my data source */
        DataSource ds = Manager.createDataSource(ml);

        requestFormatResolution(ds);

        /* Create & start my player */
        Player p = Manager.createRealizedPlayer(ds);

        p.start();
            Component videoScreen = p.getVisualComponent();
Austin
  • 4,801
  • 6
  • 34
  • 54
Jayashri
  • 366
  • 4
  • 13
  • 25
  • It seems like you're not closing the resource. Are you sure you've included all relevant bits of code (including the closing of your `Player`)? – Makoto Jun 18 '12 at 03:27
  • no in whole application I cannot close any resource is it necessary to close the resource ... here can I close only player or all resources – Jayashri Jun 18 '12 at 03:30

1 Answers1

2

You need to close your Player and deallocate it as well.

Simply do the following:

player.close();
player.deallocate();

For more details of these methods, look at the API ( close() and deallocate() ).

Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
  • It works but it automatically capture the image and stops. how to capture image on click? – Jayashri Jun 18 '12 at 05:30
  • Implement a `ActionListener` to your click event, and once you are done with that, then call the above two lines. Remember , they are to be called only when you are done with your `Player` resources. – Kazekage Gaara Jun 18 '12 at 05:34
  • but the button for capture image is inbuilt in video screen? how should I implement action listener for that? – Jayashri Jun 18 '12 at 05:38
  • I haven't actually worked with it. You can look at [this](http://stackoverflow.com/questions/276292/capturing-image-from-webcam-in-java) and [this](http://sunjava-expert.blogspot.in/2011/05/capture-image-from-webcam-java-code.html) for further help. Maybe using some library might ease your work. – Kazekage Gaara Jun 18 '12 at 05:44
  • hey Kazekage thanks for the help. Its working fine as I click it is capturing image. :) – Jayashri Jun 18 '12 at 06:44
  • @Jayashri you might want to upvote the answer as well, if it was helpful. :-) – Kazekage Gaara Jun 18 '12 at 06:48