0

I have java code that reads radio signals via Bluetooth. I want to read different signals and each signal open webcam. I have four USB cameras if the code detect signal 1 it should open camera 1 if the code detect signal2 camera2 should opened and so on. I add the follwing code using opencv with eclipse:

if (this.isDigitalOn(1) == true)
{                       
    CvCapture capture = opencv_highgui.cvCreateCameraCapture(0);

    opencv_highgui.cvSetCaptureProperty(capture, opencv_highgui.CV_CAP_PROP_FRAME_HEIGHT, 500);

    opencv_highgui.cvSetCaptureProperty(capture, opencv_highgui.CV_CAP_PROP_FRAME_WIDTH, 1000);
    IplImage grabbedimage = opencv_highgui.cvQueryFrame(capture);

    CanvasFrame frame = new CanvasFrame ("Webcam0");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    while(frame.isVisible() && (grabbedimage = opencv_highgui.cvQueryFrame(capture)) != null)                                                               
    {
        frame.showImage(grabbedimage);
    }
}
else                    
    if (this.isDigitalOn(2) == true)
    {
        CvCapture capture1 = opencv_highgui.cvCreateCameraCapture(1);
        opencv_highgui.cvSetCaptureProperty(capture1, opencv_highgui.CV_CAP_PROP_FRAME_HEIGHT, 500);
        opencv_highgui.cvSetCaptureProperty(capture1, opencv_highgui.CV_CAP_PROP_FRAME_WIDTH, 1000);

        IplImage grabbedimage1 = opencv_highgui.cvQueryFrame(capture1);
        CanvasFrame frame1 = new CanvasFrame ("Webcam1");
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        while (frame1.isVisible() && (grabbedimage1 = opencv_highgui.cvQueryFrame(capture1)) != null)
        {
            frame1.showImage(grabbedimage1);
        }
    }

The code is working fine but can\t open more than one camera at the same time. Any ideas please?

PaperBirdMaster
  • 12,806
  • 9
  • 48
  • 94

3 Answers3

1

First you need to remove the else statement. If this.isDigitalOn(1) evaluates to true it wont execute the second if statement.

Secondly, the first while loop will execute indefinitely (continuously grabbing frames from the camera) and will block your program from continuing to open the next camera. You need to open each camera in a separate thread, allowing them to run concurrently.

There are loads of tutorials on threads if you need them, here is an example.

James Barnett
  • 5,039
  • 3
  • 15
  • 18
  • Thank you so much.I changed the code using tread but still didn't got it work as i need. when I press 1 first camera works fine and when I press two nothing is showing up. Also, if I close the window of first camera the program is interrupted. Here is the new code – user3351912 Feb 26 '14 at 19:30
0

You could not check like this in java

    if (this.isDigitalOn(1) == true)

if this.isDigitalOn(1) return "true" means use this

    if (this.isDigitalOn(1).equals("true"))

Otherwise simply use

    if (this.isDigitalOn(1))

this will work

saravanakumar
  • 1,747
  • 4
  • 20
  • 38
  • Thank you so much. I changed the code using tread but still didn't got it work as i need. when I press 1 first camera works fine and when I press two nothing is showing up. Also, if I close the window of first camera the program is interrupted. Here is the new code – user3351912 Feb 26 '14 at 15:55
  • Sorry I post the code as an answer. it's showed above. the second if statement declared by the same way. – user3351912 Feb 26 '14 at 16:04
0
if (this.isDigitalOn(3))
                {
                    System.out.println("we are there 3 ");

                    Thread t = new Thread ()
                    {
                        public void run(){              
                      CvCapture capture0 = opencv_highgui.cvCreateCameraCapture(0);
                      opencv_highgui.cvSetCaptureProperty(capture0, opencv_highgui.CV_CAP_PROP_FRAME_HEIGHT, 500);
                      opencv_highgui.cvSetCaptureProperty(capture0, opencv_highgui.CV_CAP_PROP_FRAME_WIDTH, 1000);
                      IplImage grabbedimage0 = opencv_highgui.cvQueryFrame(capture0);
                      CanvasFrame frame = new CanvasFrame ("Webcam0");
                      frame.setDefaultCloseOperation(CanvasFrame.EXIT_ON_CLOSE);
                      while(frame.isVisible() && (grabbedimage0 = opencv_highgui.cvQueryFrame(capture0)) != null)
                    {
                          frame.showImage(grabbedimage0);
                    }
                    opencv_highgui.cvReleaseCapture(capture0);
                    grabbedimage0.release();
                    }
                    };
                    t.start();
                    }