Ask Your Question
0

Read h264 frame from IP Camera feed: Java version

asked 2013-11-20 19:09:37 -0600

Will Stewart gravatar image

updated 2013-11-21 02:45:04 -0600

berak gravatar image

I have been able to connect to and open an rstp h264 IP camera stream, and grab the first frame. However, when I try to retrieve() or read() the image, I'm given a null pointer exception.

public class MotionDetect {
    public static void main(String[] args) {
        Mat image=null;
        int i=0;

        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        VideoCapture capture = new VideoCapture("rtsp://169.254.71.156/axis-media/media.amp");

        double propid=0;
        if (capture.isOpened()) {
            System.out.println("Video is captured");
            int x=0;
            while (x<18) {
                x=x+1;
                propid=capture.get(x);
                System.out.println("Property Id is " + x + " value ="+ propid);
            }
            // grab 10 frames as a test
            while (i<10) {
            if (capture.grab()) {
                System.out.println("Frame was grabbed, i=" + i);}
            try {
                //capture.retrieve(image); //same results as .read()
                capture.read(image);    
            } catch (Exception e) {
                System.out.println("Null Pointer Exception during Read");
            }   
            i=i+1;
          }
        }
        capture.release();
        System.out.println("VideoCapture is released"); 
    }
}

The output is;

Video is captured
Property Id is 1 value =0.0
Property Id is 2 value =1.1111111111111112E-5
Property Id is 3 value =704.0
Property Id is 4 value =480.0
Property Id is 5 value =29.97002997002997
Property Id is 6 value =0.0
Property Id is 7 value =-3.0713859596586E15
Property Id is 8 value =0.0
Property Id is 9 value =0.0
Property Id is 10 value =0.0
Property Id is 11 value =0.0
Property Id is 12 value =0.0
Property Id is 13 value =0.0
Property Id is 14 value =0.0
Property Id is 15 value =0.0
Property Id is 16 value =0.0
Property Id is 17 value =0.0
Property Id is 18 value =0.0
Frame was grabbed, i=0
Null Pointer Exception during Read
Frame was grabbed, i=1
Null Pointer Exception during Read
Frame was grabbed, i=2
Null Pointer Exception during Read
Frame was grabbed, i=3
Null Pointer Exception during Read
Frame was grabbed, i=4
Null Pointer Exception during Read
Frame was grabbed, i=5
Null Pointer Exception during Read
Frame was grabbed, i=6
Null Pointer Exception during Read
Frame was grabbed, i=7
Null Pointer Exception during Read
Frame was grabbed, i=8
Null Pointer Exception during Read
Frame was grabbed, i=9
Null Pointer Exception during Read
VideoCapture is released

The "capture" value during the run is VideoCapture(id=18)

I've been reviewing the ip camera threads, and by the point capture is successful, the reading seems to be a non-issue.

I ran the properties to identify any odd values, and I note that while I've set my camera to 680x480, it is registering 704x480.

The same code (adjusted for an mjpeg feed) will connect and open when the camera is producing MJPEG, but will not register a successful .grab()

What am I doing wrong on my read (or retrieve)?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2013-11-21 11:10:35 -0600

JamesBarnett gravatar image

I think the issue is you need to initialise the Mat instance you pass to capture.read(). Its currently null in your code.

Mat image = new Mat();

Also, read() also performs both a grab() and retrieve() internally, so you don't need to call grab() beforehand.

while (i<10) {
        try {
            capture.read(image);    
        } catch (Exception e) {
            System.out.println("Null Pointer Exception during Read");
        }   
        i=i+1;
      }

I had a similar issue earlier, hope this helps!

edit flag offensive delete link more

Comments

@JamesBarnett , this did the trick! Thanks much, my good man.

Will Stewart gravatar imageWill Stewart ( 2013-11-23 07:18:27 -0600 )edit

Question Tools

Stats

Asked: 2013-11-20 19:09:37 -0600

Seen: 6,403 times

Last updated: Nov 21 '13