Open WebCam by using javaCV

  1. Today I will show you how open the webcam by using javaCV

  2. Open your last netbeans project

  3. Add a button and I name it as Open WebCam


  4. Double click on the button and goto button click event

  5. Enter following codes
    • To open the webcam
    •   
      CvCapture capture=opencv_highgui.cvCreateCameraCapture(0);
       
      Here 0 is a parsing a variable. o is for webcam. If you using another camera you can use 1 or 2 or something.
    • Next we have to set the height and with of camera frame
    •   opencv_highgui.cvSetCaptureProperty(capture,opencv_highgui.CV_CAP_PROP_FRAME_HEIGHT,480);
              opencv_highgui.cvSetCaptureProperty(capture,opencv_highgui.CV_CAP_PROP_FRAME_WIDTH,480);
       
    • Next we have to make IplImage
    •   IplImage img=opencv_highgui.cvQueryFrame(capture);
       
    • Next add a canves to show the images
    •   CanvasFrame frame= new CanvasFrame("WebCam Loading");
       
    • Now run the programm.
    • Can you see, when you try to close can't close the window. As a solution for it you can write above code in this part
    •  Thread x= new Thread(){
        public void run(){
        }
        
        //above codes here
       };
       x.start();
       

    3 comments: