使用带有java的opencv可以同时打开多个摄像头吗?

时间:2014-02-25 15:36:06

标签: java opencv webcam-capture

我有通过蓝牙读取无线电信号的java代码。我想读取不同的信号,每个信号打开网络摄像头。我有四个USB摄像头,如果代码检测到信号1它应该打开摄像头1,如果代码检测信号2摄像头2应该打开等等。我使用opencv和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);
        }
    }

代码工作正常,但不能同时打开多个摄像头。有什么想法吗?

3 个答案:

答案 0 :(得分:1)

首先,您需要删除else语句。如果this.isDigitalOn(1)的计算结果为true,则不会执行第二个if语句。

其次,第一个while循环将无限期执行(从摄像机连续抓取帧)并阻止程序继续打开下一个摄像机。您需要在单独的线程中打开每个摄像头,允许它们同时运行。

如果你需要线程,有很多关于线程的教程,here就是一个例子。

答案 1 :(得分:0)

你无法在java中检查这个

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

如果this.isDigitalOn(1)返回“true”表示使用此

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

否则只需使用

    if (this.isDigitalOn(1))

这将起作用

答案 2 :(得分: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();
                    }