QT 5.15 QCamera截图

环境win10 QT5.12利用USB摄像头截图保存到程序IMG文件下,用尼康单反连接电脑在官方装上 nikon webcam utikity 插件也可以当摄像头使用。ICON图标自己生成一个

代码如下

pro文件:

# Camera.pro


QT       += core gui multimedia multimediawidgets

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17
QT += network
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
    main.cpp \
    mainwindow.cpp


FORMS += \
    mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
    icon.qrc

mainwindow.h文件
 

  • 
    
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QCamera>
    #include <QCameraViewfinder>
    #include <QCameraImageCapture>
    #include <QComboBox>
    #include<QDir>
    #include<QCoreApplication>
    #include<QTimer>
    #include<QAction>
    #include<QSystemTrayIcon>
    #include<QMenu>
    #include<QImage>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
        void setfblComobox(QCamera *camera);
        QString strAppPath = QDir::toNativeSeparators(QCoreApplication::applicationDirPath());//程序路径获取
    
    private slots:
        void on_screenshot_clicked();
    
        void on_cameraType_activated(int index);
    
        void on_imageCaptured(int id, const QImage &preview);
    
        void on_fbl_activated(int index);
        void getimg();
        void Delay(int   msec);
        void initConnect();//托盘函数
    
    
    private:
        Ui::MainWindow *ui;
        QCamera *camera;
        QCameraViewfinder *viewfind;
        QList<QCameraInfo> cameras;
        QCameraImageCapture *imageCapture;
        QList<QSize> mResSize = {};//分辨率List 定义
        QComboBox *box;
       
        QAction*aboutAction ;//
        QSystemTrayIcon*trayIcon ;
        QMenu* trayIconMenu ;  // 托盘菜
    
    };
    #endif // MAINWINDOW_H
    //main.cpp
    
    #include "mainwindow.h"
    
    #include <QApplication>
    #include <QLocale>
    #include <QTranslator>
    #include<QMutex>
    #include<QDateTime>
    #include<QStyleFactory>
    
    int main(int argc, char *argv[])
    {
        
        QApplication a(argc, argv);
        a.setStyle(QStyleFactory::create("fusion"));
        MainWindow w;
        w.show();
        return a.exec();
    }

  • 
    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QDebug>
    #include <QCameraInfo>
    #include<QDateTime>
    #include<QDir>
    #include<QMessageBox>
    #include<QUrl>
    #include <mopi.h>
    
    int value = 0;
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        
    //     QPixmap *pixmap = new QPixmap(QPixmap::fromImage(QImageD_RunBEEPSHorizontalVertical(QImage(strAppPath + "\\IMG\\1.jpg"),0.999,10.9999)));
    //     pixmap->scaled(ui->imageview->size(), Qt::KeepAspectRatio);
    //     ui->imageview->setScaledContents(true);
    //     ui->imageview->setPixmap(*pixmap);
    
    
        this->setWindowIcon(QIcon(":/favicon.ico"));//标题LOGO
        this->setWindowTitle("Camera @20220327");
        this->setFixedSize(990,470);
        aboutAction = new QAction("关于", this); // 托盘空件创建
        this->aboutAction->setIcon(QIcon::fromTheme("help-about"));//托盘添加关于
    
        trayIconMenu = new QMenu(this);  // 托盘菜单
        this->trayIconMenu->addAction(aboutAction);//托盘添加关于按钮
        trayIcon = new QSystemTrayIcon(this); // 托盘图标
        this->trayIcon->setIcon(QIcon(":/favicon.ico"));
        this->trayIcon->setToolTip("Camera @20220327");
        this->trayIcon->setContextMenu(trayIconMenu);
        this->trayIcon->show();  // 托盘空件
        this->initConnect();//初始化托盘
    
    
        ui->cameraType->clear();
        cameras = QCameraInfo::availableCameras();
        foreach(const QCameraInfo &cameraInfo, cameras) {
            qDebug() << "CameraInfo:" << cameraInfo.description().toUtf8();
            ui->cameraType->addItem(cameraInfo.description().toUtf8());
        }
        camera = new QCamera(this);
        viewfind = new QCameraViewfinder(this);
        viewfind->setGeometry(10,10,480,320);
        camera->setViewfinder(viewfind);
        imageCapture = new QCameraImageCapture(camera);
    
        camera->start();
        connect(imageCapture, SIGNAL(imageCaptured(int, QImage)), this, SLOT(on_imageCaptured(int, QImage)));
        setfblComobox(camera);
     
    
    }
    
    
    void  MainWindow::initConnect()//托盘函数
    {
        //connect(this->aboutAction, SIGNAL(triggered()), this, SLOT(showAbout()));
        connect(this->aboutAction, &QAction::triggered, qApp, &QApplication::aboutQt);
        return ;
    
    }
    
    //void MainWindow::showAbout()
    //{
    //    QString msg = "DO16 \n Copyright © 2022-0101 .\n All Rights Reserved.\n ";
    //    QMessageBox::information(nullptr,"关于",msg);
    
    //}
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    void MainWindow::on_cameraType_activated(int index)
    {
        index = ui->cameraType->currentIndex();
        // qDebug()<<"Index"<< index <<": "<< ui->cameraType->currentText();
        camera->stop();
        camera = new QCamera(cameras[index]);
        camera->setCaptureMode(QCamera::CaptureVideo);
        camera->setViewfinder(viewfind);
        imageCapture = new QCameraImageCapture(camera);
        camera->start();
        connect(imageCapture, SIGNAL(imageCaptured(int, QImage)), this, SLOT(on_imageCaptured(int, QImage)));
        setfblComobox(camera);
    }
    
    void MainWindow::on_imageCaptured(int id, const QImage &preview){
    
        QPixmap *pixmap = new QPixmap(QPixmap::fromImage(preview));
        pixmap->scaled(ui->imageview->size(), Qt::KeepAspectRatio);
        ui->imageview->setScaledContents(true);
        ui->imageview->setPixmap(*pixmap);
    }
    
    void MainWindow::on_screenshot_clicked()
    {
        for(int i =0 ; i < 1; i++){
            this->Delay(70);
            this->getimg();
            if (i == 2){break;}
    
        }
    
    }
    
    void MainWindow::Delay(int   msec)//time*1000为秒数
    {
        QTime dieTime = QTime::currentTime().addMSecs(msec);
        while( QTime::currentTime() < dieTime )
            QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
    }
    
    
    void MainWindow::getimg()
    {
        //QDateTime current_date_time =QDateTime::currentDateTime();
        //QString current_date =current_date_time.toString("hhmmsszzz");
    
        //current_date;
        value++;
        QString current_date = QString::number(value);
        //在exe目录下建一个IMG文件夹用来保存图片
        imageCapture->capture(strAppPath + "\\IMG\\"+current_date+".jpg");
    
    }
    
    
    void MainWindow::on_fbl_activated(int index){
        index = ui->fbl->currentIndex();
        //qDebug()<<"Index"<< index <<": "<< ui->fbl->currentText();
        qDebug()<<"mResSize:"<<mResSize[index];
        //设置摄像头参数
        QCameraViewfinderSettings set;
        set.setResolution(mResSize[index]);
        camera->setViewfinderSettings(set);
    }
    
    void MainWindow::setfblComobox(QCamera *camera){
    
        mResSize.clear();
        mResSize = camera->supportedViewfinderResolutions();
        ui->fbl->clear();
        int i=0;
        foreach (QSize msize, mResSize) {
            //qDebug()<<msize;
            ui->fbl->addItem(QString::number(msize.width(),10)+"*"+QString::number(msize.height(),10), i++);
        }  //摄像头支持分辨率打印
        ui->fbl->setCurrentIndex(i-1);
    }

  • mainwindow.ui
    
    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>990</width>
        <height>470</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralwidget">
       <widget class="QPushButton" name="screenshot">
        <property name="geometry">
         <rect>
          <x>260</x>
          <y>410</y>
          <width>111</width>
          <height>31</height>
         </rect>
        </property>
        <property name="font">
         <font>
          <family>AcadEref</family>
          <pointsize>14</pointsize>
         </font>
        </property>
        <property name="text">
         <string>截图</string>
        </property>
       </widget>
       <widget class="QFrame" name="frame">
        <property name="geometry">
         <rect>
          <x>500</x>
          <y>10</y>
          <width>480</width>
          <height>340</height>
         </rect>
        </property>
        <property name="frameShape">
         <enum>QFrame::Box</enum>
        </property>
        <property name="frameShadow">
         <enum>QFrame::Raised</enum>
        </property>
        <widget class="QLabel" name="imageview">
         <property name="geometry">
          <rect>
           <x>10</x>
           <y>10</y>
           <width>461</width>
           <height>321</height>
          </rect>
         </property>
         <property name="text">
          <string/>
         </property>
        </widget>
       </widget>
       <widget class="QFrame" name="frame_2">
        <property name="geometry">
         <rect>
          <x>10</x>
          <y>10</y>
          <width>480</width>
          <height>340</height>
         </rect>
        </property>
        <property name="frameShape">
         <enum>QFrame::Box</enum>
        </property>
        <property name="frameShadow">
         <enum>QFrame::Raised</enum>
        </property>
       </widget>
       <widget class="QWidget" name="layoutWidget">
        <property name="geometry">
         <rect>
          <x>20</x>
          <y>390</y>
          <width>202</width>
          <height>48</height>
         </rect>
        </property>
        <layout class="QGridLayout" name="gridLayout">
         <item row="0" column="0">
          <widget class="QLabel" name="label_2">
           <property name="font">
            <font>
             <family>AcadEref</family>
             <pointsize>12</pointsize>
            </font>
           </property>
           <property name="text">
            <string>摄像头</string>
           </property>
          </widget>
         </item>
         <item row="1" column="0">
          <widget class="QLabel" name="label_3">
           <property name="font">
            <font>
             <family>AcadEref</family>
             <pointsize>12</pointsize>
            </font>
           </property>
           <property name="text">
            <string>分辨率</string>
           </property>
          </widget>
         </item>
         <item row="1" column="1">
          <widget class="QComboBox" name="fbl">
           <property name="font">
            <font>
             <family>AcadEref</family>
             <pointsize>10</pointsize>
            </font>
           </property>
          </widget>
         </item>
         <item row="0" column="1">
          <widget class="QComboBox" name="cameraType"/>
         </item>
        </layout>
       </widget>
      </widget>
      <widget class="QStatusBar" name="statusbar"/>
     </widget>
     <resources/>
     <connections/>
    </ui>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值