7

I'm trying to capture the video using the jxcapture. I manage to do so just for once but when I'm trying to capture second time the video in the same program I got into troubles. My code is the following:

public VideoCapture videoCapture = VideoCapture.create(VideoFormat.WMV);
public CaptureVideoFromWebCamera(){}

public void start(String filename){


    List<VideoSource> availableVideoSources = VideoSource.getAvailable();
    System.out.println("availableVideoSources = " + availableVideoSources);

    if (availableVideoSources.isEmpty()) {
        throw new IllegalStateException("No external video sources available");
    }
    VideoSource webCamera = availableVideoSources.get(0);
    System.out.println("webCamera = " + webCamera);

    videoCapture.setVideoSource(webCamera);

    java.util.List<Codec> videoCodecs = videoCapture.getVideoCodecs();
    System.out.println("videoCodecs = " + videoCodecs);
    if (videoCodecs.isEmpty()) {
        throw new IllegalStateException("No video codecs available");
    }

    Codec videoCodec = videoCodecs.get(2);
    System.out.println("videoCodec = " + videoCodec);

    EncodingParameters encodingParameters = new EncodingParameters(new File("WebCamera.wmv"));
    encodingParameters.setBitrate(500000);
    encodingParameters.setFramerate(10);
    encodingParameters.setKeyFrameInterval(1);
    encodingParameters.setCodec(videoCodec);

    videoCapture.setEncodingParameters(encodingParameters);
    videoCapture.start();
    System.out.println("Recording started. Press 'Enter' to terminate.");

}

public void stop(String filename) throws IOException{
 System.in.read();
 videoCapture.stop();
}


public static void main(String[] args) throws Throwable {

    CaptureVideoFromWebCamera obj = new CaptureVideoFromWebCamera();
    obj.start("");
    obj.stop("");

    CaptureVideoFromWebCamera obj1 = new CaptureVideoFromWebCamera();       
    obj1.start("");
    obj1.stop("");

}

}

When I'm trying to do so I'm reveiving the following error (Insufficient system resources exist to complete the requested service web camera):

Exception in thread "main" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at com.teamdev.jxcapture.video.win.BaseDirectShowCapture.doStart(SourceFile:103) at com.teamdev.jxcapture.VideoCapture.start(SourceFile:146) at capturer.CaptureVideoFromWebCamera.start(CaptureVideoFromWebCamera.java:58) at capturer.CaptureVideoFromWebCamera.main(CaptureVideoFromWebCamera.java:76) Caused by: java.lang.reflect.InvocationTargetException at com.teamdev.jxdesktop.win32.g.doInvokeAndWait(Unknown Source) at com.teamdev.jxcapture.video.win.BaseDirectShowCapture.doStart(SourceFile:97) ... 3 more Caused by: com.teamdev.jxdesktop.win32.com.ComException: COM object method returns error code: 0x800705AA; Insufficient system resources exist to complete the requested service.

EDIT2: I tried to do add some thread sleep to the code in order to wait for the second capturing process.

CaptureVideoFromWebCamera obj = new CaptureVideoFromWebCamera();
    obj.start("1.wmv");
    obj.stop("");
    Thread.sleep(5000);
    CaptureVideoFromWebCamera obj1 = new CaptureVideoFromWebCamera();       
    obj1.start("2.wmv");
    obj1.stop("");

I got the same error.

EDIT3: When I am trying to use the same object for the capturing I got the following message:

Exception in thread "main" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at com.teamdev.jxcapture.video.win.BaseDirectShowCapture.doStart(SourceFile:103) at com.teamdev.jxcapture.VideoCapture.start(SourceFile:146) at CaptureVideoFromWebCamera.start(CaptureVideoFromWebCamera.java:47) //videoCapture.start(); at CaptureVideoFromWebCamera.main(CaptureVideoFromWebCamera.java:64) /obj.start("2.wmv"); Caused by: java.lang.reflect.InvocationTargetException at com.teamdev.jxdesktop.win32.g.doInvokeAndWait(Unknown Source) at com.teamdev.jxcapture.video.win.BaseDirectShowCapture.doStart(SourceFile:97) ... 3 more

alex
  • 8,904
  • 6
  • 49
  • 75
Jose Ramon
  • 5,572
  • 25
  • 76
  • 152
  • 1
    Try to add some sleep time before calling the camera again. The stop operation might not have completed when you're trying to start the camera the second time. Or better still, test with a GUI where you have to press a button to start and stop the camera. – Olantobi Jul 15 '16 at 23:58
  • I tried both of them still I got the same issue! – Jose Ramon Jul 16 '16 at 11:33
  • It seems that videoStop stops the capturing, however the recourses for the camera still remain in usage from the java so I cant initialize again the capturer. The program stop capturing however the camera is still open and I have to close the program in order my carmera to close. – Jose Ramon Jul 16 '16 at 12:55
  • When I debug the program without breakpoints in the second capturing process I am receiving runtimeException (VideoCapture).start() Source not found. – Jose Ramon Jul 16 '16 at 15:28
  • Oh yeah I'm totally gonna make the effort to get a license for this non-free library to see if I can figure this out!? Maybe not :) Apparently though you have a license? It looks like even the evaluation license includes email support :) *hint* – Arjan Jul 17 '16 at 20:57
  • You dont need to have licence to get the jar files. You can use the jar not the runnable. – Jose Ramon Jul 17 '16 at 21:14
  • It's supposed to detect my camera without the license? – Arjan Jul 17 '16 at 21:38
  • 1
    Yes it works fine. If you download the jar files from the site and run the posted code it should capture a video. – Jose Ramon Jul 17 '16 at 21:49
  • Alright thanks :) then I'll have something else to figure out first :) I'm on Linux, just installed a webcam, it works in vlc, but the sample code doesn't detect any cam. I'll see if I can fix that later :) – Arjan Jul 17 '16 at 21:54
  • Those are all InvocationTargetException. It seems this is an InvocationTargetException again. Look at the inner exception again until you find the root cause – Thomas Weller Jul 18 '16 at 09:47
  • How can I do so? How can I find the inner exceptions? – Jose Ramon Jul 18 '16 at 10:01
  • 1
    you can use [javacv](http://stackoverflow.com/questions/14070370/how-to-capture-and-record-video-from-webcam-using-javacv) to do this. it will work corectly.... – RayanFar Jul 21 '16 at 19:37
  • Yes this seems to be a bug in the jxcapture framework. Either report it as a bug on their site, or use a library with better support ;-) – Falco Jul 22 '16 at 09:28
  • Im thinking its the file you create in the start function -- it looks like the file is never released back to the system. – Richard Barker Jul 22 '16 at 19:40

3 Answers3

1

Actually, you are getting error message because your resource has been already locked by another thread and the lock is not released while you try to utilize the same resource from the another thread.

Here, you have to do two main things :

Step 1 : In your program, your have setup Thread.Sleep(5000); but it actually pause your thread instead and you have not setup any statement to release the resource. So, Try resetting camera socket and closing object in a finally statement.

Step 2 : try Synchronizedthread instead using normal one as only one process can able to use your resource at a time.

1

Can it help you? I think you need to release a resource after first capturing that next capture process could take it freely.

 private VideoSource webCamera; // make it as object field accessible both start and stop methods

 public void start(String file name) {
    ...
    webCamera = availableVideoSources.get(0);
    ...
   }

 public void stop(String filename) throws IOException{
   System.in.read();
   videoCapture.stop();
   webCamera.release();
 }
Yan Pak
  • 1,767
  • 2
  • 19
  • 15
1

Try to reshuffle your code a little bit, so you don't initialize the video system twice:

public VideoCapture videoCapture = VideoCapture.create(VideoFormat.WMV);

public void init() {

    List<VideoSource> availableVideoSources = VideoSource.getAvailable();
    System.out.println("availableVideoSources = " + availableVideoSources);

    if (availableVideoSources.isEmpty()) {
        throw new IllegalStateException("No external video sources available");
    }
    VideoSource webCamera = availableVideoSources.get(0);
    System.out.println("webCamera = " + webCamera);

    videoCapture.setVideoSource(webCamera);

    java.util.List<Codec> videoCodecs = videoCapture.getVideoCodecs();
    System.out.println("videoCodecs = " + videoCodecs);
    if (videoCodecs.isEmpty()) {
        throw new IllegalStateException("No video codecs available");
    }

    Codec videoCodec = videoCodecs.get(2);
    System.out.println("videoCodec = " + videoCodec);
}

public void start(String fileName) {
    EncodingParameters encodingParameters = new EncodingParameters(new File(fileName));
    encodingParameters.setBitrate(500000);
    encodingParameters.setFramerate(10);
    encodingParameters.setKeyFrameInterval(1);
    encodingParameters.setCodec(videoCodec);

    videoCapture.setEncodingParameters(encodingParameters);
    videoCapture.start();
    System.out.println("Recording started. Press 'Enter' to terminate.");

}

public void stop() throws IOException{
    System.in.read();
    videoCapture.stop();
}


public static void main(String[] args) throws Throwable {

    CaptureVideoFromWebCamera videoCapture = new CaptureVideoFromWebCamera();
    videoCapture.init();
    videoCapture.start("video1.wmv");
    videoCapture.stop();

    Thread.sleep(5000);

    videoCapture.start("viedo2.wmv");
    videoCapture.stop("");
}

I hope this helps, I don't have JxCapture's license (nor the web cam :)) to check this.

MirMasej
  • 1,592
  • 12
  • 17