現在的畢業設計越做越出花來了 用java進行視頻捕捉這樣的需求也會出現 其實也不是不可以 只是這樣做有意思嗎
簡單上網查了下 用java捕捉不外乎用采集卡原生的api做個jni的wrapper和用directshow或vfw等windows通用的api做jni兩種做法 當然有同學硬要做video4linux的api我也不會反對的
因為我沒有那位同學的采集卡 所以用廠商的sdk的方案短期內不現實 所以我用了sun的jmf庫來采集
jmf在windows平臺下用的是vfw api 效率比較低 另外有一個商業的directshow的java wrapper名字叫dsj 這個性能基本和c++打平了 不過這個需要授權費 所以算了吧
jmf的安裝很簡單 去 http://java.sun.com/javase/technologies/desktop/media/jmf/2.1.1/download.html 下載下來 安裝好以后在桌面會看到一個JMStudio圖標 運行JMStudio如果視頻采集正常的話 說明jmf能識別采集卡(其實就是采集卡提供了vfw接口)至此視頻采集成功了大半 簡單吧
=============================================================================================================================
下面是代碼片斷和簡單說明
PlayerFrame是一個視頻播放控制類 我們就創建一個繼承自他的新類Demo
public class Demo extends PlayerFrame {
public Demo() {
super(null, "Capture");
}
...
設備的選擇連接和打開
CaptureDialog是一個捕捉設備選擇對話框
JMFUtils.createCaptureDataSource函數根據設備創建datasource
private void init() throws NoPlayerException, IOException {
// setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
String nameCaptureDeviceAudio = null;
String nameCaptureDeviceVideo = null;
CaptureDialog dialogCapture = new CaptureDialog(this, null);
dialogCapture.show();
if (dialogCapture.getAction() == CaptureDialog.ACTION_CANCEL)
return;
CaptureDeviceInfo cdi = dialogCapture.getAudioDevice();
if (cdi != null && dialogCapture.isAudioDeviceUsed())
nameCaptureDeviceAudio = cdi.getName();
cdi = dialogCapture.getVideoDevice();
if (cdi != null && dialogCapture.isVideoDeviceUsed())
nameCaptureDeviceVideo = cdi.getName();
dataSource = JMFUtils.createCaptureDataSource(nameCaptureDeviceAudio,
dialogCapture.getAudioFormat(), nameCaptureDeviceVideo,
dialogCapture.getVideoFormat());
DataSource cdswrapper = new CDSWrapper(
(PushBufferDataSource) dataSource);
dataSource=cdswrapper;
dataSource.connect();
open(dataSource);
...
preview畫面顯示控件的放置
public void createComponent() throws NoPlayerException, IOException {
setTitle("視頻信號");
// addWindowListener(new WinClose());
panel = new JPanel();
if ((com = mediaPlayerCurrent.getVisualComponent()) != null) {
panel.add(com);
}
add(BorderLayout.CENTER, panel);
}
單幀捕捉
JButton capture = new JButton("Capture Image");
capture.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
mediaPlayerCurrent.stop();
Buffer bufferFrame;
BufferToImage bufferToImage;
Image image;
BufferedImage bi;
controlGrabber = (FrameGrabbingControl) mediaPlayerCurrent
.getControl("javax.media.control.FrameGrabbingControl");
bufferFrame = controlGrabber.grabFrame();
bufferToImage = new BufferToImage((VideoFormat) bufferFrame
.getFormat());
image = bufferToImage.createImage(bufferFrame);
File out = new File("capture" + (++captureCount) + ".png");
try {
bi = toBufferedImage(image);
ImageIO.write(bi, "png", out);
} catch (IOException e1) {
e1.printStackTrace();
}
mediaPlayerCurrent.start();
}
});
寫好的文件在 http://www.tkk7.com/Files/zarra/Demo001.zip
posted on 2008-05-10 12:33
zarra 閱讀(1472)
評論(6) 編輯 收藏