📄 devicesfinder.java~2~
字号:
package visage_v3_2;
import javax.media.*;
import javax.media.protocol.*;
import javax.media.control.FormatControl;
import javax.media.format.VideoFormat;
import java.awt.*;
import java.util.Vector;
import javax.swing.JOptionPane;
/*
This class opens a video capture device and reads frames in the given format.
ALL THE CODE WRITTEN HERE IS TAKEN FROM Amith Yamasani'S 'VideoPreview' CODE SAMPLE, SLIGHT MODIFICATIONS WHERE MADE.
*/
public class DevicesFinder implements BufferTransferHandler
{
public DevicesFinder(){}
public PushBufferDataSource findDevices(Frame frame)
{
PushBufferDataSource pbds = null;
// Preferred capture format parameters
String prefEncoding = "RGB";
float prefFPS = 30.0f;
Dimension prefSize = new Dimension(320, 240);
VideoFormat prefFormat = new VideoFormat(prefEncoding,prefSize,Format.NOT_SPECIFIED,null,prefFPS);
Vector devices = CaptureDeviceManager.getDeviceList(prefFormat);
if (devices.size() < 1)
{
JOptionPane.showMessageDialog(frame,
"No capture devices found, or devices does not support given format\n" +
prefFormat.toString(), null,
JOptionPane.ERROR_MESSAGE);
System.exit(-1);
}
CaptureDeviceInfo cdi = (CaptureDeviceInfo)devices.elementAt(0);
frame.cdi = cdi;
try
{
DataSource ds = Manager.createDataSource(cdi.getLocator());
if (ds instanceof CaptureDevice)
{
FormatControl[] fcs = ( (CaptureDevice) ds).getFormatControls();
for (int i = 0; i < cdi.getFormats().length; i++)
{
VideoFormat vf = (VideoFormat) cdi.getFormats()[i];
if (vf.matches(prefFormat))
{
prefFormat = (VideoFormat) vf.intersects(prefFormat);
fcs[0].setFormat(prefFormat);
break;
}
}
}
ds.connect();
pbds = (PushBufferDataSource) ds;
pbds.getStreams()[0].setTransferHandler(this);
pbds.start();
return pbds;
}
catch (Exception e)
{
JOptionPane.showMessageDialog(frame,"Could not connect to capture device, please check your webcam",null,JOptionPane.ERROR_MESSAGE);
System.exit(-1);
return null;
}
}
public void transferData(PushBufferStream pbs) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -