Question

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?

Was it helpful?

Solution

You could use JavaCV to capture the image.

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). You can find an example here.

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top