Take the 2-minute tour ×

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?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

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 {
    IplImage image;
    CanvasFrame canvas = new CanvasFrame("Web Cam");
    public GrabberShow() {
    public void run() {
        FrameGrabber grabber = new VideoInputFrameGrabber(0); 
        int i=0;
        try {
            IplImage img;
            while (true) {
                img = grabber.grab();
                if (img != null) {
                    cvSaveImage((i++)+"-capture.jpg", img);
                    // show image on window
        } catch (Exception e) {

Another alternative would be to use the Java Media Framework (JMF). You can find an example here.

share|improve this answer

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

After you run this code you should see JFrame with image from your webcam inside.

share|improve this answer
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

share|improve this answer

protected by Gilbert Le Blanc Jul 26 '13 at 7:33


Would you like to answer one of these unanswered questions instead?
