這篇文章給大家介紹怎么在Java中利用OpenCV3.2播放視頻,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比金林網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式金林網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋金林地區(qū)。費用合理售后完善,10余年實體公司更值得信賴。
實現(xiàn)思路
首先用OpenCV相關(guān)API讀取視頻流或者視頻文件的每一幀,然后通過Swing JComponent組件實現(xiàn)視頻每一幀的更新顯示,我模仿了C++的HIGHGUI里面創(chuàng)建窗口與顯示圖像接口,基于Swing實現(xiàn)了一個視頻播放窗口類,把讀取到的每一幀都傳給它就可以實現(xiàn)連續(xù)顯示即播放。每幀之間相隔100毫秒,我是通過Java線程Sleep方法實現(xiàn)。
運行效果 - USB攝像頭讀取每幀
運行效果 - 視頻文件讀取每幀
代碼實現(xiàn)
視頻文件讀取
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); // 打開攝像頭或者視頻文件 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視頻讀與播放演示", new Dimension(frame_width, frame_height)); Mat frame = new Mat(); while(true) { boolean have = capture.read(frame); Core.flip(frame, frame, 1);// Win上攝像頭 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 r=0, g=0, b=0; for(int row=0; row視頻與圖像顯示窗口類
package com.gloomyfish.video.demo; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import javax.swing.JComponent; import javax.swing.JDialog; public class ImageGUI extends JComponent { /** * */ private static final long serialVersionUID = 1L; private BufferedImage image; public ImageGUI() { } @Override protected void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D)g; if(image == null) { g2d.setPaint(Color.BLACK); g2d.fillRect(0, 0, this.getWidth(), this.getHeight()); } else { g2d.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), null); System.out.println("show frame..."); } } public void createWin(String title) { JDialog ui = new JDialog(); ui.setTitle(title); ui.getContentPane().setLayout(new BorderLayout()); ui.getContentPane().add(this, BorderLayout.CENTER); ui.setSize(new Dimension(330, 240)); ui.setVisible(true); } public void createWin(String title, Dimension size) { JDialog ui = new JDialog(); ui.setTitle(title); ui.getContentPane().setLayout(new BorderLayout()); ui.getContentPane().add(this, BorderLayout.CENTER); ui.setSize(size); ui.setVisible(true); } public void imshow(BufferedImage image) { this.image = image; this.repaint(); } }關(guān)于怎么在Java中利用OpenCV3.2播放視頻就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
當(dāng)前題目:怎么在Java中利用OpenCV3.2播放視頻
鏈接分享:http://weahome.cn/article/jcgojs.html