您好,登錄后才能下訂單哦!
這篇文章主要介紹了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.運行效果
感謝你能夠認真閱讀完這篇文章,希望小編分享的“VS2017+Qt5+Opencv3.4如何調用攝像頭拍照并存儲”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。