qt项目:大恒相机实时采集,单帧采集,并且在屏幕上显示,含有保存bmp图片功能
ref:
https://blog.csdn.net/qq_35299223/article/details/103754599?utm_medium=distribute.pc_relevant_download.none-task-blog-2~default~searchFromBaidu~default-10.test_version_3&depth_1-utm_source=distribute.pc_relevant_download.none-task-blog-2~default~searchFromBaidu~default-10.test_version_
软件框架
ui界面框架
.pro文件
#------------------------------------------------- # # Project created by QtCreator 2019-12-20T09:41:39 # #------------------------------------------------- QT += core gui QT+=charts greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport TARGET = RailMeasurement TEMPLATE = app # The following define makes your compiler emit warnings if you use # any feature of Qt which as been marked as deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ main.cpp \ mainwindow.cpp \ qcustomplot.cpp \ camerawindow.cpp \ mat2qtformatmethod.cpp \ cgxbitmap.cpp \ convertstring.cpp \ drawwidget.cpp HEADERS += \ mainwindow.h \ qcustomplot.h \ camerawindow.h \ mat2qtformatmethod.h \ cgxbitmap.h \ convertstring.h \ drawwidget.h FORMS += \ mainwindow.ui \ camerawindow.ui # opencv环境,添加了imgproc,core,highui库 win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../cvpack/package/opencv/build/x64/vc14/lib/ -lopencv_imgproc2413 else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../cvpack/package/opencv/build/x64/vc14/lib/ -lopencv_imgproc2413d else:unix: LIBS += -L$$PWD/../../cvpack/package/opencv/build/x64/vc14/lib/ -lopencv_imgproc2413 INCLUDEPATH += $$PWD/../../cvpack/package/opencv/build/include DEPENDPATH += $$PWD/../../cvpack/package/opencv/build/include win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../cvpack/package/opencv/build/x64/vc14/lib/ -lopencv_core2413 else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../cvpack/package/opencv/build/x64/vc14/lib/ -lopencv_core2413d else:unix: LIBS += -L$$PWD/../../cvpack/package/opencv/build/x64/vc14/lib/ -lopencv_core2413 INCLUDEPATH += $$PWD/../../cvpack/package/opencv/build/include DEPENDPATH += $$PWD/../../cvpack/package/opencv/build/include win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../cvpack/package/opencv/build/x64/vc14/lib/ -lopencv_highgui2413 else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../cvpack/package/opencv/build/x64/vc14/lib/ -lopencv_highgui2413d else:unix: LIBS += -L$$PWD/../../cvpack/package/opencv/build/x64/vc14/lib/ -lopencv_highgui2413 INCLUDEPATH += $$PWD/../../cvpack/package/opencv/build/include DEPENDPATH += $$PWD/../../cvpack/package/opencv/build/include # 大恒sdk环境,添加了C++库和包含路径,没有添加c程序的库 unix|win32: LIBS += -L$$PWD/'../../CameraDemo/C++ SDK/lib/x64/' -lGxIAPICPPEx INCLUDEPATH += $$PWD/'../../CameraDemo/C++ SDK/lib/x64' DEPENDPATH += $$PWD/'../../CameraDemo/C++ SDK/lib/x64' INCLUDEPATH += $$PWD/'../../CameraDemo/C++ SDK/inc' DEPENDPATH += $$PWD/'../../CameraDemo/C++ SDK/inc' CONFIG +=C++11
camerawindow.h
1 #ifndef CAMERAWINDOW_H 2 #define CAMERAWINDOW_H 3 #include "GalaxyIncludes.h" 4 #include <QDebug> 5 #include <opencv2/core.hpp> 6 #include <opencv2/highgui.hpp> 7 #include <opencv2/imgproc.hpp> 8 #include "mat2qtformatmethod.h" 9 #include <QPixmap> 10 #include <QImage> 11 #include <QLabel> 12 #include <QMessageBox> 13 #include <string> 14 #include "cgxbitmap.h" 15 #include "convertstring.h" 16 #include <time.h> 17 #include <QPainter> 18 #include "drawwidget.h" 19 20 21 struct CameraWindowGeoParams{ 22 int WidgetWidth; 23 int WidgetHeight; 24 int LabelWidth; 25 int LabelHeight; 26 int LineeditWidth; 27 int LineeditHeight; 28 int ButtonWidth; 29 int ButtonHeight; 30 }; 31 struct MyUserParam{ 32 cv::Mat CurImgMat; 33 QLabel* LabelShowImgPtr; 34 }; 35 36 37 38 #include <QWidget> 39 40 namespace Ui { 41 class CameraWindow; 42 struct CameraWindowGeoParams; 43 //class CSampleCaptureEventHandler; 44 } 45 46 class CameraWindow : public QWidget 47 { 48 Q_OBJECT 49 50 private: 51 //回调采集事件类 52 class CSampleCaptureEventHandler : public ICaptureEventHandler 53 { 54 public: 55 56 void DoOnImageCaptured(CImageDataPointer& objImageDataPointer, void* pUserParam) 57 { 58 59 if (objImageDataPointer->GetStatus() == GX_FRAME_STATUS_SUCCESS) 60 { 61 CameraWindow* CamUiPtr=(CameraWindow*)pUserParam; 62 //以下为连续采集部分的显示 63 void* pRaw8Buffer = NULL; 64 pRaw8Buffer = objImageDataPointer->ConvertToRaw8(GX_BIT_0_7); 65 std::memcpy(CamUiPtr->CurImgMat.data, pRaw8Buffer, (objImageDataPointer->GetHeight())*(objImageDataPointer->GetWidth())); 66 //cv::flip(CamUiPtr->CurImgMat, CamUiPtr->CurImgMat, 0);//大恒的图像要进行翻转,但是此处似乎不需要翻转 67 68 //调用自定义绘图函数进行绘制实时采集图像 69 CamUiPtr->ShowCurImgInLabel(CamUiPtr->LabelShowCurImg,CamUiPtr->CurImgMat); 70 71 //重新调整图像大小以适应窗口,并且此处进行了数据更新,这样才能使用update()函数 72 //使用drawimage进行绘图的代码 73 //先得到一张完整的qimage,然后根据窗口进行修改qimage的大小,最后再进行更新。更行时需要对窗口进行提升,使用自定义窗口。 74 //使用update进行画图有更好的连续性效果 75 //CamUiPtr->CurImgQimg=CV2QTFORMAT::cvMatToQImage(CamUiPtr->CurImgMat); 76 //CamUiPtr->pDrawWidget->ReSizeImg(CamUiPtr->CurImgQimg); 77 //CamUiPtr->pDrawWidget->update(); 78 79 //单帧采集 80 if(CamUiPtr->m_bCheckSaveBmp==true){ 81 MakeMyDirectory(CamUiPtr->strFilePath);//创建文件夹 82 std::string PathAndName=CamUiPtr->strFilePath+"\\"+CamUiPtr->strFileName; 83 CamUiPtr->m_pBitmap->SaveBmp(objImageDataPointer,PathAndName);//保存单帧图像 84 CamUiPtr->m_bCheckSaveBmp=false; 85 86 //通过读取的方式画出单帧图像 87 QString LoadImgName=QString::fromStdString(PathAndName); 88 CamUiPtr->SingleImgPixMap.load(LoadImgName,nullptr,Qt::AutoColor); 89 CamUiPtr->LabelSingleImg->setPixmap(CamUiPtr->SingleImgPixMap); 90 91 //仅仅在激发采集的时候是有意义的 92 CamUiPtr->TimeTakePhoto=clock(); 93 qDebug()<<(double)(CamUiPtr->TimeTakePhoto-CamUiPtr->TimePushButton); 94 CamUiPtr->TimePushButton=0; 95 CamUiPtr->TimeTakePhoto=0; 96 } 97 } 98 } 99 }; 100 101 102 public: 103 explicit CameraWindow(QWidget *parent = 0); 104 ~CameraWindow(); 105 106 void CamWinParamsSet(int _WidgetW, int _WidgetH, int _SmallPartW, int _SmallPartH); 107 108 private slots: 109 void on_pushButton_ListDevice_clicked(); 110 111 void on_pushButton_OpenCam_clicked(); 112 113 void on_pushButton_CloseCam_clicked(); 114 115 void on_pushButton_StartGrab_clicked(); 116 117 void on_pushButton_StopGrab_clicked(); 118 119 void on_pushButton_GetImg_clicked(); 120 121 void on_pushButton_ParamsSet_clicked(); 122 123 private: 124 Ui::CameraWindow *ui; 125 CameraWindowGeoParams CwParas; 126 //相机库 127 GxIAPICPP::gxdeviceinfo_vector vectorDeviceInfo;//枚举设备结果 128 CGXDevicePointer ObjDevicePtr;//当前界面操作的相机指针 129 CGXStreamPointer ObjStreamPtr;//流采集指针 130 CGXFeatureControlPointer ObjFeatureControlPtr;//远端设备控制器 131 ICaptureEventHandler* pCaptureEventHandler;//注册回调事件指针 132 133 //Opencv参数 134 cv::Mat CurImgMat;//实时窗口的Mat 135 void AllocateRoomForMatCurImgPtr();//为实时采集窗口指针分配空间 136 137 //这里使用string,然后使用自定义函数进行转换,放弃了MFC的CString类 138 std::string strFilePath;//保存路径 139 std::string strFileName;//文件名称 140 141 // 保存图像为BMP格式 142 CGXBitmap* m_pBitmap;// 保存图像指针 143 bool m_bCheckSaveBmp;// 是否保存图片 144 int ImgWidth;//记录图片的宽度 145 int ImgHeight;//记录图片的高度 146 147 //展示窗口指针 148 QLabel* LabelShowCurImg; 149 QLabel* LabelSingleImg; 150 QPixmap SingleImgPixMap; 151 QPixmap CurImgPixMap;//画一个当前的图 152 DrawWidget* pDrawWidget;//提升窗口指针,使用drawqimage显示实时图像 153 154 //使用Painter事件来绘图drawQimg 155 QImage CurImgQimg; 156 QSize LableSize; 157 158 //计时函数 159 clock_t TimePushButton; 160 clock_t TimeTakePhoto; 161 double DelayTime;//用于后期设定延时时间 162 163 //窗口几何参数设计 164 void GeoSetWidgetCurInput(int _x, int _y, int _width, int _height);//设置实时采集框几何尺寸 165 void GeoSetWidgetSampleImg(int _x, int _y, int _width, int _height);//设置单帧采集几何尺寸 166 void GeoSetFrameParamsSet(int _x, int _y, int _width, int _height, int _dx, int _dy);//设置参数框几何尺寸 167 void GeoSetFrameCamOperate();//设置操作框几何尺寸 168 void GeoSetFrameWinInfo();//设置抬头框几何尺寸 169 170 //绘制实时采集图像成比例 171 void ShowCurImgInLabel(QLabel* ptrLabelToShow,cv::Mat& CVMat); 172 }; 173 174 #endif // CAMERAWINDOW_H
cgxbitmap.h
1 #ifndef CGXBITMAP_H 2 #define CGXBITMAP_H 3 4 //------------------------------------------------------------------------ 5 /* 6 \file GXBitmap.h 7 \brief 此类主要用于图像的显示和存储,图像显示和存储可以自适应黑白彩色相机, 8 图像存储可以存储为Bmp、Raw,对图像显示和存储进行了声明 9 */ 10 //------------------------------------------------------------------------ 11 #include "GalaxyIncludes.h" 12 #include "convertstring.h" 13 class CGXBitmap 14 { 15 public: 16 //构造函数 17 CGXBitmap(CGXDevicePointer& objCGXDevicePointer); 18 19 //析构函数 20 ~CGXBitmap(void); 21 22 //显示图像 23 void Show(CImageDataPointer& objCImageDataPointer); 24 25 //显示图像及帧率 26 void Show(CImageDataPointer& objCImageDataPointer,char* strDeviceSNFPS); 27 28 //图像处理后并显示图像 29 void ShowImageProcess(CImageProcessConfigPointer& objCfg,CImageDataPointer& objCImageDataPointer); 30 31 // 存储Bmp图像 32 void SaveBmp(CImageDataPointer& objCImageDataPointer,const std::string& strFilePath); 33 34 // 存储Raw图像 35 void SaveRaw(CImageDataPointer& objCImageDataPointer,const std::string& strFilePath); 36 37 //通过GX_PIXEL_FORMAT_ENTRY获取最优Bit位 38 GX_VALID_BIT_LIST GetBestValudBit(GX_PIXEL_FORMAT_ENTRY emPixelFormatEntry); 39 private: 40 //判断PixelFormat是否为8位 41 bool __IsPixelFormat8(GX_PIXEL_FORMAT_ENTRY emPixelFormatEntry); 42 43 //为彩色相机图像显示准备资源 44 void __ColorPrepareForShowImg(); 45 46 //为黑白相机图像显示准备资源 47 void __MonoPrepareForShowImg(); 48 49 //判断是否兼容 50 bool __IsCompatible(BITMAPINFO *pBmpInfo, uint64_t nWidth, uint64_t nHeight); 51 52 //更新Bitmap的信息 53 void __UpdateBitmap(CImageDataPointer& objCImageDataPointer); 54 55 //将m_pBufferRGB中图像显示到界面 56 void __DrawImg(BYTE* pBuffer); 57 58 //将m_pBufferRGB中图像和帧率显示到界面 59 void __DrawImg(BYTE* pBuffer, char* strDeviceSNFPS); 60 61 //计算宽度所占的字节数 62 int64_t __GetStride(int64_t nWidth, bool bIsColor); 63 64 65 private: 66 //CameraWindow* pCamw; //<显示图像窗口(控件)指针 67 bool m_bIsColor ; //<是否支持彩色相机 68 int64_t m_nImageHeight; //<原始图像高 69 int64_t m_nImageWidth; //<原始图像宽 70 BITMAPINFO *m_pBmpInfo; //<BITMAPINFO 结构指针,显示图像时使用 71 char m_chBmpBuf[2048]; //<BIMTAPINFO 存储缓冲区,m_pBmpInfo即指向此缓冲区 72 //这里的这个mfc绘图句柄删去 73 //HDC m_hDC; //<绘制图像DC句柄 74 BYTE *m_pImageBuffer; //<保存翻转后的图像用于显示 75 private: 76 CGXBitmap& operator=(const CGXBitmap&); 77 CGXBitmap(const CGXBitmap&); 78 }; 79 80 #endif // CGXBITMAP_H
.......
qcustomplot.cpp
文件下载
链接:https://pan.baidu.com/s/1RqvkmACFslQxbHzkkihV_g
提取码:o09o