0

我想从网络摄像头捕获和显示视频。为了捕获,我使用了这个 JavaCV 代码,它提供了可以转换为BufferedImage的IplImage。如何在 JFrame 上显示此图像?

注意:我想显示视频而不是单个图像,即图像应该在从网络摄像头捕获时不断更新

4

1 回答 1

0

使用 JavaCV lib 可以在 JPanel 中使用类 Graphics 绘制 BufferedImage 而无需使用 IplImages。

这是一个在 JFrame 内的 JPanel 中显示来自网络摄像头的图像的示例。

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.
    }
于 2017-12-23T18:10:03.573 回答