现在的毕业设计越做越出花来了 用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.blogjava.net/Files/zarra/Demo001.zip
posted on 2008-05-10 12:33
zarra 阅读(1466)
评论(6) 编辑 收藏