我很长时间都在为这个问题而战.我无法让OpenCV工作,我已经关注了很多关于它的教程以及如何在Qt中使用,所以我累了,我想避免使用OpenCV.
现在,我的要求或问题......我需要在一个只有一个按钮的Qt GUI应用程序中显示网络摄像头提要(实时视频,没有音频):"拍摄快照",显然,从当前提要中拍摄照片,存储它.
就这样.
无论如何在没有使用OpenCV的情况下完成这项工作?
系统规格:
Qt 4.8
Windows XP 32位
USB 2.0.1.3M UVC WebCam(我现在使用的那个,它也应该支持其他型号)
希望有人可以帮助我,因为我疯了.
提前致谢!
好的,我终于做到了,所以我会在这里发布我的解决方案,以便我们对此有一些明确的看法.
我使用了一个名为"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;
}
Run Code Online (Sandbox Code Playgroud)
和头文件:
#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
Run Code Online (Sandbox Code Playgroud)
这很简单,但仅仅是出于示例目的.使用应该是这样的:
Camera cam(320, 240);
cam.initialize();
cam.capture();
QImage img(cam.getImage());
ui->label->setPixmap(QPixmap::fromImage(img));
Run Code Online (Sandbox Code Playgroud)
当然,您可以使用QTimer并在QLabel中更新框架,您将在那里看到视频......
希望它有所帮助!并感谢尼古拉斯的帮助!