欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-10 17:05:20
1.qt的ui界面,找着画就好2.头文件直接贴出,之后有时间慢慢解释吧#pragma once #include #include "ui_camar...

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

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

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

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.运行效果

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

完整项目下载:qtwidgetsapplication2_jb51.rar

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