由于某些原因,此代码似乎找不到内置的网络摄像头:
package com.lorenjz.smooth;
import java.awt.BorderLayout;
import javax.media.*;
import javax.media.protocol.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import javax.media.control.FormatControl;
import javax.media.format.VideoFormat;
public class test {
public static void main(String[] args){
test t = new test();
t.getCam();
}
public void getCam(){
try{
/* Grab the default web cam*/
MediaLocator ml = new MediaLocator("vfw://2");
/* Create my data source */
DataSource ds = Manager.createDataSource(ml);
requestFormatResolution(ds);
/* Create & start my player */
Player p = Manager.createRealizedPlayer(ds);
p.start();
Thread.currentThread().sleep(1000);
/* code for creating a JFrame and adding the visual component to it */
JFrame jfrm=new JFrame("Testing Webcam");
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
if(p.getVisualComponent()!=null)jfrm.getContentPane().add(p.getVisualComponent());
if(p.getControlPanelComponent()!=null)jfrm.getContentPane().add
(p.getControlPanelComponent(),BorderLayout.SOUTH);
jfrm.pack();
jfrm.setLocationRelativeTo(null);
jfrm.setVisible(true);
jfrm.setSize(320,240);
}catch(Exception e){
e.printStackTrace();
}
}
public boolean requestFormatResolution(DataSource ds) {
if (ds instanceof CaptureDevice) {
FormatControl[] fcs = ((CaptureDevice) ds).getFormatControls();
for (FormatControl fc : fcs) {
Format[] formats = ((FormatControl) fc).getSupportedFormats();
for (Format format : formats) {
if ((format instanceof VideoFormat) &&
(((VideoFormat)format).getSize().getHeight() <= 240) &&
(((VideoFormat)format).getSize().getWidth() <= 320)) {
((FormatControl) fc).setFormat(format);
return true;
}
}
}
}
return false;
}
}
控制台返回以下错误:
javax.media.NoDataSourceException: Cannot find a DataSource for: vfw://0
at javax.media.Manager.createDataSource(Manager.java:1037)
at com.lorenjz.smooth.test.getCam(test.java:21)
at com.lorenjz.smooth.test.main(test.java:14)
我试图找到与此行不同的参数:
MediaLocator ml = new MediaLocator("vfw://2");
,但我一直无法。谁能提供任何建议?
谢谢,
劳伦
最佳答案
AFAIK,“ vfw://”是“ Video For Windows”对象的方案...显然,这是特定于平台的。
您应该使用CaptureDeviceManager.getDeviceList(null)
查看系统上可用的设备,然后从这些选项中进行选择。