Java VideoCapture is Open, But is Always false

asked 2017-01-10 13:37:58 -0500

Aokiji gravatar image

updated 2017-01-19 18:46:23 -0500

                                            Hey All:

The java code below is supposed to take a snapshot, by reading the from the frame of the connected camera. However, though camera.isOpened is True, the is always false. I've followed several walkthrus from stack overflow and posts from here including: waiting for 11s for the VideoCapture camera to open, and inserting before the " if(" because a post solution was stated the first is always false. Because of the code implementation, it loops the if-else block until terminated from the console so the ---Output--- below is the first few loops.

Despite that, mine is always false. I added an else block in within the while(true) body, to check if the camera is null, but its not (see Output below). The camera is opencv compatible as well. But nothing so far. Any suggestions help, its a rare case please help. More details are provided at the bottom. Thank you!

                                ---------Java Source Code--------
import org.opencv.core.*;
import org.opencv.highgui.Highgui;        
import org.opencv.highgui.VideoCapture;        

public class VideoCap {
    public static void main (String args[]) throws InterruptedException{

        VideoCapture camera = new VideoCapture(0); //Tried -1, opened options. But same output


        System.out.println("VidCap isOpened =" + camera.isOpened());
        System.out.println("Frame Width: " + camera.get(Highgui.CV_CAP_PROP_FRAME_WIDTH));
        System.out.println("Frame Height: " + camera.get(Highgui.CV_CAP_PROP_FRAME_HEIGHT));

        else {
            Mat frame = new Mat();
            //; incase the first = false, as suggested from a post
            System.out.println("In progress...");
                //Print properties of the camera to check for null values.
                System.out.println("frame = "+ frame);
                System.out.println(" = ";
                System.out.println("is camera still open? " + camera.isOpened());

                //Always returns false, and prints the else block unlike the youtube video
                   // (see --Sources and Equipment--)
                if ({
                    System.out.println("Frame Obtained");
                    System.out.println("Captured Frame Width " + 
                    frame.width() + " Height " + frame.height());
                    Highgui.imwrite("camera.jpg", frame);
                    System.out.println("Else cont... camera read: ";

VidCap isOpened =true Frame Width: 640.0 Frame Height: 480.0 In progress... frame = Mat [ 00CV_8UC1, isCont=false, isSubmat=false, nativeObj=0x26ae6d0, dataAddr=0x0 ] = false is camera still open? true Else cont... camera read: false frame = Mat [ 00CV_8UC1, isCont=false, isSubmat=false, nativeObj=0x26ae6d0, dataAddr=0x0 ] = false is camera still open? true Else cont... camera read: false

------Sources & Equipment------ *Youtube video of code: How to install OpenCV and use it with Java and configure it with Eclipse ? Taha Emara

*OpenCV Intallaion:

*Camera from Amazon: ELP Mini Aluminum Black Case Usb Security Camera With 170degree Wide Angle Fisheye Lens And 3Meters USB Cable For Home Security And Machine Vision System

*Computer: Windows 10 64bit, 4G RAM


edit retag flag offensive close merge delete


Does it work on video files? Like if you do: VideoCapture camera = new VideoCapture("vid.avi"); Why dont you try OpenCV 3.2 version? I have no problem with video camera, tested on 3 devices.

Caracal gravatar imageCaracal ( 2017-01-13 10:12:52 -0500 )edit

Thanks Caracal!

Aokiji gravatar imageAokiji ( 2017-01-19 18:44:52 -0500 )edit