我一直试图在 Java 上使用 OpenCV (我相对较新),但它似乎比其他语言复杂得多。虽然像python这样的语言有 imshow 来循环网络摄像头,但java没有这个选项。
我想要做的只是运行一个文件,它将加载opencv,并使用它在Jframe中处理的图像,以便从相机(我的网络摄像头)连续显示更新的图像(如视频)。
我可以获取快照的代码,但每当我尝试循环超过此时,我仍然只能获得一个快照。
这是代码: `的System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// Instantiating the VideoCapture class (camera:: 0)
VideoCapture capture = new VideoCapture(0);
// Reading the next video frame from the camera
Mat matrix = new Mat();
//Instantiate JFrame
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//When we find the user close our window.
frame.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
System.out.println("Frame closing...");
frame.setVisible(false);
capture.release();
}
});
// Instantiating the imgcodecs class
Imgcodecs imageCodecs = new Imgcodecs();
// Where we will save the image.
String file = "C:/Users/Jim/Documents/Samples/sample.jpg";
// If camera is opened
if (capture.isOpened()) {
frame.setVisible(true);
// While there is next video frame
while (capture.read(matrix)) {
System.out.println("Retrieving Frames...");
capture.read(matrix);
// Creating BuffredImage from the matrix
BufferedImage image = new BufferedImage(matrix.width(),
matrix.height(), BufferedImage.TYPE_3BYTE_BGR);
//Saving the mat to an image.
imageCodecs.imwrite(file, matrix);
imageCodecs.imread(file);
ImageIcon icon = new ImageIcon(file); // Inserts the image icon
JLabel label = new JLabel(icon); //Label of ImageIcon
frame.getContentPane().add(label);
frame.pack();
}
}`
请帮助我找到我需要添加的内容以更新图像,以及不需要的内容。