Java使用OpenCV3.2實現視頻讀取與播放

來源:互聯網
上載者:User

標籤:讀取   etc   ack   javascrip   key   blog   aml   for   org   

Java使用OpenCV3.2實現視頻讀取與播放

OpenCV從3.x版本開始其JAVA語言的SDK可使用視訊檔案讀寫,這樣就極大的方便了廣大Java語言開發人員學習與使用OpenCV,通過網路攝影機或者視頻檔案讀取幀的內容與播放,完成視頻內容分析與對象跟蹤等各種應用開發工作單位。可以說OpenCV C++ SDK可以做到絕大多數事情,在OpenCV3.x版本上用Java都可以完成,這樣就為很多Java開發人員學習OpenCV開啟了方便之門。

實現思路

首先用OpenCV相關API讀取視頻流或者視頻檔案的每一幀,然後通過Swing JComponent組件實現視頻每一幀的更新顯示,我模仿了C++的HIGHGUI裡面建立視窗與顯示映像介面,基於Swing實現了一個視頻播放視窗類別,把讀取到的每一幀都傳給它就可以實現連續顯示即播放。每幀之間相隔100毫秒,我是通過Java線程Sleep方法實現。

運行效果 - USB網路攝影機讀取每幀

運行效果 - 視頻檔案讀取每幀

代碼實現

視頻檔案讀取

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<height; row++) {            for(int col=0; col<width; col++) {                if(dims == 3) {                    index = row*width*dims + col*dims;                    b = rgbdata[index]&0xff;                    g = rgbdata[index+1]&0xff;                    r = rgbdata[index+2]&0xff;                    pixels[row*width+col] = ((255&0xff)<<24) | ((r&0xff)<<16) | ((g&0xff)<<8) | b&0xff;                 }                if(dims == 1) {                    index = row*width + col;                    b = rgbdata[index]&0xff;                    pixels[row*width+col] = ((255&0xff)<<24) | ((b&0xff)<<16) | ((b&0xff)<<8) | b&0xff;                 }            }        }        setRGB( image, 0, 0, width, height, pixels);        return image;    }    /**     * A convenience method for setting ARGB pixels in an image. This tries to avoid the performance     * penalty of BufferedImage.setRGB unmanaging the image.     */    public static void setRGB( BufferedImage image, int x, int y, int width, int height, int[] pixels ) {        int type = image.getType();        if ( type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )            image.getRaster().setDataElements( x, y, width, height, pixels );        else            image.setRGB( x, y, width, height, pixels, 0, width );    }}

視頻與映像顯示視窗類別

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();    }}

關注公眾號,擷取更多影像處理知識與OpenCV相關知識
【gloomyfish-賈志剛】

Java使用OpenCV3.2實現視頻讀取與播放

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.