title: Webcam stream in opencv using Java
body: <p>I want to stream my webcam in my java application for some image processing stuff. I am using OpenCV and Java Library files which came along with it. </p>
  
  <p>I got how to capture image, but how to capture and display frames continuously like a video.</p>
  
  <p>Please suggest modification in below code or an alternative way to do so.</p>
  
  <p>I m using Java Swing Application development .</p>
  
  <pre><code>package openc;
  import org.opencv.core.*;
  
  public class openc {
  
  private JFrame frame;
  
  /**
   * Launch the application.
   */
  public static void main(String[] args) {
      EventQueue.invokeLater(new Runnable() {
          public void run() {
              try {
                  openc window = new openc();
                  window.frame.setVisible(true);
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      });
  }
  
  /**
   * Create the application.
   */
  public openc() {
      initialize();
  }
  
  /**
   * Initialize the contents of the frame.
   */
  private void initialize() {
      frame = new JFrame();
      frame.setBounds(100, 100, 450, 300);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
      Canvas canvas = new Canvas();
      frame.getContentPane().add(canvas, BorderLayout.CENTER);
  
              System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
              VideoCapture camera = new VideoCapture(0);
              if(!camera.isOpened()){
                  System.out.println("Error");
              }
              else {
                  Mat iframe = new Mat();
                  while(true){
                      if (camera.read(iframe)){
                          System.out.println("Frame Obtained");
                          System.out.println("Captured Frame Width " + 
                          iframe.width() + " Height " + iframe.height());
                          while(true){
                          Highgui.imwrite(canvas, iframe);
                          }
  
  
                      }
                  }   
              }
              camera.release();
  
  }
  
  }
  </code></pre>