用QCamera读取摄像头图像并显示到QLabel上的代码示例
时间: 2023-04-05 09:00:32 浏览: 246
以下是使用QCamera读取摄像头图像并显示到QLabel上的代码示例:
```cpp
#include <QCamera>
#include <QCameraViewfinder>
#include <QCameraImageCapture>
#include <QVBoxLayout>
#include <QLabel>
#include <QWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// 创建一个QWidget作为主窗口
QWidget *mainWidget = new QWidget();
// 创建一个QCamera对象
QCamera *camera = new QCamera();
// 创建一个QCameraViewfinder对象
QCameraViewfinder *viewfinder = new QCameraViewfinder(mainWidget);
viewfinder->setFixedSize(640, 480);
// 创建一个QCameraImageCapture对象
QCameraImageCapture *imageCapture = new QCameraImageCapture(camera);
// 设置QCamera对象的视图finder和图像捕获imageCapture
camera->setViewfinder(viewfinder);
camera->setImageCapture(imageCapture);
// 创建一个QLabel对象用于显示图像
QLabel *label = new QLabel(mainWidget);
label->setFixedSize(640, 480);
// 创建一个QVBoxLayout对象,并将QCameraViewfinder和QLabel添加到其中
QVBoxLayout *layout = new QVBoxLayout(mainWidget);
layout->addWidget(viewfinder);
layout->addWidget(label);
// 开始捕获图像
camera->start();
// 将捕获的图像显示到QLabel上
QObject::connect(imageCapture, &QCameraImageCapture::imageCaptured, [=](int id, const QImage &image) {
label->setPixmap(QPixmap::fromImage(image));
});
// 显示主窗口
mainWidget->show();
return a.exec();
}
```
这段代码可以实现使用QCamera读取摄像头图像并显示到QLabel上的功能。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)