Ask Your Question
0

Webcam can be opened but not read

asked 2017-11-08 15:14:30 -0500

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

berak gravatar image

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.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-11-09 21:12:50 -0500

supra56 gravatar image

updated 2017-11-09 21:13:40 -0500

There are two missing are imread and waitKey.

edit flag offensive delete link more

Comments

supra, out of luck here ;(

imread() and waitKey() are usually not available to java users

(though latest GSOC efforts resulted in a java highgui module, which mimicks the c++ functionality)

also, i think, that neither the drawing/gui code, nor using the java wrappers is the problem here, more a hardware / driver / install problem with the webcam used.

berak gravatar imageberak ( 2017-11-09 21:20:33 -0500 )edit

@supra56 can you be more specific?

lincolnb gravatar imagelincolnb ( 2017-11-09 21:27:04 -0500 )edit

@berak. He hasn't mentioned java.

supra56 gravatar imagesupra56 ( 2017-11-10 04:00:39 -0500 )edit

@supra56, there is a java tag, and the code is in java .... and post starts with:

I'm using the Java bindings

berak gravatar imageberak ( 2017-11-10 04:03:47 -0500 )edit

@lincolnb. My Apologized, I thought it was c++. The imread and waitKey isn't on java.

supra56 gravatar imagesupra56 ( 2017-11-10 04:06:17 -0500 )edit

new VideoCapture(); to new VideoCapture(0);

supra56 gravatar imagesupra56 ( 2017-11-10 05:58:29 -0500 )edit

This could be Mat frame = new Mat(); inside if/else condition block.

VideoCapture camera = new VideoCapture(0);
if(!camera.isOpened()){
    System.out.println("The camera could not be opened.");
}
else {
    Mat frame = new Mat();
    camera.read(frame); 
    while(true){
        if (camera.read(frame)){
supra56 gravatar imagesupra56 ( 2017-11-10 06:17:24 -0500 )edit

Dont forget about his one camera.release();

supra56 gravatar imagesupra56 ( 2017-11-10 06:18:40 -0500 )edit

@supra56 still doesn't work. Might be a driver issue -- any clue how to debug? camera.read() doesn't give descriptive errors.

lincolnb gravatar imagelincolnb ( 2017-11-10 10:44:36 -0500 )edit

@lincolnb. Did you test webcam to see it is working or not? What's OS you have? And brand of webcam?

supra56 gravatar imagesupra56 ( 2017-11-10 21:06:55 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-11-08 15:14:30 -0500

Seen: 482 times

Last updated: Nov 09 '17