(1)vs下新建一个qt工程,打开ui文件
(2)布局ui文件,改变名称,对象名称,保存(只用一个PushButton)
(3)工程目录下新建一个文件夹取名Image
(4)Takephoto.h中代码为
#ifndef TAKEPHOTO_H
#define TAKEPHOTO_H
#include <QtWidgets/QMainWindow>
#include "ui_takephoto.h"
#include<opencv2/opencv.hpp>
using namespace cv;
class takephoto : public QMainWindow
{
Q_OBJECT
public:
takephoto(QWidget *parent = 0);
~takephoto();
private:
Ui::takephotoClass ui;
VideoCapture cap;
Mat frame;
private slots:
void TakePhotoSlot();
};
#endif // TAKEPHOTO_H
(5)Takephoto.cpp中代码为
#include "takephoto.h"
takephoto::takephoto(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
connect(ui.TakePhotoButton,SIGNAL(clicked()),this,SLOT(TakePhotoSlot()));//clicked与TakePhotoSlot后面的括号不能忘了
}
takephoto::~takephoto()
{
}
void takephoto::TakePhotoSlot()
{
cap.open(0);//打开摄像头
int i = 1;
while (i != 11)//拍摄10张照片
{
cap >> frame;
imshow("frame", frame);
string filename = format("Image\\%d.jpg", i);
char key = waitKey(10);//值越大视频越卡,为0出错
switch (key)
{
case 'p'://按键盘上的'p'拍摄
i++;
imwrite(filename, frame);
imshow("takephoto", frame);
waitKey(1000);
destroyWindow("takephoto");
break;
default:
break;
}
}
}
(6)F5运行,按下拍照按钮,打开摄像头
(7)按下10次Image文件中拍摄完10张图片
本文介绍了如何在Windows 10环境下,结合Qt和Visual Studio 2013来创建一个连续拍照的应用,并将照片存储到指定文件夹。通过设置UI,创建Image文件夹,编写Takephoto.h和Takephoto.cpp代码,实现了按下按钮后开启摄像头并连续拍摄10张图片的功能。

1359

被折叠的 条评论
为什么被折叠?



