我正在使用swing Java创建一个GUI应用程序。我必须将web cam与我的GUI集成。有没有人知道这个?
答案 0 :(得分:8)
按如下方式使用它来开始捕捉视频。
new FrameGrabber()。start();
要获取基础图像,只需在FrameGrabber参考上调用getBufferedImage()即可。您可以在Timer任务中执行此操作,例如,每33毫秒。
示例代码:
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);
}
}
答案 1 :(得分:2)
Freedom for Media in Java是JMF(API兼容)的替代实现。以防万一你想使用OpenSource库。