2015-10-24 50 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