news 2026/7/2 1:50:16

Qt-摄像头捕获画面

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Qt-摄像头捕获画面

在qt中捕获摄像头画面,在ui界面上添加一个comboBox控件、label标签和两个pushButton按钮,comboBox用于显示摄像头的设备,按钮用于开启摄像头和捕获当前帧的画面,label用于显示摄像头捕获的画面。

//需要在.pro文件中加上multimedia multimediawidgets QT += core gui multimedia multimediawidgets
//mainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" #include <QCameraImageCapture> #include<QCameraInfo> #include<QCameraViewfinder> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); //获取当前系统的摄像头设备列表 const QList<QCameraInfo> cameras = QCameraInfo::availableCameras(); for(const QCameraInfo &cameraInfo:cameras){ qDebug()<<cameraInfo.deviceName().toLocal8Bit().data(); ui->comboBox->addItem(cameraInfo.deviceName().toUtf8().data()); } } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { //创建摄像头对象(把系统中的摄像头路径给QCamera对象) camera = new QCamera(ui->comboBox->currentText().toUtf8()); //创建取景器对象,也就是用于显示摄像头画面的控件,并给对象一个父指针 QCameraViewfinder *viewfinder = new QCameraViewfinder(ui->label); //根据label大小设置取景器的大小 viewfinder->resize(ui->label->size()); //把取景器显示在ui中 viewfinder->show(); //绑定摄像头和摄像头画面控件 camera->setViewfinder(viewfinder); //打开摄像头 camera->start(); //捕获图像需要初始化一个新的QCameraImageCapture对象 imageCapture = new QCameraImageCapture(camera); camera->setCaptureMode(QCamera::CaptureStillImage); //绑定信号与槽函数进行保存当前帧数据 connect(imageCapture,&QCameraImageCapture::imageCaptured,this,&MainWindow::ImageCaptureToShow); } void MainWindow::on_pushButton_2_clicked() { imageCapture->capture(); } void MainWindow::ImageCaptureToShow(int id, const QImage &preview) { qDebug()<<"buhuo"; preview.save("./1.jpg"); } //mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QCamera> #include <QCameraImageCapture> #include <QCameraViewfinder> #include <QMainWindow> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void on_pushButton_clicked(); void on_pushButton_2_clicked(); void ImageCaptureToShow(int id,const QImage &preview); private: Ui::MainWindow *ui; QCamera *camera; // QCameraViewfinder *viewfinder; QCameraImageCapture *imageCapture; }; #endif // MAINWINDOW_H
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/2 1:48:15

聊聊跨域问题

跨域到底该谁管&#xff1f;浏览器、代理与 Gateway CORS适用&#xff1a;WeekFlow 前后端分离开发&#xff08;Vite Gateway Nginx&#xff09; 读者&#xff1a;前后端开发、运维1. 从一个报错说起 前端跑在 http://localhost:5173&#xff0c;Gateway 在 http://localhost…

作者头像 李华
网站建设 2026/7/2 1:46:37

SSE客户端C++实现(使用libcurl)

SSE协议 的全称是 Server-Sent Events&#xff08;服务器发送事件&#xff09;&#xff0c;本质是基于 HTTP 协议的 “单向实时推送技术”——只有服务器能主动给客户端发消息&#xff0c;除了发送订阅请求外&#xff0c;客户端只能接收数据。SSE消息是纯文本格式&#xff0c;S…

作者头像 李华
网站建设 2026/7/2 1:46:10

Qt问题记录002:QMap的erase陷阱,正常运行与调试模式结果不同

Qt的QMap循环删除元素&#xff08;erase&#xff09;&#xff0c;在运行时正常&#xff0c;在调试模式下报错&#xff0c;提供解决代码。关键词&#xff1a;QMap、erase、迭代器、遍历与删除问题描述&#xff1a;在使用 Qt 的QMap 容器时&#xff0c;尝试在遍历过程中删除元素&…

作者头像 李华
网站建设 2026/7/2 1:42:03

大模型评测与AI产品质量保障:第7篇 机器学习的三种学习范式

IT策士 10余年一线大厂经验&#xff0c;专注大模型测试、AI产品质量保障与职场进阶。我会在各个平台持续发布最新文章&#xff0c;助你少走弯路。 上一篇文章我们拆解了AI的六块技术拼图。但无论哪块拼图&#xff0c;背后驱动模型学习的都是三种核心范式——有监督学习、无监督…

作者头像 李华
网站建设 2026/7/2 1:39:44

turn_on_wheeltec_robot.launch

<launch><!-- Arguments参数 --><arg name"lidar_is_cx" default"false"/><arg name"car_mode" default"mini_4wd" doc"opt: mini_akm,senior_akm,top_akm_bs,top_akm_dl,mini_mec,senior_mec_bs,senior_m…

作者头像 李华