2015-10-24 53 views
2

我的演示代码是从我的笔记本电脑的集成摄像头和USB视频采集卡(STK1160)中选择一个摄像头。我的代码已附加。Qt 5与USB视频采集卡

main.cpp中:

#include "mainwindow.h" 
#include <QApplication> 

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    MainWindow w; 
    w.show(); 

    return a.exec(); 
} 

mainwindow.h:

#ifndef MAINWINDOW_H 
#define MAINWINDOW_H 

#include <QMainWindow> 
#include <QCamera> 
#include <QCameraInfo> 
#include <QCameraImageCapture> 

namespace Ui { 
    class MainWindow; 
} 

class MainWindow : public QMainWindow { 

    Q_OBJECT 

public: 
    explicit MainWindow(QWidget *parent = 0); 
    ~MainWindow(); 

private: 
    Ui::MainWindow *ui; 
    QList <QCameraInfo> camList; 
    QCamera *camera; 

private slots: 
    void onCameraChanged(int); 
}; 

#endif // MAINWINDOW_H 

mainwindow.cpp

#include "mainwindow.h" 
#include "ui_mainwindow.h" 

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), ui(new Ui::MainWindow) { 
    ui->setupUi(this); 

    camera = NULL; 
    connect(ui->cameraComboBox,static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),this, 
      &MainWindow::onCameraChanged); 

    // find all available cameras and put them in the combo box 
    camList = QCameraInfo::availableCameras(); 
    for(QList <QCameraInfo>::iterator it = camList.begin();it!=camList.end();++it) { 
     ui->cameraComboBox->addItem(it->description()); 
    } 
} 

MainWindow::~MainWindow() { 
    delete ui; 
} 

void MainWindow::onCameraChanged(int idx) { 
    if(camera != NULL) { 
     camera->stop(); 
    } 
    camera = new QCamera(camList.at(idx),this); 
    camera->setViewfinder(ui->viewfinder); 
    camera->setCaptureMode(QCamera::CaptureStillImage); 
    camera->start(); 
} 

我的问题是,当我选择从组合框中的USB采集卡,我收到以下错误消息:

libv4l2: error turning on stream: Message too long 
CameraBin error: "Error starting streaming on device '/dev/video1'." 
CameraBin error: "Could not negotiate format" 

和相机视图全是黑色。任何人有任何想法?我在AV屏幕上测试了我的视频输入,效果很好。

+0

没有人会回答.... – zhoudingjiang

+0

Qt用户库相对其他用户来说很小,这导致了S.O上很少/很慢的响应。不幸的是:-( –

+0

我在发布这个问题后几天内解决了这个问题,通过使用openCV。fork()过程,并且在子进程中,我运行openCV代码来读取相机,就像普通的USB摄像头一样然后,我使用共享内存+信号量在子进程和母进程之间交换数据,虽然有点复杂,但运行得很好,需要注意的一点是openCV中的图像存储为BGR,而图像中Qt保存为RGB。 – zhoudingjiang

回答

0

我没有一个非常具体的答案,但关于相机&媒体API里面的更一般的答案Qt5。根据我的经验,某些功能仅适用于某些平台,而其他平台适用于其他平台。例如,我目前正努力工作QVideoProbeUbuntu即使它工作正常Android

近期,移动平台上Qt开发人员的开发重点似乎达到了80%。同样在Linux平台上,视频的“后端”是gstreamer,这意味着大多数错误都源于此。我最好的提示是升级到Qt 5.6,它依靠gstreamer1.0而不是古代的gstreamer0.1。另外请确保为您的平台安装所有gstreamer插件等,因为这可能会对媒体的运行有很大影响。

此外,如果您可以直接在gstreamer中重现错误,那么您可能可以在其中找到修复程序,然后此修复程序也可以从Qt中获得。例如,如果你缺少一个编解码器或驱动程序,使用gstreamer tools添加您需要的支持可能会解决问题

我发现在Qt中的媒体API是固体,我知道工作不断被填写每个平台媒体后端的集结功能,因此每次更新Qt应增加更多功能并修复错误。

我希望这会有所帮助,即使它没有直接解决你的问题(这可能是因为很少有你的确切经验)。

+0

感谢您的回复,我使用OpenCV + Qt + IPC解决了这个问题。 – zhoudingjiang