1

我是 StackOverflow 和 OpenCV 编程领域的新手。我用一些 Java 代码打开了我的相机,它可以工作,因为相机的灯亮着,但是当我试图关闭相机时,我失败了。

代码:

public class camera {

    public static void main(String[] args)  {
        System.loadLibrary("opencv_java244");
        VideoCapture camera = new VideoCapture(0);
        if (camera.isOpened())
             System.out.println("Camera is ready!");
        else {
             System.out.println("Camera Error!");
             return;
        }
        Mat newMat = new Mat();

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            //e.printStackTrace();
        }

        camera.read(newMat);
        Highgui.imwrite("testfile.jpg", newMat);

        camera.release();
        if (camera.isOpened()) {
            System.out.println("Camera is running!");
        }
        else {
            System.out.println("Camera closed!");
        }
    }
}

结果:

Camera is ready!
Camera closed!

我真的得到了照片,但灯仍然亮着!PS每次打开相机时,电脑都会打开一个叫YouCam的驱动软件,我必须手动关闭它才能释放相机。

4

1 回答 1

0

尝试 capture.retrieve() 而不是 capture.read()。这是一个对我有用的快照,甚至不使用 Thread.sleep() VideoCapture capture = new VideoCapture(0);

    if (!capture.isOpened()) {
        imagePanel.add(new JLabel("Oops! Your camera is not working!"));
        return;
    } 
    Mat frame = new Mat();
    capture.retrieve(frame);
    frame = FaceDetector.detect(frame);
    BufferedImage image = GestureUtil.matToBufferedImage(frame);*/
    imagePanel.setImage(image);
    imagePanel.repaint();
    String window_name = "Capture - Face detection.jpg";
    Highgui.imwrite(window_name, frame);

    capture.release();

我已经将它与 Swing 一起使用。但是,您可以忽略摇摆代码。希望这可以帮助

于 2013-11-14T14:02:23.170 回答