Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use OpenCV3.2 to realize Video Reading and playback in Java

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how Java uses OpenCV3.2 to read and play videos. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The details are as follows

Since the 3.x version of OpenCV, the SDK of JAVA language supports reading and writing of video files, which makes it very convenient for Java developers to learn and use OpenCV, read the content and playback of frames through cameras or video files, and complete various application development tasks such as video content analysis and object tracking. It can be said that OpenCV C++ SDK can do most things, and it can be done with Java on the OpenCV3.x version, which opens a convenient door for many Java developers to learn OpenCV.

Realization idea

First of all, every frame of the video stream or video file is read by the OpenCV-related API, and then the update display of each frame of the video is realized through the Swing JComponent component. I imitated the interface between creating window and displaying image in the HIGHGUI of C++, and implemented a video playback window class based on Swing. Passing every frame read to it can achieve continuous display and playback. Each frame is separated by 100ms, which I do through the Java thread Sleep method.

Code implementation

Video file reading

Package com.gloomyfish.video.demo;import java.awt.Dimension;import java.awt.image.BufferedImage;import org.opencv.core.Core;import org.opencv.core.Mat;import org.opencv.videoio.VideoCapture;public class VideoDemo {public static void main (String [] args) {System.loadLibrary (Core.NATIVE_LIBRARY_NAME); / / Open the camera or video file VideoCapture capture = new VideoCapture (); / / capture.open (0); capture.open ("D:/vcprojects/images/768x576.avi") If (! capture.isOpened ()) {System.out.println ("could not load video data..."); return;} int frame_width = (int) capture.get (3); int frame_height = (int) capture.get (4); ImageGUI gui = new ImageGUI (); gui.createWin ("OpenCV + Java Video Reading and playback Demo", new Dimension (frame_width, frame_height)); Mat frame = new Mat (); while (true) {boolean have = capture.read (frame) Core.flip (frame, frame, 1); / / camera if (! have) break; if (! frame.empty ()) {gui.imshow (conver2Image (frame)); gui.repaint ();} try {Thread.sleep (100);} catch (InterruptedException e) {e.printStackTrace ();} public static BufferedImage conver2Image (Mat mat) {int width = mat.cols (); int height = mat.rows (); int dims = mat.channels () Int [] pixels = new int [width*height]; byte [] rgbdata = new byte [width*height*dims]; mat.get (0,0, rgbdata); BufferedImage image = new BufferedImage (width, height, BufferedImage.TYPE_INT_ARGB); int index = 0; int rust 0, grub 0, bust 0; for (int row=0; row)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report