package org.bo;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Vector;
import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.DataSink;
import javax.media.Format;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.Processor;
import javax.media.ProcessorModel;
import javax.media.control.FormatControl;
import javax.media.format.VideoFormat;
import javax.media.protocol.DataSource;
import javax.media.protocol.FileTypeDescriptor;
import javax.media.protocol.SourceCloneable;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MainFrame extends JFrame {
CaptureDeviceInfo captureDevice;
Component visualComponent;
Component controlPanelComponent;
Processor processor;
Player player;
DataSink fileWriter;
FormatControl formatControl;
JPanel pnlVideo = new JPanel(new BorderLayout());
JPanel contentPane;
JPanel pnlControl = new JPanel();
JButton btnStart = new JButton("开始采集");
JButton btnStop = new JButton("停止采集");
JFileChooser fileChooser = new JFileChooser();
public MainFrame()
{
super("视频采集软件");
contentPane = (JPanel) this.getContentPane();
//
this.captureDevice = getCaptureDeviceInfo();
//
try {
DataSource source = Manager.createDataSource(captureDevice.getLocator());
DataSource cloneableSource = Manager.createCloneableDataSource(source);
player = Manager.createRealizedPlayer(((SourceCloneable)cloneableSource).createClone());
//获取默认格式
formatControl = (FormatControl) player.getControl("javax.media.control.FormatControl");
Format defaultFormat = formatControl.getFormat();
//
FileTypeDescriptor outputType = new FileTypeDescriptor(FileTypeDescriptor.MSVIDEO);
ProcessorModel processorModel = new ProcessorModel(cloneableSource, new Format[]{ defaultFormat }, outputType);
processor = Manager.createRealizedProcessor(processorModel);
} catch (Exception e) {
processException(e);
}
//
// controlPanelComponent = player.getControlPanelComponent();
// if (controlPanelComponent != null) {
// contentPane.add("North", controlPanelComponent);
// }
visualComponent = player.getVisualComponent();
if (visualComponent!=null) {
pnlVideo.add(visualComponent);
contentPane.add(pnlVideo);
}
//
btnStop.setEnabled(false);
pnlControl.add(btnStart);
pnlControl.add(btnStop);
// pnlControl.add(formatControl.getControlComponent());
// pnlControl.setPreferredSize(new Dimension(120, 0));
contentPane.add("South", pnlControl);
//
btnStart.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
start();
}
});
btnStop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
stop();
}
});
//
pack();
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void start() {
String locatorString = getLocatorString();
if(locatorString==null)
{
return;
}
//
btnStart.setEnabled(false);
//
DataSource source = processor.getDataOutput();
MediaLocator dest = new MediaLocator( locatorString );
try {
//本地文件传输
fileWriter = Manager.createDataSink(source, dest);
fileWriter.open();
fileWriter.start();
} catch (Exception e) {
processException(e);
}
processor.start();
player.start();
//
btnStop.setEnabled(true);
}
private void stop() {
btnStop.setEnabled(false);
//
player.stop();
processor.stop();
try {
fileWriter.stop();
fileWriter.close();
} catch (IOException e) {
processException(e);
}
//
btnStart.setEnabled(true);
}
private void processException(Exception e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(this, e.toString(), "错误", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
private String getLocatorString()
{
if( JFileChooser.APPROVE_OPTION != fileChooser.showSaveDialog(this))
{
return null;
}
File file = fileChooser.getSelectedFile();
if (file==null) {
return null;
}
String locatorString = file.getAbsolutePath();
if( !locatorString.endsWith(".avi" ))
{
locatorString += ".avi";
}
locatorString = "file://" + locatorString;
return locatorString;
}
private CaptureDeviceInfo getCaptureDeviceInfo()
{
Format videoFormat = new VideoFormat(VideoFormat.RGB);
Vector<CaptureDeviceInfo> deviceList = CaptureDeviceManager.getDeviceList(videoFormat);
if(deviceList.size()<1)
{
JOptionPane.showMessageDialog(this, "未检测到视频输入设备。", "错误", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
String[] deviceNames = new String[deviceList.size()];
for (int i = 0; i < deviceList.size(); i ++ ) {
deviceNames[i] = deviceList.get(i).getName();
}
String deviceName = (String) JOptionPane.showInputDialog(this, "请选择视频输入设备", "请选择", JOptionPane.QUESTION_MESSAGE, null, deviceNames, deviceNames[0]);
if (deviceName==null) {
System.exit(0);
}
//
CaptureDeviceInfo captureDevice;
for (int i = 0; i < deviceList.size(); i ++ ) {
captureDevice = deviceList.get(i);
if( deviceName.equals(captureDevice.getName()) )
{
return captureDevice;
}
}
return null;
}
}