簡體   English   中英

如何使用Java和OpenCV關閉相機?

[英]How can I close the camera with java and OpenCV?

我是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的驅動器軟件,並且必須手動關閉它才能釋放相機。

嘗試使用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一起使用。 但是,您可以忽略擺動代碼。 希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM