飞道的博客

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

308人阅读  评论(0)

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

2.头文件直接贴出,之后有时间慢慢解释吧


  
  1. #pragma once
  2. #include <QtWidgets/QWidget>
  3. #include "ui_camaraGet.h"
  4. #ifndef CAMARAGET_H
  5. #define CAMARAGET_H
  6. #include <opencv2\core\core.hpp>
  7. #include <QWidget>
  8. #include <QImage>
  9. #include <QTimer> // 设置采集数据的间隔时间
  10. #include <QGraphicsScene>
  11. #include <QGraphicsView>
  12. #include <highgui/highgui_c.h> //包含opencv库头文件
  13. #include <opencv2\imgproc\imgproc.hpp>
  14. #include <opencv2\core\core.hpp>
  15. #include <opencv2\highgui\highgui.hpp> //opencv申明
  16. #include <opencv/cv.hpp>
  17. using namespace cv;
  18. namespace Ui {
  19. class camaraGet;
  20. }
  21. class camaraGet : public QWidget
  22. {
  23. Q_OBJECT
  24. public:
  25. explicit camaraGet(QWidget *parent = 0);
  26. ~camaraGet();
  27. private slots:
  28. void openCamara(); // 打开摄像头
  29. void getFrame(); // 读取当前帧信息
  30. void closeCamara(); // 关闭摄像头。
  31. void takingPictures(); // 拍照
  32. private:
  33. Ui::camaraGet ui;
  34. QTimer *timer;
  35. QImage *imag;
  36. CvCapture *cam; // 视频获取结构, 用来作为视频获取函数的一个参数
  37. IplImage *frame;
  38. VideoCapture capture1;
  39. Mat showimage;
  40. QImage Mat2Qimage(Mat cvImg);
  41. // camaraGet(QWidget * parent);
  42. //申请IplImage类型指针,就是申请内存空间来存放每一帧图像
  43. };
  44. #endif // CAMARAGET_H

3.源文件


  
  1. #pragma once
  2. #include <QtWidgets/QWidget>
  3. #include "ui_camaraGet.h"
  4. #ifndef CAMARAGET_H
  5. #define CAMARAGET_H
  6. #include <opencv2\core\core.hpp>
  7. #include <QWidget>
  8. #include <QImage>
  9. #include <QTimer> // 设置采集数据的间隔时间
  10. #include "camaraGet.h"
  11. #include<stdlib.h>
  12. #include<random>
  13. using namespace cv;
  14. using namespace std;
  15. camaraGet::camaraGet(QWidget *parent):
  16. QWidget(parent)
  17. {
  18. ui.setupUi( this);
  19. connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(openCamara()));
  20. connect(ui.pushButton_2, SIGNAL(clicked()), this, SLOT(takingPictures()));
  21. connect(ui.pushButton_3, SIGNAL(clicked()), this, SLOT(closeCamara()));
  22. setWindowTitle(tr( "Main Window"));
  23. timer = new QTimer( this);
  24. imag = new QImage();
  25. connect(timer, SIGNAL(timeout()), this, SLOT(getFrame())); //超时就读取当前摄像头信息
  26. }
  27. camaraGet::~camaraGet()
  28. {
  29. }
  30. void camaraGet::openCamara()
  31. {
  32. capture1.open( 1); //打开摄像头,从摄像头中获取视频
  33. timer->start( 10);
  34. }
  35. void camaraGet::getFrame() {
  36. capture1 >> showimage;
  37. QImage imag = Mat2Qimage(showimage);
  38. ui.label_2->setScaledContents( true);
  39. ui.label_2->setPixmap(QPixmap::fromImage(imag));
  40. }
  41. void camaraGet::closeCamara()
  42. {
  43. timer->stop();
  44. ui.label->clear();
  45. capture1.release();
  46. }
  47. string strRand(int length) { // length: 产生字符串的长度
  48. char tmp; // tmp: 暂存一个随机数
  49. string buffer; // buffer: 保存返回值
  50. random_device rd; // 产生一个 std::random_device 对象 rd
  51. default_random_engine random(rd()); // 用 rd 初始化一个随机数发生器 random
  52. for ( int i = 0; i < length; i++) {
  53. tmp = random() % 36;
  54. if (tmp < 10) {
  55. tmp += '0';
  56. }
  57. else {
  58. tmp -= 10;
  59. tmp += 'A';
  60. }
  61. buffer += tmp;
  62. }
  63. return buffer;
  64. }
  65. void camaraGet::takingPictures()
  66. {
  67. capture1.open( 1);
  68. capture1 >> showimage;
  69. QImage img = Mat2Qimage(showimage);
  70. ui.label->setScaledContents( true);
  71. ui.label->setPixmap(QPixmap::fromImage(img));
  72. string writePath = "../tempPhoto/";
  73. string name;
  74. int i = 0;
  75. name = writePath + strRand( 4) + ".jpg";
  76. imwrite(name, showimage);
  77. i++;
  78. }
  79. QImage camaraGet::Mat2Qimage(Mat cvImg)
  80. {
  81. // 8-bits unsigned, NO. OF CHANNELS = 1
  82. if (cvImg.type() == CV_8UC1)
  83. {
  84. QImage image(cvImg.cols, cvImg.rows, QImage::Format_Indexed8);
  85. // Set the color table (used to translate colour indexes to qRgb values)
  86. image.setColorCount( 256);
  87. for ( int i = 0; i < 256; i++)
  88. {
  89. image.setColor(i, qRgb(i, i, i));
  90. }
  91. // Copy input Mat
  92. uchar *pSrc = cvImg.data;
  93. for ( int row = 0; row < cvImg.rows; row++)
  94. {
  95. uchar *pDest = image.scanLine(row);
  96. memcpy(pDest, pSrc, cvImg.cols);
  97. pSrc += cvImg.step;
  98. }
  99. return image;
  100. }
  101. // 8-bits unsigned, NO. OF CHANNELS = 3
  102. else if (cvImg.type() == CV_8UC3)
  103. {
  104. // Copy input Mat
  105. const uchar *pSrc = ( const uchar*)cvImg.data;
  106. // Create QImage with same dimensions as input Mat
  107. QImage image(pSrc, cvImg.cols, cvImg.rows, cvImg.step, QImage::Format_RGB888);
  108. return image.rgbSwapped();
  109. }
  110. else if (cvImg.type() == CV_8UC4)
  111. {
  112. // qDebug() << "CV_8UC4";
  113. // Copy input Mat
  114. const uchar *pSrc = ( const uchar*)cvImg.data;
  115. // Create QImage with same dimensions as input Mat
  116. QImage image(pSrc, cvImg.cols, cvImg.rows, cvImg.step, QImage::Format_ARGB32);
  117. return image.copy();
  118. }
  119. else
  120. {
  121. // qDebug() << "ERROR: Mat could not be converted to QImage.";
  122. return QImage();
  123. }
  124. }
  125. #include <QGraphicsScene>
  126. #include <QGraphicsView>
  127. #include <highgui/highgui_c.h> //包含opencv库头文件
  128. #include <opencv2\imgproc\imgproc.hpp>
  129. #include <opencv2\core\core.hpp>
  130. #include <opencv2\highgui\highgui.hpp> //opencv申明
  131. #include <opencv/cv.hpp>
  132. using namespace cv;
  133. namespace Ui {
  134. class camaraGet;
  135. }
  136. class camaraGet : public QWidget
  137. {
  138. Q_OBJECT
  139. public:
  140. explicit camaraGet(QWidget *parent = 0);
  141. ~camaraGet();
  142. private slots:
  143. void openCamara(); // 打开摄像头
  144. void getFrame(); // 读取当前帧信息
  145. void closeCamara(); // 关闭摄像头。
  146. void takingPictures(); // 拍照
  147. private:
  148. Ui::camaraGet ui;
  149. QTimer *timer;
  150. QImage *imag;
  151. CvCapture *cam; // 视频获取结构, 用来作为视频获取函数的一个参数
  152. IplImage *frame;
  153. VideoCapture capture1;
  154. Mat showimage;
  155. QImage Mat2Qimage(Mat cvImg);
  156. // camaraGet(QWidget * parent);
  157. //申请IplImage类型指针,就是申请内存空间来存放每一帧图像
  158. };
  159. #endif // CAMARAGET_H

4.运行效果

完整项目下载:


转载:https://blog.csdn.net/zuoyou_yi/article/details/117338900
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场