我想在无头(控制台)qt应用程序中使用摄像头(至少对于单元测试)。在Qt控制台应用程序中使用QCamera
但我面临Qt的问题。一旦我在控制台应用程序中使用我的代码,相机将无法工作 - 将不会调用事件QCameraImageCapture
。
如果我在gui应用程序中使用完全相同的代码,事件被触发,我可以捕获图像。
常见的代码,我用的是:
camera = new QCamera(cameras.at(config->cameraNumber()));
imageCapture = new QCameraImageCapture(camera);
connect(imageCapture, SIGNAL(readyForCaptureChanged(bool)), this, SLOT(readyForCapture(bool)));
camera->start(); // to start the viewfinder
// ——
void ImageCapture::readyForCapture(bool b) {
qDebug() << "ready for capture "<<b;
}
- 当我直视着我的主窗口的构造函数中调用的GUI应用程序的代码,它的工作原理(将触发事件)。
- 当我在我的qt控制台应用程序中调用此代码时,它不起作用(事件不会被触发)。
任何人都可以帮助我吗?由于
**更新8月29日 - 全码**
控制台应用程序:
的main.cpp
#include <QCoreApplication>
#include <QTest>
#include <QTimer>
#include <QDebug>
#include <runoneventloop.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
RunOnEventLoop * run = new RunOnEventLoop(&a);
QTimer::singleShot(0, run, SLOT(run()));
return a.exec();
}
RunOnEventLoop.cpp
#include "runoneventloop.h"
RunOnEventLoop::RunOnEventLoop(QObject *parent) :
QObject(parent)
{
}
void RunOnEventLoop::run() {
qDebug() << "hier run";
camera = new QCamera(0);
imageCapture = new QCameraImageCapture(camera);
connect(imageCapture, SIGNAL(readyForCaptureChanged(bool)), this, SLOT(readyForCapture(bool)));
camera->start(); // to start the viewfinder
}
void RunOnEventLoop::readyForCapture(bool b) {
qDebug() << "ready of capture "<<b;
}
RunOnEventLoop .h
#ifndef RUNONEVENTLOOP_H
#define RUNONEVENTLOOP_H
#include <QObject>
#include <QDebug>
#include <QCamera>
#include <QCameraImageCapture>
class RunOnEventLoop : public QObject
{
Q_OBJECT
public:
explicit RunOnEventLoop(QObject *parent = 0);
private:
QCamera* camera;
QCameraImageCapture* imageCapture;
signals:
public slots:
void run();
void readyForCapture(bool);
};
#endif // RUNONEVENTLOOP_H
GUI应用
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
qDebug() << "hier";
camera = new QCamera(0);
imageCapture = new QCameraImageCapture(camera);
connect(imageCapture, SIGNAL(readyForCaptureChanged(bool)), this, SLOT(readyForCapture(bool)));
camera->start(); // to start the viewfinder
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::readyForCapture(bool b) {
qDebug() << "ready of capture "<<b;
}
再次,这是相同的代码。控制台应用程序不调用readyForCapture方法,而gui应用程序调用它。
,你可以在这里下载档案:DOWNLOAD
我认为QtMultimedia取决于QtGui,所以你需要Qapplication对象。 – prajmus 2014-08-27 13:24:26