如何在Java的Swing应用程序中集成Webcam? [英] How to integrate Webcam in Swing application of Java?

查看:279
本文介绍了如何在Java的Swing应用程序中集成Webcam?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用swing Java创建一个GUI应用程序。我必须将web cam与我的GUI集成。有没有人知道这个?

I am creating one GUI application in swing Java.I have to integrate web cam with my GUI. Any body have idea about this ?

推荐答案


  1. 下载并安装 JMF

  2. 添加jmf。 jar到您的项目库

  3. 下载 FrameGrabber 源文件并将其添加到您的项目中

  4. 按如下方式使用它来开始捕获视频。

  1. Download and install JMF
  2. Add jmf.jar to your project libraries
  3. Download the FrameGrabber source file and add it to your project
  4. Use it as follows to start capturing video.

新的FrameGrabber()。start();

new FrameGrabber().start();

获取基础图像,您只需在FrameGrabber引用上调用getBufferedImage()即可。您可以在Timer任务中执行此操作,例如,每33毫秒。

To get to the underlying image, you simply call getBufferedImage() on your FrameGrabber reference. You can do this in a Timer task for example, every 33 milliseconds.

示例代码:

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

这篇关于如何在Java的Swing应用程序中集成Webcam?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆