你好,游客 登录 注册 搜索
背景:
阅读新闻

Qt5下实现摄像头预览及捕获图像方法实例

[日期:2016-03-01] 来源:Linux社区  作者:annt [字体:大 中 小]

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:编译工程效果如下

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

Qt 的详细介绍:请点这里
Qt 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-03/128792.htm

linux
相关资讯       Qt5 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款