title: Using the camera of the laptop with swing in java
body: <p>how to use camera using swing in java with the ubuntu OS please give me some examples and guidelines to use camera through the java.</p>
  
  <pre><code>public static void main(String[] args) {
      CamDemo t = new CamDemo();
      t.getCam();
  }
  
  public void getCam() {
      try {
  
          /* Grab the default web cam */
          MediaLocator ml = new MediaLocator("vfw://0");
          DataSource ds = Manager.createDataSource(ml);
          requestFormatResolution(ds);
          Player p = Manager.createRealizedPlayer(ds);
          p.start();
          Thread.currentThread().sleep(1000);
          JFrame jfrm = new JFrame("Testing Webcam");
          jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          if (p.getVisualComponent() != null)
              jfrm.getContentPane().add(p.getVisualComponent());
          if (p.getControlPanelComponent() != null)
              jfrm.getContentPane().add(p.getControlPanelComponent(),
                      BorderLayout.SOUTH);
          jfrm.pack();
          jfrm.setLocationRelativeTo(null);
          jfrm.setVisible(true);
          jfrm.setSize(320, 240);
  
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  
  public boolean requestFormatResolution(DataSource ds) {
  
      if (ds instanceof CaptureDevice) {
          FormatControl[] fcs = ((CaptureDevice) ds).getFormatControls();
          for (FormatControl fc : fcs) {
              Format[] formats = ((FormatControl) fc).getSupportedFormats();
              for (Format format : formats) {
                  if ((format instanceof VideoFormat)
                          &amp;&amp; (((VideoFormat) format).getSize().getHeight() &lt;= 240)
                          &amp;&amp; (((VideoFormat) format).getSize().getWidth() &lt;= 320)) {
                      ((FormatControl) fc).setFormat(format);
                      return true;
                  }
              }
          }
      }
      return false;
  }
  }
  </code></pre>
  
  <p>Stacktrace:</p>
  
  <pre><code>javax.media.NoDataSourceException: Cannot find a DataSource for: vfw://0
  at javax.media.Manager.createDataSource(Manager.java:1037)
  at com.convoy.gpack.pack.CamDemo.getCam(CamDemo.java:32)
  at com.convoy.gpack.pack.CamDemo.main(CamDemo.java:16)
  </code></pre>