Code Monkey home page Code Monkey logo

qtcamera's Introduction

基于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君

微信公众号:Qt君

Qt君

  • 后续更新摄像头参数选择算法(自动匹配用户设置摄像头参数,分辨率帧率格式)。

qtcamera's People

Contributors

aeagean avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.