如何通过QT软件实现与USB摄像头的高效连接与数据交互?
- 内容介绍
- 文章标签
- 相关推荐
本文共计453个文字,预计阅读时间需要2分钟。
本文以家庭分享为例,展示了使用QT连接USB摄像头的相关代码,供大家参考。
功能:使用QT连接USB摄像头,点击按钮显示画面。
代码示例:cpp#include #include #include #include #include #include
class MainWindow : public QMainWindow {public: MainWindow(QWidget *parent=nullptr) : QMainWindow(parent) { setupUI(); }
private: void setupUI() { QVideoWidget *videoWidget=new QVideoWidget(this); QVideoCapture *videoCapture=new QVideoCapture(0); // 0代表第一个USB摄像头
connect(videoCapture, &QVideoCapture::videoFrameAvailable, this, &MainWindow::processFrame); videoCapture->start();
QVBoxLayout *layout=new QVBoxLayout(this); layout->addWidget(videoWidget);
setCentralWidget(new QWidget(this)); setCentralWidget(QWidget::createWindowContainer(videoWidget)); }
void processFrame() { QVideoFrame frame=videoCapture->currentFrame(); QImage image=frame.image(); QLabel *label=new QLabel(this); label->setPixmap(QPixmap::fromImage(image)); label->show(); }};
int main(int argc, char *argv[]) { QApplication app(argc, argv); MainWindow window; window.show(); return app.exec();}
本文实例为大家分享了使用QT连接USB摄像头的具体代码,供大家参考,具体内容如下
功能:使用QT连接USB摄像头,点击按钮显示画面
QT += multimedia
QT += multimediawidgets
#include "camera.h" #include "ui_camera.h" Camera::Camera(QWidget *parent) : QWidget(parent), ui(new Ui::Camera) { ui->setupUi(this); iniCamera(); } Camera::~Camera() { delete ui; } void Camera::iniCamera() { cameras = QCameraInfo::availableCameras();//获取摄像头列表 qDebug()<<cameras.size(); for(int i = 0;i<cameras.size();i++) ui->comboCamera->addItem(cameras[i].description());//摄像头描述 } void Camera::on_camStartBtn_clicked() { curCamera=new QCamera(cameras[ui->comboCamera->currentIndex()],this);//新建QCamera curCamera->setViewfinder(ui->widget); //设置取景框预览 curCamera->start(); }
#ifndef CAMERA_H #define CAMERA_H #include <QWidget> #include <QCameraInfo> #include <QCamera> #include <QLabel> #include <QCameraViewfinder> #include <QCameraImageCapture> #include <QMediaRecorder> namespace Ui { class Camera; } class Camera : public QWidget { Q_OBJECT public: explicit Camera(QWidget *parent = nullptr); ~Camera(); void iniCamera();//摄像头初始化 QCamera *curCamera=Q_NULLPTR;// QList<QCameraInfo> cameras;//可用相机列表 QCameraImageCapture *imageCapture; //抓图 QMediaRecorder* mediaRecorder;//录像 private slots: void on_camStartBtn_clicked(); private: Ui::Camera *ui; }; #endif // CAMERA_H
实现效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。
本文共计453个文字,预计阅读时间需要2分钟。
本文以家庭分享为例,展示了使用QT连接USB摄像头的相关代码,供大家参考。
功能:使用QT连接USB摄像头,点击按钮显示画面。
代码示例:cpp#include #include #include #include #include #include
class MainWindow : public QMainWindow {public: MainWindow(QWidget *parent=nullptr) : QMainWindow(parent) { setupUI(); }
private: void setupUI() { QVideoWidget *videoWidget=new QVideoWidget(this); QVideoCapture *videoCapture=new QVideoCapture(0); // 0代表第一个USB摄像头
connect(videoCapture, &QVideoCapture::videoFrameAvailable, this, &MainWindow::processFrame); videoCapture->start();
QVBoxLayout *layout=new QVBoxLayout(this); layout->addWidget(videoWidget);
setCentralWidget(new QWidget(this)); setCentralWidget(QWidget::createWindowContainer(videoWidget)); }
void processFrame() { QVideoFrame frame=videoCapture->currentFrame(); QImage image=frame.image(); QLabel *label=new QLabel(this); label->setPixmap(QPixmap::fromImage(image)); label->show(); }};
int main(int argc, char *argv[]) { QApplication app(argc, argv); MainWindow window; window.show(); return app.exec();}
本文实例为大家分享了使用QT连接USB摄像头的具体代码,供大家参考,具体内容如下
功能:使用QT连接USB摄像头,点击按钮显示画面
QT += multimedia
QT += multimediawidgets
#include "camera.h" #include "ui_camera.h" Camera::Camera(QWidget *parent) : QWidget(parent), ui(new Ui::Camera) { ui->setupUi(this); iniCamera(); } Camera::~Camera() { delete ui; } void Camera::iniCamera() { cameras = QCameraInfo::availableCameras();//获取摄像头列表 qDebug()<<cameras.size(); for(int i = 0;i<cameras.size();i++) ui->comboCamera->addItem(cameras[i].description());//摄像头描述 } void Camera::on_camStartBtn_clicked() { curCamera=new QCamera(cameras[ui->comboCamera->currentIndex()],this);//新建QCamera curCamera->setViewfinder(ui->widget); //设置取景框预览 curCamera->start(); }
#ifndef CAMERA_H #define CAMERA_H #include <QWidget> #include <QCameraInfo> #include <QCamera> #include <QLabel> #include <QCameraViewfinder> #include <QCameraImageCapture> #include <QMediaRecorder> namespace Ui { class Camera; } class Camera : public QWidget { Q_OBJECT public: explicit Camera(QWidget *parent = nullptr); ~Camera(); void iniCamera();//摄像头初始化 QCamera *curCamera=Q_NULLPTR;// QList<QCameraInfo> cameras;//可用相机列表 QCameraImageCapture *imageCapture; //抓图 QMediaRecorder* mediaRecorder;//录像 private slots: void on_camStartBtn_clicked(); private: Ui::Camera *ui; }; #endif // CAMERA_H
实现效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

