0

I want to capture and display video from webacam. For capturing I am using this JavaCV code which gives IplImage that can be converted to BufferedImage. How to display this image on a JFrame ?

Note: I want to display video not single a image i.e. image should be updated continuously as it is captured from the webcam

Aamir Rizwan
  • 827
  • 5
  • 13
  • 34
  • Possible duplicate of [Java Swing: how to add an image to a JPanel?](http://stackoverflow.com/questions/299495/java-swing-how-to-add-an-image-to-a-jpanel) Also since you are using `Swing` you should use a `JLabel` for it there is really no reason (but you might want to) implement drawing yourself. See [the answer for details](http://stackoverflow.com/a/2706730/613495), yet [another example is here](http://stackoverflow.com/a/9403763/613495). – Boro Jun 17 '12 at 07:37
  • @Boro Will there be a performance in using `JLabel` since I want to continuously capture and draw image ? This also makes this question slightly different. – Aamir Rizwan Jun 20 '12 at 14:16
  • Do not worry about performance just now. First build the capturing and displaying. Then you could test the two approaches yourself on your code. – Boro Jun 20 '12 at 15:43

1 Answers1

0

Using JavaCV lib its possible to draw the BufferedImage in JPanel with class Graphics without using IplImages.

Here is an example that display images from webcam in JPanel inside JFrame.

try {
        OpenCVFrameGrabber camera = new OpenCVFrameGrabber(0);
        OpenCVFrameConverter.ToMat converter = new OpenCVFrameConverter.ToMat();
        Java2DFrameConverter converterBuffered = new Java2DFrameConverter();

        Frame capturedFrame = null;
        Mat matImage = null;
        camera.start();
        while ((capturedFrame = camera.grab()) != null) {
            matImage = converter.convertToMat(capturedFrame);

            BufferedImage bufferedImage = converterBuffered.convert(capturedFrame);

            Graphics g = imageView.getGraphics(); //getting the Graphics Class of the JPanel named as imageView
            g.drawImage(bufferedImage, 10,10, bufferedImage.getWidth(), bufferedImage.getHeight(),imageView); //this imageView is a JPanel component

        }
        camera.stop();

    } catch (FrameGrabber.Exception ex) {
        System.out.println("Fail to capture frame");
        //this catch clause refer to errors on opening the camera or failure on capture of frame.
    }