首页 > 编程语言 > VS2017+Qt5+Opencv3.4调用摄像头拍照并存储
2021
07-22

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

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

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

#pragma once
 
#include <QtWidgets/QWidget>
#include "ui_camaraGet.h"
 
#ifndef CAMARAGET_H
#define CAMARAGET_H
 
#include <opencv2\core\core.hpp>
#include <QWidget>
#include <QImage>
#include <QTimer>     // 设置采集数据的间隔时间
 
 
#include <QGraphicsScene>  
#include <QGraphicsView>  
 
#include <highgui/highgui_c.h>  //包含opencv库头文件
 
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>  //opencv申明
 
#include <opencv/cv.hpp>
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 <QtWidgets/QWidget>
#include "ui_camaraGet.h"
 
#ifndef CAMARAGET_H
#define CAMARAGET_H
 
#include <opencv2\core\core.hpp>
#include <QWidget>
#include <QImage>
#include <QTimer>     // 设置采集数据的间隔时间
#include "camaraGet.h"
#include<stdlib.h>
#include<random>
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 <QGraphicsScene>  
#include <QGraphicsView>  
 
#include <highgui/highgui_c.h>  //包含opencv库头文件
 
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>  //opencv申明
 
#include <opencv/cv.hpp>
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.运行效果

完整项目下载:QtWidgetsApplication2_jb51.rar

到此这篇关于VS2017+Qt5+Opencv3.4调用摄像头拍照并存储的文章就介绍到这了,更多相关Qt5 Opencv3.4拍照并存储内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!

编程技巧