【问题标题】:Windows + Qt and how to capture webcam feed without OpenCVWindows + Qt 以及如何在没有 OpenCV 的情况下捕获网络摄像头
【发布时间】:2013-03-17 06:46:49
【问题描述】:

长期以来,我一直在与这个问题作斗争。 我无法让 OpenCV 工作,并且我已经关注了很多关于它以及如何在 Qt 中使用的教程,所以我很累,我想避免为此使用 OpenCV。

现在,我的要求或问题...我需要在 Qt GUI 应用程序中显示一个网络摄像头提要(实时视频,无音频),只有一个按钮:“拍摄快照”,显然是从当前提要并存储它。

就是这样。

有没有办法在不使用 OpenCV 的情况下完成这项工作?

系统规格:

  • Qt 4.8

  • Windows XP 32 位

  • USB 2.0.1.3M UVC 网络摄像头(我现在用的,应该也支持其他机型)

希望有人能帮我解决这个问题,因为我快疯了。

提前致谢!

【问题讨论】:

  • 这里列出了一些选项:stackoverflow.com/questions/1259192/webcam-access-in-cDirectShow API 可能是最有效的。
  • Cr*p...我搜索和搜索,所以我不会重复一个问题,它就是...谢谢尼古拉斯
  • 好吧,我在您指出的问题上阅读了有关 DirectShow 的信息,它似乎很容易或几乎符合我的需求,问题是我找不到任何有关如何与 DirectShow 交互的完整示例来自 Qt。我现在正在做一些研究,但如果有人想提供帮助,我将不胜感激。
  • 您与它的交互方式与与任何库的交互方式非常相似,请确保编译器具有标头并且您正在链接它。
  • 是的,我只是在阅读它......当我想用视频源网络摄像头填充 QLabel 时,问题就出现了。奇怪的是我找不到任何关于它的例子,所以如果它比我想象的更难,我会有点紧张。

标签: windows qt webcam


【解决方案1】:

好的,我终于做到了,所以我将在这里发布我的解决方案,以便我们对此有所了解。

我使用了一个名为“ESCAPI”的库:http://sol.gfxile.net/escapi/index.html

这提供了一种从设备捕获帧的极其简单的方法。有了这些原始数据,我只需要创建一个 QImage,然后在 QLabel 中显示。

我创建了一个简单的对象来处理这个问题。

#include <QDebug>
#include "camera.h"

Camera::Camera(int width, int height, QObject *parent) :
    QObject(parent),
    width_(width),
    height_(height)
{
    capture_.mWidth = width;
    capture_.mHeight = height;
    capture_.mTargetBuf = new int[width * height];

    int devices = setupESCAPI();
    if (devices == 0)
    {
        qDebug() << "[Camera] ESCAPI initialization failure or no devices found";
    }
}

Camera::~Camera()
{
    deinitCapture(0);
}

int Camera::initialize()
{
    if (initCapture(0, &capture_) == 0)
    {
        qDebug() << "[Camera] Capture failed - device may already be in use";
        return -2;
    }
    return 0;
}

void Camera::deinitialize()
{
    deinitCapture(0);
}

int Camera::capture()
{
    doCapture(0);
    while(isCaptureDone(0) == 0);

    image_ = QImage(width_, height_, QImage::Format_ARGB32);
    for(int y(0); y < height_; ++y)
    {
        for(int x(0); x < width_; ++x)
        {
            int index(y * width_ + x);
            image_.setPixel(x, y, capture_.mTargetBuf[index]);
         }
    }
    return 1;
}

还有头文件:

#ifndef CAMERA_H
#define CAMERA_H

#include <QObject>
#include <QImage>
#include "escapi.h"

class Camera : public QObject
{
    Q_OBJECT

public:
    explicit Camera(int width, int height, QObject *parent = 0);
    ~Camera();
    int initialize();
    void deinitialize();
    int capture();
    const QImage& getImage() const { return image_; }
    const int* getImageRaw() const { return capture_.mTargetBuf; }

private:
    int width_;
    int height_;
    struct SimpleCapParams capture_;
    QImage image_;
};

#endif // CAMERA_H

它非常简单,但仅用于示例目的。 用法应该是这样的:

Camera cam(320, 240);
cam.initialize();
cam.capture();
QImage img(cam.getImage());
ui->label->setPixmap(QPixmap::fromImage(img));

当然,您可以使用 QTimer 并更新 QLabel 中的帧,您将在那里获得视频...

希望对您有所帮助!并感谢 Nicholas 的帮助!

【讨论】:

  • 我忘了说你需要将 lib escapi.dll 放在你的应用程序的同一目录中,当然,将 escapi.cpp 和 escapi.h 添加到你的编译命令中。
  • 哦哇——太棒了!比我的 Actionscript3 版本更容易理解。感谢分享!
  • 我收到“[Camera] ESCAPI 初始化失败或找不到设备”。我的腿上有摄像头
猜你喜欢
  • 1970-01-01
  • 2015-08-18
  • 2013-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-10
相关资源
最近更新 更多