由于某种原因,此代码似乎无法找到内置的网络摄像头:
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");
但我一直没能。
任何人都可以提供任何建议吗?
谢谢,
罗兰