Java - Opencv VideoCapture.read - Exception in thread java.lang.exception : Unknown Exception

不羁岁月 提交于 2019-12-10 19:35:04

问题


I have a java program that runs a webcam and capture images with the use of opencv libraries. My camera is working and eventually stops taking frames. It throws an exception.

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)

Here is my code:

(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.");
        }
    }

}

My code in stopping the the thread in SnapshotDialog.java every formWindowClosed event:

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;

Why does it operates at first and eventually stops taking frames when I open the dialog box in 5 or less tries? Is it the opencv library or wrong coding and management of threads or just, my camera is tired and needs rest. Thank you masters. Java beginner here :)

来源:https://stackoverflow.com/questions/23507638/java-opencv-videocapture-read-exception-in-thread-java-lang-exception-unkn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!