1

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.

Code:

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!

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.

3
  • Does it fail? Your output shows that it prints Camera closed which is result of if (camera.isOpened()) evaluating false, suggesting that the camera is no longer open.
    – Choc13
    Nov 8, 2013 at 11:18
  • 1
    If you mean the fact the light is still on, this could be related to the fact that YouCam has opened. Perhaps when you create camera the computer also recognises the device and mounts it ready to import pictures or whatever it might do by default. So this could relate the settings on how you computer handles the physical camera, rather than you Java code which appears to be behaving correctly.
    – Choc13
    Nov 8, 2013 at 11:22
  • Ooh yes,maybe I should try it on another computer!
    – Mapleaf
    Nov 15, 2013 at 15:09

1 Answer 1

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();

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

1
  • I have tried but it did't work.It may well be in relation to my physical computer.But your code shows your object is very similar with me!So could I consult you some extra question that how to show a real-time video of camera with Swing and OpenCV?I can't find a way without IplImage and JavaCV.ConvasFrame.
    – Mapleaf
    Nov 15, 2013 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.