qt qmidarea 关闭事件_Qt摄像头热插拔实现

3c74045d9f7853e57809838a511985c6.png
基于Qt的Camera模块实现摄像头的热插拔。当只有一个摄像头时,被拔开后再插入能自动恢复相机状态。当有多个摄像头时,拔开当前摄像头会自动设置另外一个摄像头。

基于定时查询的方法

  • 定时查询设备列表变化;
connect(&m_checkDeviceListTimer, SIGNAL(timeout()), this, SLOT(checkDeviceList()));
  • 如果当前设备列表对比上一次缓存的设备列表有变化则发送设备列表变化事件(信号)deviceListChanged()
void QtCamera::checkDeviceList()
{
    QList<QCameraInfo> curCameraInfoList = QCameraInfo::availableCameras();
    if ( m_preCameraInfoList.count() != curCameraInfoList.count() ) {
        emit deviceListChanged();
    }

    m_preCameraInfoList = curCameraInfoList;
}

单设备重连机制

  • 先前autoRestore()槽函数绑定设备列表变化信号。
connect(this, SIGNAL(deviceListChanged()), this, SLOT(autoRestore()));
  • 触发自动重连机制。
    当设备存在则会重新启动start();
    当设备不存在则关闭该设备。
void QtCamera::autoRestore()
{
    if (! m_isStarted) {
        return;
    }

    if (deviceExist(m_curCameraInfo)) {
        if (m_camera->status() != QCamera::ActiveStatus) {
            QTimer::singleShot(500, m_camera, SLOT(start()));
        }
    }
    else {
        if (m_camera->status() == QCamera::ActiveStatus) {
            m_camera->stop();
            m_camera->unload();
        }
    }
}

多设备自动切换

  • autoSelectDevice()槽函数连接设备列表变化信号。
connect(this, SIGNAL(deviceListChanged()), this, SLOT(autoSelectDevice()));
  • 当设备已存在再次刷新当前的设备;
  • 当设备被断开,自动切换到第一个设备。
void QtCamera::autoSelectDevice()
{
    QList<QCameraInfo> curCameraInfoList = QCameraInfo::availableCameras();
    if (curCameraInfoList.isEmpty())
        return;

    if (curCameraInfoList.contains(m_curCameraInfo)) {
        selectDevice(m_curCameraInfo);
        return;
    }

    selectDevice(curCameraInfoList.first());
}

关于更多

  • 源码地址:
https://github.com/aeagean/QtCamera
  • 文章首发公众号:Qt君
  • 后续更新摄像头参数选择算法(自动匹配用户设置摄像头参数,分辨率,帧率,格式)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值