Ask Your Question

Revision history [back]

Webcam in Java only properly freed after closing the program?

Hello,

Please consider the following basic code:

import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.highgui.VideoCapture;

public class V4L2 {

public static void main(String args[]) {

    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    Mat frame = new Mat();

    VideoCapture capture = new VideoCapture();

    int cont = 0;

    if(capture.open(0)) {
        while(cont < 100) {
            capture.read(frame);

            cont++;
        }
    }

    capture.release();

    while(true) {}

    /* The program will be stuck on the loop above and my
    webcam is never freed until I kill it. /*
    }
}

As you can see on my forced infinite loop and the comment, my webcam only closes when my Java program closes too, even after the capture.release() command. So while on the infinite loop if I don't kill the program its lights keep on and it's never available for other programs to use.

Is there any other way to properly close my webcam without closing the Java program? Maybe some code I've missed or even launching a System Application to do that.

I would basically want to turn my webcam on and off more than once in the same program. However it is presently impossible because I need to close and open the program again to turn it on.

Thanks

System Details: Arch: Intel i3 2.4GHz (x86_64) OS: Linux openSUSE 13.1 Kernel 3.11.10-7 OpenCV: 2.4.9 built with native V4L2 support JVM: OpenJDK 1.7.0_51

Webcam in Java only properly freed after closing the program?

Hello,

Please consider the following basic code:

import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.highgui.VideoCapture;

public class V4L2 {

public static void main(String args[]) {

    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    Mat frame = new Mat();

    VideoCapture capture = new VideoCapture();

    int cont = 0;

    if(capture.open(0)) {
        while(cont < 100) {
            capture.read(frame);

            cont++;
        }
    }

    capture.release();

    while(true) {}

    /* The program will be stuck on the loop above and my
    webcam is never freed until I kill it. /*
*/
    }
}

As you can see on my forced infinite loop and the comment, my webcam only closes when my Java program closes too, even after the capture.release() command. So while on the infinite loop if I don't kill the program its lights keep on and it's never available for other programs to use.

Is there any other way to properly close my webcam without closing the Java program? Maybe some code I've missed or even launching a System Application to do that.

I would basically want to turn my webcam on and off more than once in the same program. However it is presently impossible because I need to close and open the program again to turn it on.

Thanks

System Details: Arch: Intel i3 2.4GHz (x86_64) OS: Linux openSUSE 13.1 Kernel 3.11.10-7 OpenCV: 2.4.9 built with native V4L2 support JVM: OpenJDK 1.7.0_51

Webcam in Java only properly freed after closing the program?

Hello,

Please consider the following basic code:

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.VideoCapture;

org.opencv.highgui.VideoCapture;
public class V4L2 {
public static void main(String args[]) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat frame = new Mat();
VideoCapture capture = new VideoCapture();
int cont = 0;
if(capture.open(0)) {
while(cont < 100) {
capture.read(frame);
cont++;
}
}
capture.release();
while(true) {}
/* The program will be stuck on the loop above and my
webcam is never freed until I kill it. */
}
}

As you can see on my forced infinite loop and the comment, my webcam only closes when my Java program closes too, even after the capture.release() command. So while on the infinite loop if I don't kill the program its lights keep on and it's never available for other programs to use.

Is there any other way to properly close my webcam without closing the Java program? Maybe some code I've missed or even launching a System Application to do that.

I would basically want to turn my webcam on and off more than once in the same program. However it is presently impossible because I need to close and open the program again to turn it on.

Thanks

System Details: Arch: Intel i3 2.4GHz (x86_64) OS: Linux openSUSE 13.1 Kernel 3.11.10-7 OpenCV: 2.4.9 built with native V4L2 support JVM: OpenJDK 1.7.0_51