Java - Opencv VideoCapture.read - 线程java.lang.exception中的异常:未知异常

时间:2014-05-07 02:22:27

标签: java multithreading exception opencv video-processing

我有一个运行网络摄像头的java程序,并使用opencv库捕获图像。我的相机正在工作,最终停止拍摄。它抛出异常。

Exception in thread "Thread-11" java.lang.Exception: unknown exception
at org.opencv.highgui.VideoCapture.read_0(Native Method)
at org.opencv.highgui.VideoCapture.read(VideoCapture.java:341)
at projectana.VideoThread.run(VideoThread.java:59)
at java.lang.Thread.run(Thread.java:745)

这是我的代码:

(VideoThread.java)

import java.awt.image.BufferedImage;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.VideoCapture;
import org.opencv.objdetect.CascadeClassifier;

/**
 * @author DEV-OJT
 */

public class VideoThread implements Runnable {

    static private DoVideo vid;
    static private boolean running;

    public VideoThread(DoVideo vid) {
        this.vid = vid;
        running = true;
    }

    protected static void terminate() {
        running = false;
    }

    @Override
    public void run() {
        // CascadeClassifier faceDetector = new CascadeClassifier(this.getClass().
        // getResource("/resources/haarcascade_frontalface_alt.xml").getPath());
        Mat webcam_image = new Mat();

        BufferedImage temp;
        VideoCapture capture = new VideoCapture(0);
        if (capture.isOpened()) {
            System.err.println("Sleeping..");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                System.err.println("cant sleep");
            }
            System.err.println("camera opened");
            while (running) {
                System.out.println("entered");
                try {
                    Thread.sleep(200);
                } catch (InterruptedException ex) {
                    System.err.println("cant sleep");
                }
                capture.read(webcam_image);
                if (!webcam_image.empty()) {
                    // MatOfRect faceDetections = new MatOfRect();
                    // faceDetector.detectMultiScale(webcam_image, faceDetections);
                    // System.out.println("Number of Faces Detected: "+faceDetections.toArray().length);
                    // for(Rect rect : faceDetections.toArray()){
                    //    System.out.println(rect.toString());
                    //    Core.rectangle(webcam_image, new Point(rect.x, rect.y), 
                    //       new Point(rect.x + rect.width, rect.y + rect.height),
                    //    new Scalar(0, 255, 0));
                    // }
                    temp = Helper.matToBufferedImage(webcam_image);
                    vid.setimage(temp);
                    vid.repaint();
                } else {
                    System.out.println(" --(!) No captured frame -- Break!");
                    //break;  
                }
            }
        } else {
            System.err.println("No Camera.");
        }
    }

}

我的代码在每个formWindowClosed事件中停止SnapshotDialog.java中的线程:

if (snapshot != null) {
    VideoThread.terminate();
    try {
        snapshot.join();
    } catch (InterruptedException ex) {
        Logger.getLogger(SnapshotDialog.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.err.println("Successfully stop thread.");
} else {
    System.err.println("Unsuccessfully stop thread.");
}
snapshot = null;

为什么它首先运行并最终在5次或更少次尝试时打开对话框时停止占用帧?它是opencv库还是错误的编码和线程管理还是只是,我的相机很累,需要休息。谢谢大师。 Java初学者:)

0 个答案:

没有答案