【问题标题】:Using the camera of the laptop with swing in java [closed]在java中使用带有swing的笔记本电脑的相机[关闭]
【发布时间】: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 网络摄像头,发现了大量代码。

标签: java swing camera


【解决方案1】:

使用Webcam Capture 项目。

作者提供的 API 使用示例:

Webcam buildin = Webcam.getWebcams().get(0); // build-in laptop camera
Webcam usb = Webcam.getWebcams().get(1); // usb camera
BufferedImage image1 = buildin.getImage();
BufferedImage image2 = usb.getImage(); // do with image1 and image2 whatever you want

【讨论】:

  • 其实这个例子太大了,无法和我的项目集成......所以请给我一个小而简单的例子......再次感谢......
  • 而且我也无法导入 SARXOS 网络摄像头示例给出的项目....
  • @Gopal00005 实际上这个例子太大了,无法与我的项目集成 真的吗? 四行太大了?
  • 天哪,这正在变成一场闹剧。我认为他希望你为他做所有的工作。四行代码是一大堆代码(叹气),所以你可以理解为什么你应该飞到 OP 那里,握住他们的手却只有 3 行代码!为回答问题并不得不处理我在这里见过的最懒惰的海报之一而 +1!
  • 但是先生,我不知道如何使用 SARXOS 示例,您能否详细说明如何使用它......我的意思是一步一步......因为我无法工作与 SARXOS。很抱歉,但我是 Java 新手。
【解决方案2】:

Java Media Framework (jmf) 就是您要找的。​​p>

【讨论】:

  • 我只需要一个例子来使用相机,有人可以帮我吗?
  • 没有。自己做一些研究。表现出一些努力!
  • 为此 +1,这是一个很大的帮助,但我认为他希望您为他编写它,或者找到一个已经完成所有工作的示例。这就是你提供帮助的好处吗?
  • 其实 SARXOS API 是很棒的 AP​​I,我也尝试了 jmf,但为了简单和可靠,最好使用 SARXOS。非常感谢朋友...
猜你喜欢
  • 2023-01-03
  • 2023-01-21
  • 2021-12-31
  • 1970-01-01
  • 2020-05-12
  • 2011-02-25
  • 1970-01-01
  • 2020-03-02
  • 1970-01-01
相关资源
最近更新 更多