JavaCV Program to Access User Webcam Video inside Canvas GUI Desktop App

You are currently viewing JavaCV Program to Access User Webcam Video inside Canvas GUI Desktop App

JavaCV Program to Access User Webcam Video inside Canvas GUI Desktop App

import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.FrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.*;
import com.googlecode.javacv.OpenCVFrameGrabber;
import static com.googlecode.javacv.cpp.opencv_core.cvFlip;
 
public class demo_video {
 public static void main(String[] args) {
  //Create canvas frame for displaying webcam.
     CanvasFrame canvas = new CanvasFrame("Webcam"); 
   
  //Set Canvas frame to close on exit
     canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);   
      
     //Declare FrameGrabber to import output from webcam
     FrameGrabber grabber = new OpenCVFrameGrabber("");  
      
      
     try {      
       
      //Start grabber to capture video
      grabber.start();      
       
      //Declare img as IplImage
      IplImage img;
       
      while (true) {
        
       //inser grabed video fram to IplImage img
       img = grabber.grab();
        
       //Set canvas size as per dimentions of video frame.
       canvas.setCanvasSize(grabber.getImageWidth(), grabber.getImageHeight()); 
        
       if (img != null) {      
         //Flip image horizontally
         cvFlip(img, img, 1);
        //Show video frame in canvas
        canvas.showImage(img);               
        }
       }
      }
     catch (Exception e) {      
     }
    }
}

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply