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

    Introduction to openCV (Java)

    1. Today I will show you how using openCv with java

    2. OpenCV is a very use full tool for working with images

    3. Normally openCV is using with c++. But we can use openCV with java

    4. First you have to download and install the openCV

    5. You can download it from following link

    OpenCV

    When you installing the openCV, it will asks folder to extract. Then choose C drive and extract them.

    6. Then you have download few jar files. Download them and extract them

    7. Go to following link and download them(only the following files)

    JavaCV


    8. After you extract it you can see "javac-bin" folder and "javacv-cppjars" folder

    OK. Now open neatbeans and make new project

    9. Now you have add few libraries

    10. Right click on libraries and choose "Add Jar/Folder"


    11. Now open the javac-bin folde. Select javacpp.jar,javacv.jar jar files and if you using windows choos javacv-windows-x86.jar and javacv-windows-x86_64.jar files.if you usin another operating system please choose jar file by compareing with your operating system.
    12. Select them and click open.
    13. Same way go to javacv-cppjars folde and choose jar files with your operating system name.

    14. Now simply add a button to jframe. Double click on it and got click event of button

    15. Type the following codes
    
    
    IplImage image=cvLoadImage("C:\\Users\\Udayanga\\Desktop\\Internet\\img.png");
            final CanvasFrame canvas= new CanvasFrame("Demo");
            canvas.showImage(image);
            canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    

    16. If you see some erro right clic on code area and choose Fix Imports

    17. Now run your programm.


    By using openCV we can make very interesting applications. Follow is some of them.