Ask Your Question

Aokiji's profile - activity

2017-01-19 18:46:23 -0600 received badge  Editor (source)
2017-01-19 18:45:49 -0600 commented question Java VideoCapture is Open, But Mat.read(frame) is Always false

Thanks Caracal!

2017-01-10 14:02:04 -0600 asked a question Java VideoCapture is Open, But Mat.read(frame) is Always false
                                            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{
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

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

            //Thread.sleep(11000);

        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));

        if(!camera.isOpened()){
            System.out.println("Error");
        }
        else {
            Mat frame = new Mat();
            //camera.read(frame); incase the first camera.read = false, as suggested from a post
            System.out.println("In progress...");
            while(true){
                //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("OK");
                    break;
                }
                else
                    System.out.println("Else cont... camera read: "+camera.read(frame));
            }   
        }
        camera.release();
    }
}   
                                -------------Output-------------

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

===================