2012-07-10 66 views
2

我剛安裝明智從網站openimaj指令的幾乎所有的庫:http://sourceforge.net/p/openimaj/wiki/OpenIMAJ%20From%20Source/庫安裝並正常工作。我只需要一個示例代碼來從網絡攝像頭捕捉視頻並將其保存到硬盤。例如:Openimaj捕獲並保存集成相機或USB攝像頭視頻hardisk(JAVA)

這是把相機上的代碼,並顯示您的視頻:


import org.openimaj.image.MBFImage; 
import org.openimaj.video.VideoDisplay; 
import org.openimaj.video.VideoDisplayListener; 
import org.openimaj.video.capture.VideoCapture; 

public class VideoDemo { 
    ... 
    VideoCapture vc = new VideoCapture(320, 240); 
    VideoDisplay<MBFImage> vd = VideoDisplay.createVideoDisplay(vc); 
    ... 
} 

如何保存[VD]在磁盤上?

回答

1

您需要使用XuggleVideoWriter類。以下代碼在屏幕上顯示視頻內容,並將內容寫入文件,直至按下轉義鍵。視頻格式由文件名稱控制(即「video.flv」創建FLV格式的視頻)。

package org.openimaj.demos.sandbox; 

import java.awt.event.KeyAdapter; 
import java.awt.event.KeyEvent; 
import java.io.IOException; 

import javax.swing.SwingUtilities; 

import org.openimaj.image.MBFImage; 
import org.openimaj.video.Video; 
import org.openimaj.video.VideoDisplay; 
import org.openimaj.video.VideoDisplayListener; 
import org.openimaj.video.capture.VideoCapture; 
import org.openimaj.video.xuggle.XuggleVideoWriter; 

/** 
* Record the webcam to a file. 
* 
* @author Jonathon Hare ([email protected]) 
*/ 
public class VideoRecorder extends KeyAdapter implements VideoDisplayListener<MBFImage> { 
    private Video<MBFImage> video; 
    private VideoDisplay<MBFImage> display; 
    private XuggleVideoWriter writer; 
    private boolean close = false; 

    /** 
    * Default constructor 
    * @throws IOException 
    */ 
    public VideoRecorder() throws IOException { 
     //open webcam 
     video = new VideoCapture(320, 240); 

     //open display 
     display = VideoDisplay.createVideoDisplay(video); 

     //open a writer 
     writer = new XuggleVideoWriter("video.flv", video.getWidth(), video.getHeight(), 30); 

     //set this class to listen to video display events 
     display.addVideoListener(this); 

     //set this class to listen to keyboard events 
     SwingUtilities.getRoot(display.getScreen()).addKeyListener(this); 
    } 

    @Override 
    public void afterUpdate(VideoDisplay<MBFImage> display) { 
     //Do nothing 
    } 

    @Override 
    public void beforeUpdate(MBFImage frame) { 
     //write a frame 
     if (!close) { 
      writer.addFrame(frame); 
     } 
    } 

    @Override 
    public void keyPressed(KeyEvent key) { 
     //wait for the escape key to be pressed 
     close = key.getKeyCode() == KeyEvent.VK_ESCAPE; 
    } 

    /** 
    * Main method 
    * @param args ignored 
    * @throws IOException 
    */ 
    public static void main(String[] args) throws IOException { 
     new VideoRecorder(); 
    } 
} 
+0

感謝喬恩我把它完成。此代碼正在工作 – 2012-07-11 10:31:36

+0

我可以爲此課程添加音頻嗎? – 2012-08-02 06:48:48

相關問題