简体   繁体   English

如何用Java连接到网络摄像头?

[英]How to connect to webcam in Java?

I have a form in which i want to capture the image of the person and display that image in the form. 我有一个表单,我想要捕获人的图像并在表单中显示该图像。

How can i connect to the webcam through java and display that image in the form? 如何通过java连接到网络摄像头并在表单中显示该图像?

You could use JavaCV to capture the image. 您可以使用JavaCV来捕获图像。

This code should get you started (taken from here ): 这段代码应该让你入门(从这里开始):

import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.FrameGrabber;
import com.googlecode.javacv.VideoInputFrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
public class GrabberShow implements Runnable {
    //final int INTERVAL=1000;///you may use interval
    IplImage image;
    CanvasFrame canvas = new CanvasFrame("Web Cam");
    public GrabberShow() {
        canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    }
    @Override
    public void run() {
        FrameGrabber grabber = new VideoInputFrameGrabber(0); 
        int i=0;
        try {
            grabber.start();
            IplImage img;
            while (true) {
                img = grabber.grab();
                if (img != null) {
                    cvFlip(img, img, 1);// l-r = 90_degrees_steps_anti_clockwise
                    cvSaveImage((i++)+"-capture.jpg", img);
                    // show image on window
                    canvas.showImage(img);
                }
                 //Thread.sleep(INTERVAL);
            }
        } catch (Exception e) {
        }
    }
}

Another alternative would be to use the Java Media Framework ( JMF ). 另一种选择是使用Java Media Framework( JMF )。 You can find an example here . 你可以在这里找到一个例子。

You can use Webcam Capture project to do that. 您可以使用Webcam Capture项目来执行此操作。 It's working on Windows XP, Vista, 7, Linux, Mac OS, Raspberry Pi and more. 它适用于Windows XP,Vista,7,Linux,Mac OS,Raspberry Pi等。 There is a ready-to-use Swing component extending JPanel which can be used to display image from your webcam. 有一个现成的Swing组件,可以扩展JPanel,可以用来显示网络摄像头的图像。 Please found this example for more details of how this can be done - it presents some advanced capabilities of this component, but basic usage would be the following: 请参阅此示例以获取有关如何执行此操作的更多详细信息 - 它提供了此组件的一些高级功能,但基本用法如下:

JFrame window = new JFrame("Test webcam panel");
window.add(new WebcamPanel(Webcam.getDefault()));
window.pack();
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

After you run this code you should see JFrame with image from your webcam inside. 运行此代码后,您应该看到JFrame中包含来自网络摄像头的图像。

Webcam.setAutoOpenMode(true);
BufferedImage image = Webcam.getDefault().getImage();
ImageIO.write(image, "PNG", new File("F:/test.png"));

can download the latest version from https://github.com/sarxos/webcam-capture 可以从https://github.com/sarxos/webcam-capture下载最新版本

and add other library file that in the zip file 并在zip文件中添加其他库文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM