我的演示代码是从我的笔记本电脑的集成摄像头和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屏幕上测试了我的视频输入,效果很好。
没有人会回答.... – zhoudingjiang
Qt用户库相对其他用户来说很小,这导致了S.O上很少/很慢的响应。不幸的是:-( –
我在发布这个问题后几天内解决了这个问题,通过使用openCV。fork()过程,并且在子进程中,我运行openCV代码来读取相机,就像普通的USB摄像头一样然后,我使用共享内存+信号量在子进程和母进程之间交换数据,虽然有点复杂,但运行得很好,需要注意的一点是openCV中的图像存储为BGR,而图像中Qt保存为RGB。 – zhoudingjiang