这篇文章主要介绍了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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。