*.pro 文件加入:
QT += core gui multimediamulti mediawidgets
------------------------------------------------------------------------
mainwindow.h 檔案:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QCameraImageCapture>
#include <QCameraViewfinder>
#include <QCamera>
#include <QTimer>
#include <QImage>
#include <QDebug>
#include <QFile>
namespace Ui{
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget*parent=0);
~MainWindow();
private:
Ui::MainWindow*ui;
QCameraImageCapture*ic;
privateslots:
void displayImage(intid,QImageimage);
void videoFrameCapture();
};
#endif //MAINWINDOW_H
------------------------------------------------------------------------
mainwindow.cpp 檔案:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget*parent):
QMainWindow(parent),
ui(new Ui::MainWindow){
ui->setupUi(this);
QCamera*c = new QCamera;
ic = new QCameraImageCapture(c);
QCameraViewfinder*vf = new QCameraViewfinder();
QTimer * t = new QTimer(this);
connect(t,SIGNAL(timeout()),
this,SLOT(videoFrameCapture()));
connect(ic,SIGNAL(imageCaptured(int,QImage))
,this,SLOT(displayImage(int,QImage)));
//設定視訊參數
c->setCaptureMode(QCamera::CaptureViewfinder);
c->setViewfinder(vf);
c->start();//開始擷取畫面
t->start(33);//每33毫秒觸發displayImage()
}
//擷取畫面
void MainWindow::videoFrameCapture(){
ic->capture();
}
//寫到Label中
void MainWindow::displayImage(int id, QImage image){
ui->imgLabel->setPixmap(QPixmap::fromImage(image));
////保存圖片
//QFilefile("C:/Test/yourFile.png");
//file.open(QIODevice::WriteOnly);
//ui->imgLabel->pixmap()->save(&file,"PNG");
}
MainWindow::~MainWindow(){
delete ui;
}
全站熱搜
留言列表