先看一下效果:(最大传2M图片,所以我把图片缩小并降低了DPI)
注意:
一定不要把usb摄像头接到同一个USB集线器上,我这里是一个接到电脑外置接口,一个接到PCI-usb接口上了。如果要接更多的usb摄像头,最好接到PCI-USB接口上,这样才能保证同时打开,否则只能一个一个打开,不是我们想要的效果。
示例代码(这里代码只为了做出Demo,所以很简单):
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QCamera>
#include <QCameraInfo>
#include <QCameraViewfinder>
#include <QLabel>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void initCamera();
void initDeviceName();
private:
Ui::MainWindow *ui;
QList<QCameraInfo> m_camera;
QList<QCamera*> m_cameraList;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setFixedSize(600,200);
setWindowTitle("Demo of Use Cameras");
setWindowIcon(QIcon(":/Demo_Hello.ico"));
initDeviceName();
initCamera();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initCamera()
{
int count = m_camera.count();
for(int i = 0; i < count; i++) {
QWidget* wid = new QWidget(this);
wid->resize(this->width()/count, this->height());
wid->move(i*this->width()/count, 0);
QCamera* camera = new QCamera(m_camera.at(i));
QCameraViewfinder* viewfinder= new QCameraViewfinder(wid);
viewfinder->setFixedSize(wid->size());
camera->setViewfinder(viewfinder);
QLabel* title = new QLabel(this);
title->setText(m_camera.at(i).description());
title->resize(wid->width(), 20);
title->move(i*this->width()/count, 0);
title->setAlignment(Qt::AlignCenter);
title->setStyleSheet("background-color: black; color: white");
m_cameraList << camera;
if(i == 0) {
wid->setStyleSheet("background-color:red");
} else {
wid->setStyleSheet("background-color:blue");
}
m_cameraList.at(i)->start();
title->raise();
}
}
void MainWindow::initDeviceName()
{
m_camera.clear();
foreach (QCameraInfo info, QCameraInfo::availableCameras()) {
m_camera.append(info);
}
}
标签:count,wid,USB,title,Demo,QCamera,camera,include,MainWindow From: https://blog.51cto.com/u_15854865/5811182