#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); m_view = new MyView(this); imageCapture = new QCameraImageCapture(camera); //ui->ImageView->addWidget(viewfinder); ui->ImageView->addWidget(m_view); ui->ImageCapture->setScaledContents(true); //camera->setViewfinder(viewfinder); camera->setViewfinder(m_view); 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())); connect(ui->buttonStop,SIGNAL(clicked()),camera,SLOT(stop())); connect(ui->buttonStart,SIGNAL(clicked()),camera,SLOT(start()));}MainWindow::~MainWindow(){ delete ui;}void MainWindow::captureImage(){ imageCapture->capture();}void MainWindow::displayImage(int , QImage image){ QByteArray ba; QBuffer buffer(&ba); buffer.open(QIODevice::WriteOnly); image.save(&buffer,"jpg"); ui->ImageCapture->setText(QString::number(ba.size())); QImage pic; pic.loadFromData(ba); ui->ImageCapture->setPixmap(QPixmap::fromImage(pic));}void MainWindow::saveImage(){ QString fileName=QFileDialog::getSaveFileName(this, tr("save file"), QDir::homePath(), tr("jpegfile(*.jpg)")); if(fileName.isEmpty()) { return; } const QPixmap* pixmap=ui->ImageCapture->pixmap(); if(pixmap) { pixmap->save(fileName); }}
【源码目录】.
└── untitled
├── main.cpp
├── mainwindow.cpp
├── mainwindow.h
├── mainwindow.ui
├── myview.cpp
├── myview.h
├── untitled.pro
├── untitled.pro.user
├── untitled.pro.user.22
└── untitled.pro.user.b66346c
1 directory, 10 files
评论