VideoCapture无效

时间:2015-04-15 09:10:49

标签: java opencv video-capture javacv

当我第一次运行此代码时,我从VideoCapture类实例化的对象通常能够从相机捕获帧并将其显示在JFrame中。后来出乎意料的是,当我再次运行程序时,我发现JFrame没有显示任何内容,尽管我没有更改代码。

可能是什么原因?

    vidCap = new VideoCapture();
        vidCap.open(WEBCAM_EXTERNAL);
        Thread.sleep(2000);// wait to initialize the device;

        if (vidCap.isOpened()) {
            camThread.start();
        } else {
            System.out.println("failed to open the Cam");
        }
    } else {
        Log.E(TAG, "main", "objDescExt is null or empty or has no rows");
    }

    private static class ThreadCam implements Runnable {

    public void run() {
        // TODO Auto-generated method stub
        camStream();
    }
}

public static void camStream() {
    // TODO Auto-generated method stub
    while(vidCap.grab()) {

        grappedMat = new Mat();
        vidCap.retrieve(grappedMat);

        if (!grappedMat.empty()) {

            try {
                Thread.sleep(1);//wait a bit to decrease the overhead of the work load as it is an intensive operation
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            //MatFactory.writeMat(FilePathUtils.newOutputPath("CapturedImg"), grappedMat);//needed when capture a query image
            Mat sceneDescExt = sceneDescriptor(grappedMat);// FeatureDetection+DescriptorExtractor, returns the descriptorExtractor of the scene/train
            if ( (!sceneDescExt.empty()) && (sceneDescExt.rows() != 0) ) {

                Mat outImg = descMatcher(objDescExt, sceneDescExt);//descriptor Matching operation

                if ( (outImg != null) && (!outImg.empty()) ) {

                    BufferedImage buffImg = mat2BufferedImage(outImg);

                    if (buffImg != null) {
                        facePanel.setFace(buffImg);
                        facePanel.repaint();
                    } else {
                        Log.W(TAG, "camStream", "buffImg is null.");
                    }
                } else {
                    Log.W(TAG, "camStream", "outImg is null or empty.");
                }

            } else {
                Log.W(TAG, "camStream", "sceneDescExt is empty or has no rows");
            }
        }
    }
}

0 个答案:

没有答案