如何在 Java 的 Swing 应用程序中集成网络摄像头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1382508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:21:49  来源:igfitidea点击:

How to integrate Webcam in Swing application of Java?

java

提问by om.

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

我正在用 Swing Java 创建一个 GUI 应用程序。我必须将网络摄像头与我的 GUI 集成。任何机构对此有想法吗?

回答by JRL

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

    new FrameGrabber().start();

  1. 下载并安装JMF
  2. 将 jmf.jar 添加到您的项目库中
  3. 下载FrameGrabber源文件并将其添加到您的项目中
  4. 如下使用它开始捕获视频。

    新的 FrameGrabber().start();

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.

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

Sample code:

示例代码:

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

回答by Denis Tulskiy

Freedom for Media in Javais an alternative implementation of JMF (API compatible). Just in case you'd like to use OpenSource library.

Java中的媒体自由是 JMF(API 兼容)的另一种实现。以防万一您想使用开源库。