3

我有一个表单,我想在其中捕获人的图像并在表单中显示该图像。

如何通过 java 连接到网络摄像头并在表单中显示该图像?

4

3 回答 3

4

您可以使用JavaCV来捕获图像。

这段代码应该让你开始(取自这里):

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

另一种选择是使用 Java Media Framework ( JMF )。你可以在这里找到一个例子。

于 2012-05-22T06:13:06.317 回答
2

You can use Webcam Capture project to do that. It's working on Windows XP, Vista, 7, Linux, Mac OS, Raspberry Pi and more. There is a ready-to-use Swing component extending JPanel which can be used to display image from your webcam. 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.

于 2013-02-07T06:23:35.753 回答
0
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

and add other library file that in the zip file

于 2013-07-26T07:25:03.497 回答