linux qt调用摄像头,Qt5下实现摄像头预览及捕获图像方法实例

本文介绍了如何使用Qt库中的QCamera类进行Linux摄像头操作,并深入探讨了V4L2接口和ioctl命令在底层调用中的作用。通过实例展示了如何捕获、显示和保存图像,同时预告了后续将要研究的高级主题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Step4:修改mainwindow.cpp

源码如下

#include "mainwindow.h"

#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :

QMainWindow(parent),

ui(new Ui::MainWindow)

{

ui->setupUi(this);

camera=new QCamera(this);

viewfinder=new QCameraViewfinder(this);

imageCapture=new QCameraImageCapture(camera);

ui->ImageView->addWidget(viewfinder);

ui->ImageCapture->setScaledContents(true);

camera->setViewfinder(viewfinder);

camera->start();

connect(imageCapture, SIGNAL(imageCaptured(int,QImage)), this, SLOT(displayImage(int,QImage)));

connect(ui->buttonCapture, SIGNAL(clicked()), this, SLOT(captureImage()));

connect(ui->buttonSave, SIGNAL(clicked()), this, SLOT(saveImage()));

connect(ui->buttonQuit, SIGNAL(clicked()), qApp, SLOT(quit()));

}

MainWindow::~MainWindow()

{

delete ui;

}

void MainWindow::captureImage()

{

ui->statusBar->showMessage(tr("capturing..."), 1000);

imageCapture->capture();

}

void MainWindow::displayImage(int , QImage image)

{

ui->ImageCapture->setPixmap(QPixmap::fromImage(image));

ui->statusBar->showMessage(tr("capture OK!"), 5000);

}

void MainWindow::saveImage()

{

QString fileName=QFileDialog::getSaveFileName(this, tr("save file"), QDir::homePath(), tr("jpegfile(*.jpg)"));

if(fileName.isEmpty()) {

ui->statusBar->showMessage(tr("save cancel"), 5000);

return;

}

const QPixmap* pixmap=ui->ImageCapture->pixmap();

if(pixmap) {

pixmap->save(fileName);

ui->statusBar->showMessage(tr("save OK"), 5000);

}

}

Step5:编译工程效果如下

30eb4039b912c0d85ad1b8c3937fa799.png

总结:QCamera类封装了很多底层操作,为了更进一步地了解Linux下摄像头的调用机制,我将在后续探讨V4L2及ioctl操作摄像头的机理。

Qt 的详细介绍:请点这里

Qt 的下载地址:请点这里

0b1331709591d260c1c78e86d0c51c18.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值