Ask Your Question

Revision history [back]

Webcam can be opened but not read

I'm using the Java bindings for OpenCV 3.3.1, although I had this same issue with 2.4.13.

What I want to do is open the webcam, and display each frame as it is captured. Here is my code:

VideoCapture camera = new VideoCapture();
camera.open(0);
Mat frame = new Mat();

if (!camera.isOpened()) {
    System.out.println("The camera could not be opened.");
    return;
} else {
    while (true) {
        if (camera.read(frame)) {
            image = MatToBufferedImage(frame);
            repaint();
        } else {
            System.out.println("Unable to read from camera");
        }
    }
}

The strange thing is, the camera is opened! The green light never turns on indicating that it's being used, but the VideoCapture object returns true for isOpened().

However, in that while loop, all it does is repeatedly print out the message "Unable to read from camera", indicating that camera.read(frame) returns false.

What am I doing wrong? I've been searching the internet but can't find anything.

click to hide/show revision 2
retagged

updated 2017-11-08 15:26:28 -0600

berak gravatar image

Webcam can be opened but not read

I'm using the Java bindings for OpenCV 3.3.1, although I had this same issue with 2.4.13.

What I want to do is open the webcam, and display each frame as it is captured. Here is my code:

VideoCapture camera = new VideoCapture();
camera.open(0);
Mat frame = new Mat();

if (!camera.isOpened()) {
    System.out.println("The camera could not be opened.");
    return;
} else {
    while (true) {
        if (camera.read(frame)) {
            image = MatToBufferedImage(frame);
            repaint();
        } else {
            System.out.println("Unable to read from camera");
        }
    }
}

The strange thing is, the camera is opened! The green light never turns on indicating that it's being used, but the VideoCapture object returns true for isOpened().

However, in that while loop, all it does is repeatedly print out the message "Unable to read from camera", indicating that camera.read(frame) returns false.

What am I doing wrong? I've been searching the internet but can't find anything.