2009-09-05 12 views
6

Sto creando un'applicazione GUI in swing Java. Devo integrare la web cam con la mia GUI. Qualche corpo ha idea di questo?Come integrare Webcam nell'applicazione Swing di Java?

+1

c'è Java Media Framework. Con JMF puoi riprodurre film, vedere webcam, ... Forse è una soluzione per te –

risposta

2

Freedom for Media in Java è un'implementazione alternativa di JMF (compatibile API). Nel caso in cui ti piacerebbe utilizzare la libreria OpenSource.

7
  1. Scaricare e installare JMF
  2. Add jmf.jar alle librerie di progetto
  3. Scaricare il file FrameGrabber sorgente e aggiungerlo al progetto
  4. usarlo come segue per iniziare a registrare il video.

    new FrameGrabber(). Start();

Per ottenere l'immagine sottostante, è sufficiente chiamare getBufferedImage() sul riferimento FrameGrabber. È possibile farlo in un'attività del timer, ad esempio, ogni 33 millisecondi.

codice di esempio:

public class TestWebcam extends JFrame { 
    private FrameGrabber vision; 
    private BufferedImage image; 
    private VideoPanel videoPanel = new VideoPanel(); 
    private JButton jbtCapture = new JButton("Show Video"); 
    private Timer timer = new Timer(); 

    public TestWebcam() { 
    JPanel jpButton = new JPanel(); 
    jpButton.setLayout(new FlowLayout()); 
    jpButton.add(jbtCapture); 

    setLayout(new BorderLayout()); 
    add(videoPanel, BorderLayout.CENTER); 
    add(jpButton, BorderLayout.SOUTH); 
    setVisible(true); 

    jbtCapture.addActionListener(
     new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       timer.schedule(new ImageTimerTask(), 1000, 33); 
      } 
     } 
    ); 
    } 

    class ImageTimerTask extends TimerTask { 
    public void run() { 
     videoPanel.showImage(); 
    } 
    } 

    class VideoPanel extends JPanel { 
     public VideoPanel() { 
     try { 
      vision = new FrameGrabber(); 
      vision.start(); 
     } catch (FrameGrabberException fge) { 
     } 
     } 

     protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     if (image != null) 
      g.drawImage(image, 10, 10, 160, 120, null); 
     } 

     public void showImage() { 
      image = vision.getBufferedImage(); 
      repaint(); 
     } 
    } 

    public static void main(String[] args) { 
     TestWebcam frame = new TestWebcam(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(190, 210); 
     frame.setVisible(true); 
    } 
} 
+0

Grazie a JRL, sto cercando di implementarlo, voglio sapere se rileverà automaticamente la mia webcam? –