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拍照並存儲內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- OpenCV+Qt實現圖像處理操作工具的示例代碼
- OpenCV+Qt實現圖像處理操作
- VisualStudio2019配置OpenCV的詳細過程
- 基於Qt OpenCV的圖像灰度化像素操作詳解
- OpenCV實現圖像距離變換