我正在尝试使用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("");
}
}
当我尝试这样做时,我收到以下错误(系统资源不足,无法完成请求的服务网络摄像头):
线程"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) 在捕获者。CaptureVideoFromWebCamera.start(CaptureVideoFromWebCamera.java:58) 在捕获者。CaptureVideoFromWebCamera.main(CaptureVideoFromWebCamera.java:76) 由: java.lang.reflect.InvocationTargetException at com.teamdev.jxdesktop.win32.g.doInvokeAndWait(未知来源) at 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("");
我得到了同样的错误。
编辑3:当我尝试使用相同的对象进行捕获时,我收到以下消息:
线程"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"); 由: java.lang.reflect.InvocationTargetException at com.teamdev.jxdesktop.win32.g.doInvokeAndWait(未知来源) at com.teamdev.jxcapture.video.win.BaseDirectShowCapture.doStart(SourceFile:97) ...3 更多
实际上,您会收到错误消息,因为您的资源已被另一个线程锁定,并且在您尝试使用另一个线程的相同资源时未释放锁。
在这里,您必须做两件主要事情:
步骤 1 :在您的程序中,您已经设置了Thread.Sleep(5000);
但它实际上暂停了您的线程,并且您没有设置任何语句来释放资源。因此,请尝试在finally
语句中重置相机套接字并关闭对象。
步骤 2 :尝试Synchronized
线程改用普通线程,因为一次只有一个进程可以使用您的资源。
它能帮到你吗?我认为您需要在第一次捕获后释放资源,下一个捕获过程可以自由地使用它。
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();
}
尝试稍微重新洗牌一下代码,这样就不会初始化视频系统两次:
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 的许可证(也没有网络摄像头:))来检查这一点。