【发布时间】:2014-09-30 18:44:05
【问题描述】:
如何在 ubuntu 操作系统中使用 java 中的 swing 使用相机 请给我一些通过 java 使用相机的示例和指南。
public static void main(String[] args) {
CamDemo t = new CamDemo();
t.getCam();
}
public void getCam() {
try {
/* Grab the default web cam */
MediaLocator ml = new MediaLocator("vfw://0");
DataSource ds = Manager.createDataSource(ml);
requestFormatResolution(ds);
Player p = Manager.createRealizedPlayer(ds);
p.start();
Thread.currentThread().sleep(1000);
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.convoy.gpack.pack.CamDemo.getCam(CamDemo.java:32)
at com.convoy.gpack.pack.CamDemo.main(CamDemo.java:16)
【问题讨论】:
-
到目前为止你尝试了什么?
-
当你有 javafx 时为什么要摇摆
-
您好,先生,我没有任何相机集成的示例所以请告诉我一个示例......提前致谢。
-
您已获得示例但“无法使用”。你到底尝试了什么,你的研究工作在哪里?我们不是来为你写的。向我们展示您尝试过的内容、相关代码以及您遇到的具体问题。阅读how to write a Short, Self Contained, Correct Example 并在此处查看help centre。然后edit您的问题并提供所有必要的详细信息。投票结束,因为它暂时不在主题范围内。我用谷歌搜索了 Java 网络摄像头,发现了大量代码。