URGENT:Java VideoCapture is Open, But Mat.read(frame) is Always false

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

Aokiji gravatar image
                                            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 camera.read(frame) 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 camera.read(frame) before the " if(camera.read(frame))" because a post solution was stated the first camera.read(frame) 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();
            //camera.read(frame); incase the first camera.read = 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("camera.read(frame) = "+camera.read(frame));
                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 (camera.read(frame)){
                    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: "+camera.read(frame));

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 ] camera.read(frame) = false is camera still open? true Else cont... camera read: false frame = Mat [ 00CV_8UC1, isCont=false, isSubmat=false, nativeObj=0x26ae6d0, dataAddr=0x0 ] camera.read(frame) = 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: opencv-java-tutorials.readthedocs.io

*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



anyone's question is the same urgent here.

berak gravatar imageberak ( 2017-01-10 16:11:12 -0600 )edit

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 -0600 )edit