如何用java和OpenCV关闭相机? [英] How can I close the camera with java and OpenCV?

查看:506
本文介绍了如何用java和OpenCV关闭相机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是StackOverflow和OpenCV编程世界的新人。
我使用一些Java代码打开了我的相机,它的工作原因是相机的灯已打开,但当我尝试关闭相机时,我失败了。

I'm new in the world of StackOverflow and in OpenCV programming. I've opened my camera with some Java code and it worked because the light of camera was on, but when I tried to close the camera, I failed.

代码:

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

result:

Camera is ready!
Camera closed!

我真的得到了图片,但光仍然在!
P.S.每当我尝试打开相机时,我的计算机将打开一个名为YouCam的驱动器软件,我必须手动关闭它才能释放相机。

I really got the picture, but the light was still on! P.S. Everytime when I try to open my camera, my computer will open a drive software named YouCam, and I must close it manually to release the camera.

推荐答案

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

Try capture.retrieve() instead of capture.read(). Here is a snapshot which works for me without using even 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一起使用。但是,您可以忽略摆动代码。希望这有助于

I have using this along with Swing. However, you can ignore swing code. Hope this helps

这篇关于如何用java和OpenCV关闭相机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆