我想捕获并显示来自webacam的视频。为了捕获我正在使用this JavaCV code,它提供了 IplImage ,可以转换为 BufferedImage 。如何在JFrame上显示此图像?
注意:我想显示视频而不是单张图像,即图像应该在从网络摄像头捕获时连续更新
答案 0 :(得分:0)
使用JavaCV lib可以在不使用IplImages的情况下使用类Graphics在JPanel中绘制BufferedImage。
以下是在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.
}