Я пытался использовать OpenCV (что я относительно новичок) на Java , но это кажется намного сложнее, чем в других языках. В то время как такие языки, как 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();
}
}`
Пожалуйста, помогите мне найти то, что мне нужно добавить для обновления изображений, а также то, что не нужно.