2014-08-27 128 views
0

我想在无头(控制台)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

+0

我认为QtMultimedia取决于QtGui,所以你需要Qapplication对象。 – prajmus 2014-08-27 13:24:26

回答

0

如果将是很好,如果你可以提供更多的东西你基于控制台的Qt应用程序的...你提供的代码,它是如何通过你的主代码叫什么名字?

反正只是猜测,如果没有事件被触发所有可能是因为您没有运行任何事件循环......你确定你的代码在你QCoreApplication对象的某些点呼叫exec()?您确定您呼叫connect()的对象的所有者是QCoreApplication的线程吗?

+0

看到更新.... – appsthatmatter 2014-08-28 10:18:36

+0

@ jjoe64:这是不够的...我需要看看如何声明'RunOnEventLoop' ...你能提供该类的完整代码吗? – 2014-08-28 13:48:12

+0

已上传完整代码 – appsthatmatter 2014-09-01 14:51:54

相关问题