WebCam永远不会开启

时间:2015-04-12 10:05:44

标签: java opencv video-capture

我正在尝试打开外部摄像头以使用opencv捕获图像。我写下面的代码,我也检查了一些与此问题相关的问题但是,当我运行代码时,外部网络摄像头不会打开绿色LED - 指示网络摄像头的LED指示灯亮起 - 和#34;打开"打印在屏幕上。单词"打开",如下面代码中所示,表示凸轮已开启。

请让我知道为什么我收到“#34;已打开"而网络摄像头的LED不亮。

代码

public class MainClass {

static {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}

private final static int WEBCAM_SELECT = -1;
private final static int WEBCAM_BUILTIN = 0;
private final static int WEBCAM_EXTERNAL = 2;

static JFrame mediaFrame = new JFrame("Media");

public static void main(String[] args) throws InterruptedException {

    Thread camThread = new Thread(new ThreadCam(), "CamThread");
    camThread.setDaemon(true);

    VideoCapture vidCap = new VideoCapture(WEBCAM_EXTERNAL);
    vidCap.open(WEBCAM_EXTERNAL);

    Thread.sleep(10000);// wait 10 sec to initilize the device;

    if (vidCap.isOpened()) {
        System.out.println("opened");//after 10 seconds this word will be printed
        camThread.start();
    }
}

更新

请取悦Thread.sleep(10000);行和旁边的评论。

static {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}

private final static int WEBCAM_SELECT = -1;
private final static int WEBCAM_BUILTIN = 0;
private final static int WEBCAM_EXTERNAL = 1;

static JFrame mediaFrame = new JFrame("Media");

public static void main(String[] args) throws InterruptedException {

    Thread camThread = new Thread(new ThreadCam(), "CamThread");
    camThread.setDaemon(true);

    VideoCapture vidCap = new VideoCapture();
    vidCap.open(WEBCAM_EXTERNAL);

    Thread.sleep(10000);// wait 10 sec to initilize the device; upto this line the Cam is ON, but after the 10 secs, it is OFF again and the word "Opened" is printed

    if (vidCap.isOpened()) {
        System.out.println("opened");//after 10 seconds this word will be printed
        camThread.start();
    }
}

2 个答案:

答案 0 :(得分:2)

我之前遇到过这个问题,我意识到的是,以下两行:

VideoCapture vidCap =   new VideoCapture();
vidCap.open(WEBCAM_EXTERNAL);

用于实例化VideoCapture类的对象并打开特定设备。

并且因为.isOpened返回true,这意味着,您选择的设备已成功打开。在调用.isOpened()之后,作为ypur设备的LED在.isOpened()和OFF之前是ON,这并不意味着您选择打开的设备不是ON或者无法打开,但实际上它是打开的但是您没有执行从您选择打开的设备派生的任何操作。

例如,在.isOpened之后尝试调用vidCap.grap()或进行视频流,然后eLED应该再次打开。

答案 1 :(得分:0)

尝试使用WEBCAM_EXTERNAL = 1;而不是WEBCAM_EXTERNAL = 2;

我想知道你的硬件是什么,PC / MAC?