系统资源不足,从网络摄像头java捕获视频

时间:2016-07-15 20:02:46

标签: java

我尝试使用jxcapture捕获视频。我设法这样做只是一次但是当我试图在同一个节目中第二次拍摄视频时,我遇到了麻烦。我的代码如下:

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("");

}

}

当我尝试这样做时,我发现以下错误(系统资源不足以完成所请求的服务网络摄像头):

  

线程中的异常&#34; main&#34; java.lang.RuntimeException:java.lang.reflect.InvocationTargetException       在com.teamdev.jxcapture.video.win.BaseDirectShowCapture.doStart(SourceFile:103)       在com.teamdev.jxcapture.VideoCapture.start(SourceFile:146)       at capturer.CaptureVideoFromWebCamera.start(CaptureVideoFromWebCamera.java:58)       at capturer.CaptureVideoFromWebCamera.main(CaptureVideoFromWebCamera.java:76)   引起:java.lang.reflect.InvocationTargetException       at com.teamdev.jxdesktop.win32.g.doInvokeAndWait(Unknown Source)       在com.teamdev.jxcapture.video.win.BaseDirectShowCapture.doStart(SourceFile:97)       ......还有3个   引起:com.teamdev.jxdesktop.win32.com.ComException:COM对象方法返回错误代码:0x800705AA;系统资源不足,无法完成所请求的服务。

EDIT2:我尝试在代码中添加一些线程休眠,以便等待第二个捕获过程。

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

我得到了同样的错误。

EDIT3:当我尝试使用相同的对象进行捕获时,我收到以下消息:

  

线程中的异常&#34; main&#34; java.lang.RuntimeException:java.lang.reflect.InvocationTargetException       在com.teamdev.jxcapture.video.win.BaseDirectShowCapture.doStart(SourceFile:103)       在com.teamdev.jxcapture.VideoCapture.start(SourceFile:146)       在CaptureVideoFromWebCamera.start(CaptureVideoFromWebCamera.java:47)//videoCapture.start();       在CaptureVideoFromWebCamera.main(CaptureVideoFromWebCamera.java:64)/obj.start("2.wmv");   引起:java.lang.reflect.InvocationTargetException       at com.teamdev.jxdesktop.win32.g.doInvokeAndWait(Unknown Source)       在com.teamdev.jxcapture.video.win.BaseDirectShowCapture.doStart(SourceFile:97)       ......还有3个

3 个答案:

答案 0 :(得分:1)

实际上,您收到错误消息,因为您的资源已被另一个线程锁定,并且在您尝试从另一个线程使用相同资源时未释放锁定。

在这里,你必须做两件事:

第1步: 在您的程序中,您已经设置了scope.answers[scope.name] = element.val(); 但它实际上暂停了您的线程,而您没有设置任何语句来释放资源。因此,请尝试在Thread.Sleep(5000);语句中重置相机套接字并关闭对象。

第2步: 尝试使用普通线程尝试finally线程,因为一次只能有一个进程能够使用您的资源。

答案 1 :(得分:1)

它可以帮到你吗?我认为你需要在首次捕获下一个捕获过程可以自由使用之后释放资源。

 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();
 }

答案 2 :(得分:1)

尝试稍微重新调整您的代码,这样您就不会对视频系统进行两次初始化:

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("");
}

我希望这有帮助,我没有JxCapture的许可证(也不是网络摄像头:))来检查这个。