VS2017+Qt5+Opencv3.4调用摄像头拍照并存储

1. Qt的ui界面,找着画就好

VS2017+Qt5+Opencv3.4调用摄像头拍照并存储_第1张图片

VS2017+Qt5+Opencv3.4调用摄像头拍照并存储_第2张图片

2.头文件直接贴出,之后有时间慢慢解释吧

#pragma once

#include 
#include "ui_camaraGet.h"

#ifndef CAMARAGET_H
#define CAMARAGET_H

#include 
#include 
#include 
#include      // 设置采集数据的间隔时间


#include   
#include   

#include   //包含opencv库头文件

#include 
#include 
#include   //opencv申明

#include 
using namespace cv;

namespace Ui {
	class camaraGet;
}

class camaraGet : public QWidget
{
	Q_OBJECT

public:
	explicit camaraGet(QWidget *parent = 0);
	~camaraGet();

private slots:
	void openCamara();      // 打开摄像头
	void getFrame();       // 读取当前帧信息
	void closeCamara();     // 关闭摄像头。
	void takingPictures();  // 拍照

private:
	Ui::camaraGet ui;
	QTimer    *timer;
	QImage    *imag;
	CvCapture *cam;// 视频获取结构, 用来作为视频获取函数的一个参数
	IplImage  *frame;
	VideoCapture capture1;
	Mat showimage;
	QImage Mat2Qimage(Mat cvImg);

//	camaraGet(QWidget * parent);
	//申请IplImage类型指针,就是申请内存空间来存放每一帧图像
};

#endif // CAMARAGET_H

3.源文件

#pragma once

#include 
#include "ui_camaraGet.h"

#ifndef CAMARAGET_H
#define CAMARAGET_H

#include 
#include 
#include 
#include      // 设置采集数据的间隔时间
#include "camaraGet.h"
#include
#include
using namespace cv;
using namespace std;





camaraGet::camaraGet(QWidget *parent):
	QWidget(parent)
{
    ui.setupUi(this);
	connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(openCamara()));
	connect(ui.pushButton_2, SIGNAL(clicked()), this, SLOT(takingPictures()));
	connect(ui.pushButton_3, SIGNAL(clicked()), this, SLOT(closeCamara()));
	setWindowTitle(tr("Main Window"));

	timer = new QTimer(this);
	imag = new QImage();
	connect(timer, SIGNAL(timeout()), this, SLOT(getFrame()));//超时就读取当前摄像头信息
}
camaraGet::~camaraGet()
{

}

void camaraGet::openCamara()
{
	capture1.open(1);                                            //打开摄像头,从摄像头中获取视频
	timer->start(10);
	
}

void camaraGet::getFrame() {
	capture1 >> showimage;
	QImage imag = Mat2Qimage(showimage);

	ui.label_2->setScaledContents(true);
	ui.label_2->setPixmap(QPixmap::fromImage(imag));
}

void camaraGet::closeCamara()
{
	timer->stop();
	ui.label->clear();
	capture1.release();
}

string strRand(int length) {			// length: 产生字符串的长度
	char tmp;							// tmp: 暂存一个随机数
	string buffer;						// buffer: 保存返回值

	
	random_device rd;					// 产生一个 std::random_device 对象 rd
	default_random_engine random(rd());	// 用 rd 初始化一个随机数发生器 random

	for (int i = 0; i < length; i++) {
		tmp = random() % 36;	
		if (tmp < 10) {			
			tmp += '0';
		}
		else {				
			tmp -= 10;
			tmp += 'A';
		}
		buffer += tmp;
	}
	return buffer;
}


void camaraGet::takingPictures()
{
	capture1.open(1);
	capture1 >> showimage;
	QImage img = Mat2Qimage(showimage);
	ui.label->setScaledContents(true);
	ui.label->setPixmap(QPixmap::fromImage(img));

	string writePath = "../tempPhoto/";
	string name;
	int i = 0;
	name = writePath + strRand(4) + ".jpg";
	imwrite(name, showimage);
	i++;

}


QImage camaraGet::Mat2Qimage(Mat cvImg)
{
	// 8-bits unsigned, NO. OF CHANNELS = 1
	if (cvImg.type() == CV_8UC1)
	{
		QImage image(cvImg.cols, cvImg.rows, QImage::Format_Indexed8);
		// Set the color table (used to translate colour indexes to qRgb values)
		image.setColorCount(256);
		for (int i = 0; i < 256; i++)
		{
			image.setColor(i, qRgb(i, i, i));
		}
		// Copy input Mat
		uchar *pSrc = cvImg.data;
		for (int row = 0; row < cvImg.rows; row++)
		{
			uchar *pDest = image.scanLine(row);
			memcpy(pDest, pSrc, cvImg.cols);
			pSrc += cvImg.step;
		}
		return image;
	}
	// 8-bits unsigned, NO. OF CHANNELS = 3
	else if (cvImg.type() == CV_8UC3)
	{
		// Copy input Mat
		const uchar *pSrc = (const uchar*)cvImg.data;
		// Create QImage with same dimensions as input Mat
		QImage image(pSrc, cvImg.cols, cvImg.rows, cvImg.step, QImage::Format_RGB888);
		return image.rgbSwapped();
	}
	else if (cvImg.type() == CV_8UC4)
	{
//		qDebug() << "CV_8UC4";
		// Copy input Mat
		const uchar *pSrc = (const uchar*)cvImg.data;
		// Create QImage with same dimensions as input Mat
		QImage image(pSrc, cvImg.cols, cvImg.rows, cvImg.step, QImage::Format_ARGB32);
		return image.copy();
	}
	else
	{
//		qDebug() << "ERROR: Mat could not be converted to QImage.";
		return QImage();
	}
}


#include   
#include   

#include   //包含opencv库头文件

#include 
#include 
#include   //opencv申明

#include 
using namespace cv;

namespace Ui {
	class camaraGet;
}

class camaraGet : public QWidget
{
	Q_OBJECT

public:
	explicit camaraGet(QWidget *parent = 0);
	~camaraGet();

private slots:
	void openCamara();      // 打开摄像头
	void getFrame();       // 读取当前帧信息
	void closeCamara();     // 关闭摄像头。
	void takingPictures();  // 拍照

private:
	Ui::camaraGet ui;
	QTimer    *timer;
	QImage    *imag;
	CvCapture *cam;// 视频获取结构, 用来作为视频获取函数的一个参数
	IplImage  *frame;
	VideoCapture capture1;
	Mat showimage;
	QImage Mat2Qimage(Mat cvImg);

//	camaraGet(QWidget * parent);
	//申请IplImage类型指针,就是申请内存空间来存放每一帧图像
};

#endif // CAMARAGET_H

4.运行效果

VS2017+Qt5+Opencv3.4调用摄像头拍照并存储_第3张图片

完整项目下载:

你可能感兴趣的:(opencv,计算机视觉,qt)