Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version
bool button1pressed = false;
void action1(ActionEvent e)  {
        button1pressed = true;
}


main() {

        VideoCapture camera1 = new VideoCapture(0);
        if (!camera1.isOpened()) {
            System.out.println("Error! Camera1 can't be opened!");
            return;
        }

        VideoCapture camera2 = new VideoCapture(1); // next id
        if (!camera2.isOpened()) {
            System.out.println("Error! Camera2 can't be opened!");
            return;
        }


        Mat frame1 = new Mat();
        Mat frame2 = new Mat();

        while (true) {
            if (! camera1.read(frame1)) {
                System.out.println("cam1 disconnected");
                return;
           }
            if (! camera2.read(frame2)) {
                System.out.println("cam2 disconnected");
                return;
           }
           //
           // your processing code for frame1 and frame2 here
           //

           // check events (poll)
           if (button1pressed)  {
                 // do something
                  button1pressed = false;
           }

       }
}

some notes:

  • VideoCapture has an internal fifo, so if you don't read images continuously, you get stale, outdated frames.
  • thus, you should not start your Capture(s) from an event method, but let them run for the whole duration of your prog, if you need something done with events, rather send signals from there to your main loop.
  • when using usb webcams, your usb hub is the bottleneck here. if you experience lags, try to find a different hub, or reduce the resolution

bool button1pressed = false; void action1(ActionEvent e) { button1pressed = true; } }

main() {

        VideoCapture camera1 = new VideoCapture(0);
        if (!camera1.isOpened()) {
            System.out.println("Error! Camera1 can't be opened!");
            return;
        }

        VideoCapture camera2 = new VideoCapture(1); // next id
        if (!camera2.isOpened()) {
            System.out.println("Error! Camera2 can't be opened!");
            return;
        }


        Mat frame1 = new Mat();
        Mat frame2 = new Mat();

        while (true) {
            if (! camera1.read(frame1)) {
                System.out.println("cam1 disconnected");
                return;
           }
            if (! camera2.read(frame2)) {
                System.out.println("cam2 disconnected");
                return;
           }
           //
           // your processing code for frame1 and frame2 here
           //

           // check events (poll)
           if (button1pressed)  {
                 // do something
                  button1pressed = false;
           }

       }
}

some notes:

  • VideoCapture has an internal fifo, so if you don't read images continuously, you get stale, outdated frames.
  • thus, you should not start your Capture(s) from an event method, but let them run for the whole duration of your prog, if you need something done with events, rather send signals from there to your main loop.
  • when using usb webcams, your usb hub is the bottleneck here. if you experience lags, try to find a different hub, or reduce the resolution
  • camera ids are dynamic, 0 is the first, you plugged in, 1 the second, etc. it's the order, that matters here. if you disconnect them, and plug them in in a different order, your ids will change, too !


bool button1pressed = false;
 void action1(ActionEvent e)  {
         button1pressed = true;
    }

}


main() {

        VideoCapture camera1 = new VideoCapture(0);
        if (!camera1.isOpened()) {
            System.out.println("Error! Camera1 can't be opened!");
            return;
        }

        VideoCapture camera2 = new VideoCapture(1); // next id
        if (!camera2.isOpened()) {
            System.out.println("Error! Camera2 can't be opened!");
            return;
        }


        Mat frame1 = new Mat();
        Mat frame2 = new Mat();

        while (true) {
            if (! camera1.read(frame1)) {
                System.out.println("cam1 disconnected");
                return;
           }
            if (! camera2.read(frame2)) {
                System.out.println("cam2 disconnected");
                return;
           }
           //
           // your processing code for frame1 and frame2 here
           //

           // check events (poll)
           if (button1pressed)  {
                 // do something
                  button1pressed = false;
           }

       }
}

some notes:

  • VideoCapture has an internal fifo, so if you don't read images continuously, you get stale, outdated frames.
  • thus, you should not start your Capture(s) from an event method, but let them run for the whole duration of your prog, if you need something done with events, rather send signals from there to your main loop.
  • when using usb webcams, your usb hub is the bottleneck here. if you experience lags, try to find a different hub, or reduce the resolution
  • camera ids are dynamic, 0 is the first, you plugged in, 1 the second, etc. it's the order, that matters here. if you disconnect them, and plug them in in a different order, your ids will change, too !


bool button1pressed = false;
void action1(ActionEvent e)  {
        button1pressed = true;
}


main() {

        VideoCapture camera1 = new VideoCapture(0);
        if (!camera1.isOpened()) {
            System.out.println("Error! Camera1 can't be opened!");
            return;
        }

        VideoCapture camera2 = new VideoCapture(1); // next id
        if (!camera2.isOpened()) {
            System.out.println("Error! Camera2 can't be opened!");
            return;
        }


        Mat frame1 = new Mat();
        Mat frame2 = new Mat();

        while (true) {
            if (! camera1.read(frame1)) {
                System.out.println("cam1 disconnected");
                return;
           }
            if (! camera2.read(frame2)) {
                System.out.println("cam2 disconnected");
                return;
           }
           //
           // your processing code for frame1 and frame2 here
           //

           // check events (poll)
           if (button1pressed)  {
                 // do something
                  button1pressed = false;
           }

       }
}

some notes:

  • VideoCapture has an internal fifo, so if you don't read images continuously, continuously (flush the fifo), you get stale, outdated frames.
  • thus, you should not start your Capture(s) from an event method, but let them run for the whole duration of your prog, if you need something done with events, rather send signals from there to your main loop.
  • when using usb webcams, your usb hub is the bottleneck here. if you experience lags, try to find a different hub, or reduce the resolution
  • camera ids are dynamic, 0 is the first, you plugged in, 1 the second, etc. it's the order, that matters here. if you disconnect them, and plug them in in a different order, your ids will change, too !


bool button1pressed = false;
void action1(ActionEvent e)  {
        button1pressed = true;
}


main() {

        VideoCapture camera1 = new VideoCapture(0);
        if (!camera1.isOpened()) {
            System.out.println("Error! Camera1 can't be opened!");
            return;
        }

        VideoCapture camera2 = new VideoCapture(1); // next id
        if (!camera2.isOpened()) {
            System.out.println("Error! Camera2 can't be opened!");
            return;
        }


        Mat frame1 = new Mat();
        Mat frame2 = new Mat();

        while (true) {
            if (! camera1.read(frame1)) {
                System.out.println("cam1 disconnected");
                return;
           }
            if (! camera2.read(frame2)) {
                System.out.println("cam2 disconnected");
                return;
           }
           //
           // your processing code for frame1 and frame2 here
           //

           // check events (poll)
           if (button1pressed)  {
                 // do something
                  button1pressed = false;
           }

       }
}

some notes:

  • VideoCapture has an internal fifo, so if you don't read images continuously (flush the fifo), you get stale, outdated frames.
  • thus, you should not start your Capture(s) from an event method, method (stopping and restarting them is also very expensive), but let them run for the whole duration of your prog, if you need something done with events, rather send signals from there to your main loop.
  • when using usb webcams, your usb hub is the bottleneck here. if you experience lags, try to find a different hub, or reduce the resolution
  • camera ids are dynamic, 0 is the first, you plugged in, 1 the second, etc. it's the order, that matters here. if you disconnect them, and plug them in in a different order, your ids will change, too !


bool button1pressed = false;
void action1(ActionEvent e)  {
        button1pressed = true;
}


main() {

        VideoCapture camera1 = new VideoCapture(0);
        if (!camera1.isOpened()) {
            System.out.println("Error! Camera1 can't be opened!");
            return;
        }

        VideoCapture camera2 = new VideoCapture(1); // next id
        if (!camera2.isOpened()) {
            System.out.println("Error! Camera2 can't be opened!");
            return;
        }


        Mat frame1 = new Mat();
        Mat frame2 = new Mat();

        while (true) {
            if (! camera1.read(frame1)) {
                System.out.println("cam1 disconnected");
                return;
           }
            if (! camera2.read(frame2)) {
                System.out.println("cam2 disconnected");
                return;
           }
           //
           // your processing code for frame1 and frame2 here
           //

           // check events (poll)
           if (button1pressed)  {
                 // do something
                  button1pressed = false;
           }

       }
}

some notes:

  • VideoCapture has an internal fifo, fifo (with webcams, not movies), so if you don't read images continuously (flush the fifo), you get stale, outdated frames.
  • thus, you should not start your Capture(s) from an event method (stopping and restarting them is also very expensive), but let them run for the whole duration of your prog, if you need something done with events, rather send signals from there to your main loop.
  • when using usb webcams, your usb hub is the bottleneck here. if you experience lags, try to find a different hub, or reduce the resolution
  • camera ids are dynamic, 0 is the first, you plugged in, 1 the second, etc. it's the order, that matters here. if you disconnect them, and plug them in in a different order, your ids will change, too !


bool button1pressed = false;
void action1(ActionEvent e)  {
        button1pressed = true;
}


main() {

        VideoCapture camera1 = new VideoCapture(0);
        if (!camera1.isOpened()) {
            System.out.println("Error! Camera1 can't be opened!");
            return;
        }

        VideoCapture camera2 = new VideoCapture(1); // next id
        if (!camera2.isOpened()) {
            System.out.println("Error! Camera2 can't be opened!");
            return;
        }


        Mat frame1 = new Mat();
        Mat frame2 = new Mat();

        while (true) {
            if (! camera1.read(frame1)) {
                System.out.println("cam1 disconnected");
                return;
           }
            if (! camera2.read(frame2)) {
                System.out.println("cam2 disconnected");
                return;
           }

           // check events (poll)
           if (button1pressed)  {
                 // do something
                  button1pressed = false;
           }

           //
           // your processing code for frame1 and frame2 here
           //

           // check events (poll)
           if (button1pressed)  {
                 // do something
                  button1pressed = false;
           }

       }
}

some notes:

  • VideoCapture has an internal fifo (with webcams, not movies), so if you don't read images continuously (flush the fifo), you get stale, outdated frames.
  • thus, you should not start your Capture(s) from an event method (stopping and restarting them is also very expensive), but let them run for the whole duration of your prog, if you need something done with events, rather send signals from there to your main loop.
  • when using usb webcams, your usb hub is the bottleneck here. if you experience lags, try to find a different hub, or reduce the resolution
  • camera ids are dynamic, 0 is the first, you plugged in, 1 the second, etc. it's the order, that matters here. if you disconnect them, and plug them in in a different order, your ids will change, too !