运维开发网

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

运维开发网 https://www.qedev.com 2020-04-29 18:44 出处:网络 作者:运维开发网整理
我正在尝试使用 jxcapture捕获视频.我设法这样做只是一次但是当我试图在同一个程序中第二次捕获视频时我遇到了麻烦.我的代码如下: public VideoCapture videoCapture = VideoCapture.create(VideoFormat.WMV); public CaptureVideoFromWebCamera(){} public void start(Str
我正在尝试使用 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("");

}

}

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

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:我试图在代码中添加一些线程休眠,以便等待第二个捕获过程.

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

我得到了同样的错误.

编辑3:当我尝试使用相同的对象进行捕获时,我得到以下消息:

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

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

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

步骤1 :

在你的程序中,你已经设置了Thread.Sleep(5000);但它实际上会暂停你的线程而你没有设置任何语句来释放资源.因此,尝试在finally语句中重置相机套接字和关闭对象.

第2步 :尝试使用普通的同步线程,因为只有一个进程可以一次使用您的资源.

扫码领视频副本.gif

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号