亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

VS2017+Qt5+Opencv3.4調用攝像頭拍照并存儲

 更新時間:2021年05月30日 10:13:17   作者:zuoyou_yi  
本文主要介紹了VS2017+Qt5+Opencv3.4調用攝像頭拍照并存儲,實現(xiàn)了視頻,拍照,保存這三個功能。具有一定的參考價值,感興趣的小伙伴們可以參考一下

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>     // 設置采集數(shù)據(jù)的間隔時間
 
 
#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;// 視頻獲取結構, 用來作為視頻獲取函數(shù)的一個參數(shù)
	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>     // 設置采集數(shù)據(jù)的間隔時間
#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: 暫存一個隨機數(shù)
	string buffer;						// buffer: 保存返回值
 
	
	random_device rd;					// 產生一個 std::random_device 對象 rd
	default_random_engine random(rd());	// 用 rd 初始化一個隨機數(shù)發(fā)生器 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;// 視頻獲取結構, 用來作為視頻獲取函數(shù)的一個參數(shù)
	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拍照并存儲內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:

相關文章

  • C語言用封裝方法實現(xiàn)飛機大戰(zhàn)游戲

    C語言用封裝方法實現(xiàn)飛機大戰(zhàn)游戲

    這篇文章主要為大家詳細介紹了C語言用封裝方法實現(xiàn)飛機大戰(zhàn)游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • 通過實例詳解C語言函數(shù)返回值

    通過實例詳解C語言函數(shù)返回值

    函數(shù)的返回值是指函數(shù)被調用之后,執(zhí)行函數(shù)體中的程序段所取得的并返回給主調函數(shù)的值,下面這篇文章主要給大家介紹了關于C語言函數(shù)返回值的相關資料,需要的朋友可以參考下
    2022-01-01
  • QT編寫tcp通信工具(Client篇)

    QT編寫tcp通信工具(Client篇)

    這篇文章主要介紹了QT編寫tcp通信工具,適用于Client端,類似網上常見的網絡調試工具,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • C語言菜鳥基礎教程之Hello World

    C語言菜鳥基礎教程之Hello World

    C語言是一門通用計算機編程語言,應用廣泛。C語言的設計目標是提供一種能以簡易的方式編譯、處理低級存儲器、產生少量的機器碼以及不需要任何運行環(huán)境支持便能運行的編程語言。
    2017-10-10
  • C++ 構造雙向鏈表的實現(xiàn)代碼

    C++ 構造雙向鏈表的實現(xiàn)代碼

    本篇文章是對C++中構造雙向鏈表的實現(xiàn)代碼進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • C++類成員函數(shù)中的名字查找問題

    C++類成員函數(shù)中的名字查找問題

    這篇文章主要介紹了C++類成員函數(shù)中的名字查找問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • C++?pimpl機制詳細講解

    C++?pimpl機制詳細講解

    PIMPL?是?C++?中的一個編程技巧,意思為指向實現(xiàn)的指針。具體操作是把類的實現(xiàn)細節(jié)放到一個單獨的類中,并用一個指針進行訪問
    2022-08-08
  • C語言設計前中后隊列實例代碼

    C語言設計前中后隊列實例代碼

    隊列最主要的作用就是用來管理數(shù)據(jù)流的,防止數(shù)據(jù)因為傳輸頻率過快得不到及時處理而丟失,下面這篇文章主要給大家介紹了關于C語言設計前中后隊列的相關資料,需要的朋友可以參考下
    2021-12-12
  • C語言之選擇分支語句詳解

    C語言之選擇分支語句詳解

    大家好,本篇文章主要講的是C語言之選擇分支語句詳解,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • C++ typeid 和虛函數(shù)詳解

    C++ typeid 和虛函數(shù)詳解

    這篇文章主要介紹了c++ typeid 和虛函數(shù)的使用,幫助大家更好的理解和使用c++,感興趣的朋友可以了解下,希望能夠給你帶來幫助
    2021-09-09

最新評論