QCamera同时打开多个USB摄像头小Demo

QCamera同时打开多个USB摄像头小Demo
原创沙振宇 最后发布于2018-10-29 16:18:01 阅读数 4066  收藏
展开
先看一下效果:(CSDN最大传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);
    }
}
 
————————————————
版权声明:本文为CSDN博主「沙振宇」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u014597198/article/details/78297740

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值