基于QT的海康威视的相机二次开发

与上一篇不同的是,本篇注重于实现相机的一键开启,注重相机采集过程中调整曝光量和伽马矫正等。(代码来自短学期实践时五位老师提供的,上传以供大家学习借鉴)

代码资源:

链接:https://pan.baidu.com/s/19oeR5lgsF-FjJEd0EKpFrw?pwd=lvu4 
提取码:lvu4

环境配置:

opencv开源库,Matlab,vs2017,qt以及海康相机配套的mvs软件,这是各个软件的安装包以及安装教程(先装vs2017,再安装qt)

mvcamera.h:

#ifndef MVCAMERA_H
#define MVCAMERA_H

#include 
#include "string.h"
#include "MvCameraControl.h"

#ifndef MV_NULL
#define MV_NULL    0
#endif

class MvCamera
{
public:
    MvCamera();
    ~MvCamera();

    // ch:枚举设备 | en:Enumerate Device
    static int EnumDevices(unsigned int nTLayerType, MV_CC_DEVICE_INFO_LIST* pstDevList);


    /// ch:打开设备 | en:Open Device
    int Open(MV_CC_DEVICE_INFO* pstDeviceInfo);

    /// ch:关闭设备 | en:Close Device
    int Close();

    /// ch:注册图像数据回调 | en:Register Image Data CallBack
    int RegisterImageCallBack(void(__stdcall* cbOutput)(unsigned char * pData, MV_FRAME_OUT_INFO_EX* pFrameInfo, void* pUser), void* pUser);

    /// ch:开启抓图 | en:Start Grabbing
    int StartGrabbing();

    /// ch:停止抓图 | en:Stop Grabbing
    int StopGrabbing();

    /// ch:主动获取一帧图像数据 | en:Get one frame initiatively
    int GetImageBuffer(MV_FRAME_OUT* pFrame, int nMsec);

    /// ch:释放图像缓存 | en:Free image buffer
    int FreeImageBuffer(MV_FRAME_OUT* pFrame);

    /// ch:显示一帧图像 | en:Display one frame image
    int DisplayOneFrame(MV_DISPLAY_FRAME_INFO* pDisplayInfo);

    /// ch:获取设备信息 | en:Get device information
    int GetDeviceInfo(MV_CC_DEVICE_INFO* pstDevInfo);

    // ch:获取和设置Enum型参数,如 PixelFormat,详细内容参考SDK安装目录下的 MvCameraNode.xlsx 文件
    // en:Get Enum type parameters, such as PixelFormat, for details please refer to MvCameraNode.xlsx file under SDK installation directory
    int GetEnumValue(IN const char* strKey, OUT MVCC_ENUMVALUE *pEnumValue);
    int SetEnumValue(IN const char* strKey, IN unsigned int nValue);
    int SetEnumValueByString(IN const char* strKey, IN const char* sValue);

    // ch:获取和设置Float型参数,如 ExposureTime和Gain,详细内容参考SDK安装目录下的 MvCameraNode.xlsx 文件
    // en:Get Float type parameters, such as ExposureTime and Gain, for details please refer to MvCameraNode.xlsx file under SDK installation directory
    int GetFloatValue(IN const char* strKey, OUT MVCC_FLOATVALUE *pFloatValue);
    int SetFloatValue(IN const char* strKey, IN float fValue);

    // ch:获取和设置Bool型参数,如 ReverseX,详细内容参考SDK安装目录下的 MvCameraNode.xlsx 文件
    // en:Get Bool type parameters, such as ReverseX, for details please refer to MvCameraNode.xlsx file under SDK installation directory
    int GetBoolValue(IN const char* strKey, OUT bool *pbValue);
    int SetBoolValue(IN const char* strKey, IN bool bValue);

private:

    void*  m_hDevHandle;
};

#endif // MVCAMERA_H

mvcamera.cpp:

#include "mvcamera.h"

MvCamera::MvCamera()
{
    m_hDevHandle = MV_NULL;
}

MvCamera::~MvCamera()
{
    if (m_hDevHandle)
    {
        MV_CC_DestroyHandle(m_hDevHandle);
        m_hDevHandle    = MV_NULL;
    }
}

int MvCamera::EnumDevices(unsigned int nTLayerType, MV_CC_DEVICE_INFO_LIST *pstDevList)
{
    return MV_CC_EnumDevices(nTLayerType, pstDevList);
}

int MvCamera::Open(MV_CC_DEVICE_INFO *pstDeviceInfo)
{
    if (MV_NULL == pstDeviceInfo)
    {
        return MV_E_PARAMETER;
    }

    if (m_hDevHandle)
    {
        return MV_E_CALLORDER;
    }

    int nRet  = MV_CC_CreateHandle(&m_hDevHandle, pstDeviceInfo);
    if (MV_OK != nRet)
    {
        return nRet;
    }

    nRet = MV_CC_OpenDevice(m_hDevHandle);
    if (MV_OK != nRet)
    {
        MV_CC_DestroyHandle(m_hDevHandle);
        m_hDevHandle = MV_NULL;
    }

    return nRet;
}

int MvCamera::Close()
{
    if (MV_NULL == m_hDevHandle)
    {
        return MV_E_HANDLE;
    }

    MV_CC_CloseDevice(m_hDevHandle);

    int nRet = MV_CC_DestroyHandle(m_hDevHandle);
    m_hDevHandle = MV_NULL;

    return nRet;
}

int MvCamera::RegisterImageCallBack(void (*cbOutput)(unsigned char *, MV_FRAME_OUT_INFO_EX *, void *), void *pUser)
{
    return MV_CC_RegisterImageCallBackEx(m_hDevHandle, cbOutput, pUser);
}

int MvCamera::StartGrabbing()
{
    return MV_CC_StartGrabbing(m_hDevHandle);
}

int MvCamera::StopGrabbing()
{
    return MV_CC_StopGrabbing(m_hDevHandle);
}

int MvCamera::GetImageBuffer(MV_FRAME_OUT *pFrame, int nMsec)
{
    return MV_CC_GetImageBuffer(m_hDevHandle, pFrame, nMsec);
}

int MvCamera::FreeImageBuffer(MV_FRAME_OUT *pFrame)
{
    return MV_CC_FreeImageBuffer(m_hDevHandle, pFrame);
}

int MvCamera::DisplayOneFrame(MV_DISPLAY_FRAME_INFO *pDisplayInfo)
{
    return MV_CC_DisplayOneFrame(m_hDevHandle, pDisplayInfo);
}

int MvCamera::GetDeviceInfo(MV_CC_DEVICE_INFO *pstDevInfo)
{
    return MV_CC_GetDeviceInfo(m_hDevHandle, pstDevInfo);
}

int MvCamera::SetEnumValue(const char *strKey, unsigned int nValue)
{
    return MV_CC_SetEnumValue(m_hDevHandle, strKey, nValue);
}

int MvCamera::GetFloatValue(const char *strKey, MVCC_FLOATVALUE *pFloatValue)
{
    return MV_CC_GetFloatValue(m_hDevHandle, strKey, pFloatValue);
}

int MvCamera::SetFloatValue(const char *strKey, float fValue)
{
    return MV_CC_SetFloatValue(m_hDevHandle, strKey, fValue);
}

int MvCamera::SetBoolValue(const char *strKey, bool bValue)
{
    return MV_CC_SetBoolValue(m_hDevHandle, strKey, bValue);
}

grabthread.h:

#ifndef GRABTHREAD_H
#define GRABTHREAD_H

#include 
#include 
#include 
#include "mvcamera.h"
#include 
#include "opencv2/opencv.hpp"
#include "opencv2/core.hpp"
#include "algorithm.h"

class Algorithm;
class GrabThread
        :public QThread
{
    Q_OBJECT
public:
    GrabThread(MvCamera *pMvCamera);
    ~GrabThread();
    virtual void run();

    inline void setThreadState(bool ret) {m_bThreadState = ret;}
signals:
    void grabImg(QImage& img);

private:
    MvCamera* m_pMvCamera;
    bool m_bThreadState;
};

#endif // GRABTHREAD_H

grabthread.cpp:

#include "grabthread.h"

GrabThread::GrabThread(MvCamera *pMvCamera)
    :m_bThreadState(false)
{
    m_pMvCamera = pMvCamera;

    qRegisterMetaType(" QImage&");
}

GrabThread::~GrabThread()
{
//    delete m_pMvCamera;
//    m_pMvCamera = nullptr;
}

void GrabThread::run()
{
    MV_FRAME_OUT stImageInfo = {0};
    int nRet = MV_OK;
    while(m_bThreadState)
    {
        qDebug()<GetImageBuffer(&stImageInfo, 1000);
        if(nRet == MV_OK)
        {
            void* buff = stImageInfo.pBufAddr;
            int width = stImageInfo.stFrameInfo.nWidth;
            int height = stImageInfo.stFrameInfo.nHeight;

            m_pMvCamera->FreeImageBuffer(&stImageInfo);
            cv::Mat Mat_img = cv::Mat(height,width,CV_8UC3,(uchar*)buff);
            cv::cvtColor(Mat_img,Mat_img,cv::COLOR_BGR2RGB);

            Algorithm alg;
            QImage img = alg.Mat2QImage(Mat_img);
//            QImage img = QImage(buff,width,height,QImage::Format_RGB888);
//            cvtColor(image,rgb,CV_BGR2RGB);

//            QImage img;
//            try
//            {
//                cv::Mat matImg;
//                matImg = cv::Mat(height,width,CV_8UC3,buff);
                cv::cvtColor(matImg,matImg,cv::COLOR_BGR2RGB);
//                Algorithm alg;
//                img = alg.Mat2QImage(matImg);
//            }
//            catch(cv::Exception& e)
//            {
//                qDebug()<<"error";
//            }
//            QImage img(width,height,QImage::Format_Indexed8);
//            {
//                img.setColorCount(256);
//                for(int i=0;i<256;i++)
//                    img.setColor(i,qRgb(i,i,i));
//                uchar *pSrc = reinterpret_cast(buff);
//                for(int row = 0; row(width));
//                    pSrc += width;
//                }
//            }

            emit grabImg(img);


        }


    }
}
algorithm.h:
#ifndef ALGORITHM_H
#define ALGORITHM_H

#include "opencv2/opencv.hpp"
#include "opencv2/core.hpp"
#include "qimage.h"
#include "QDebug"

class Algorithm
{
public:
    Algorithm();
    QImage Mat2QImage(const cv::Mat& mat);
    cv::Mat QImage2cvMat(QImage image);
};

#endif // ALGORITHM_H
algorithm.cpp:
#include "algorithm.h"

Algorithm::Algorithm()
{
}

cv::Mat Algorithm::QImage2cvMat(QImage image)
{
    cv::Mat mat;
   // qDebug() << image.format();
    switch(image.format())
    {
    case QImage::Format_ARGB32:
    case QImage::Format_RGB32:
    case QImage::Format_ARGB32_Premultiplied:
        mat = cv::Mat(image.height(), image.width(), CV_8UC4, (void*)image.constBits(), image.bytesPerLine());
        break;
    case QImage::Format_RGB888:
        mat = cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine());
        cv::cvtColor(mat, mat, cv::COLOR_BGR2RGB);
        break;
    case QImage::Format_Indexed8:
        mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine());
        break;
    }
    return mat;
}

QImage Algorithm::Mat2QImage(const cv::Mat& mat)

{
    // 8-bits unsigned, NO. OF CHANNELS = 1
    if(mat.type() == CV_8UC1)
    {
        QImage image(mat.cols, mat.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 = mat.data;
        for(int row = 0; row < mat.rows; row ++)
        {
            uchar *pDest = image.scanLine(row);
            memcpy(pDest, pSrc, mat.cols);
            pSrc += mat.step;
        }
        return image;
    }
    // 8-bits unsigned, NO. OF CHANNELS = 3
    else if(mat.type() == CV_8UC3)
    {
        // Copy input Mat
        const uchar *pSrc = (const uchar*)mat.data;
        // Create QImage with same dimensions as input Mat
        QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
        return image.rgbSwapped();
    }
    else if(mat.type() == CV_8UC4)
    {
        qDebug() << "CV_8UC4";
        // Copy input Mat
        const uchar *pSrc = (const uchar*)mat.data;
        // Create QImage with same dimensions as input Mat
        QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);
        return image.copy();
    }
    else
    {
        qDebug() << "ERROR: Mat could not be converted to QImage.";
        return QImage();
    }


}

widget.h:
#ifndef WIDGET_H
#define WIDGET_H

#include 
#include "mvcamera.h"
#include "QDebug"
#include "grabthread.h"
#include 
#include "algorithm.h"
#include "string.h"

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

typedef enum
{
    GrabState_OpenGrab,
    GrabState_CloseGrab,
}GrabState;

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
public slots:
    // 打开,关闭设备槽函数
    void slotBtnOpenDevice();
    void slotBtnCloseDevice();
    // 开始停止抓取图像槽函数
    void slotBtnStartGrab();
    void slotBtnStopGrab();
    // 显示图像槽函数
    void slotDisImg(QImage &img);

    // 保存图像槽函数
    void slotBtnSaveBMP();
    void slotBtnSaveJPG();
    void slotBtnSaveTIFF();
    void slotBtnSavePNG();

    // 相机参数相关槽函数
    void slotBtnGetParam();
    void slotBtnSetParam();

    // g伽马校正相关槽函数
    void slotBtnGetGamma();
    void slotBtnSetGamma();

private:
    void updateState(GrabState ret);

private:
    bool m_bOpenDevice;
private:
    double m_dExposureEdit;  // 曝光时间
    double m_dGainEdit;      // 增益
    double m_dFrameRateEdit; // 帧率
    double m_dGammaEdit; // 伽马值

    int GetExposureTime(); // ch:设置曝光时间 | en:Set Exposure Time
    int SetExposureTime();
    int GetGain();         // ch:设置增益 | en:Set Gain
    int SetGain();
    int GetFrameRate();    // ch:设置帧率 | en:Set Frame Rate
    int SetFrameRate();
    int GetGamma();    // ch:伽马矫正 | en:Set Gamma
    int SetGamma();


private:
    bool m_bSaveImgBMP;
    bool m_bSaveImgJPG;
    bool m_bSaveImgTIFF;
    bool m_bSaveImgPNG;
    bool m_bUpdateData;

    MvCamera* m_pMvCamera;
    GrabThread* m_pGrabThread;
    Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp:
#include "widget.h"
#include "ui_widget.h"
#include 
#include "string.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , m_bOpenDevice(false)
    , m_pMvCamera(nullptr)
    , m_pGrabThread(nullptr)
    , ui(new Ui::Widget)
    , m_bSaveImgBMP(false)
    , m_bSaveImgJPG(false)
    , m_bSaveImgTIFF(false)
    , m_bSaveImgPNG(false)
    , m_bUpdateData(false)

    , m_dExposureEdit(0)
    , m_dGainEdit(0)
    , m_dFrameRateEdit(0)
    , m_dGammaEdit(0)

{
    ui->setupUi(this);

    // 点击“开始抓图”, 开始抓取图像
    connect(ui->btnStartGrab,&QPushButton::clicked,this,&Widget::slotBtnStartGrab);
    // 点击“停止抓图”, 停止抓取图像
    connect(ui->btnStopGrab,&QPushButton::clicked,this,&Widget::slotBtnStopGrab);

    // 点击“保存图像”,开始保存图像
    connect(ui->btnSaveBMP,&QPushButton::clicked,this,&Widget::slotBtnSaveBMP);
    connect(ui->btnSaveJPG,&QPushButton::clicked,this,&Widget::slotBtnSaveJPG);
    connect(ui->btnSaveTIFF,&QPushButton::clicked,this,&Widget::slotBtnSaveTIFF);
    connect(ui->btnSavePNG,&QPushButton::clicked,this,&Widget::slotBtnSavePNG);

    connect(ui->btnCloseWin,&QPushButton::clicked,this,&Widget::close);

    // 点击“参数获取”,获取相机参数
    connect(ui->B_getParam,&QPushButton::clicked,this,&Widget::slotBtnGetParam);
    // 点击“参数设置”,设置相机参数
    connect(ui->B_setParam,&QPushButton::clicked,this,&Widget::slotBtnSetParam);

    // 点击“参数获取”,获取gamma值
    connect(ui->B_getGamma,&QPushButton::clicked,this,&Widget::slotBtnGetGamma);
    // 点击“伽马校正”,进行伽马校正
    connect(ui->B_setGamma,&QPushButton::clicked,this,&Widget::slotBtnSetGamma);

    updateState(GrabState_CloseGrab);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::slotBtnOpenDevice()
{

    if(true == m_bOpenDevice || m_pMvCamera != nullptr)
    {
        qDebug()<<"is open Device";
        return;
    }


    // get current link camera
    MV_CC_DEVICE_INFO_LIST  m_stDevList;
    int nRet = MvCamera::EnumDevices(MV_GIGE_DEVICE , &m_stDevList);
    if (MV_OK != nRet)
    {
        qDebug()<<"get Camera Error";
        return;
    }

    m_pMvCamera = new MvCamera;
    if(nullptr == m_pMvCamera)
        return;

    nRet = m_pMvCamera->Open(m_stDevList.pDeviceInfo[0]);
    if (MV_OK != nRet)
    {
        delete m_pMvCamera;
        m_pMvCamera = nullptr;
        qDebug()<<"Open Fail";
        return;
    }

    qDebug()<<"Open success";
    m_bOpenDevice = true;
}

void Widget::slotBtnCloseDevice()
{
    if(m_pMvCamera != nullptr)
    {
        m_pMvCamera->Close();

        delete m_pMvCamera;
        m_pMvCamera = nullptr;
        qDebug()<<"close success";
        m_bOpenDevice = false;
    }
    else
       qDebug()<<"device is not create";
}

void Widget::slotBtnStartGrab()
{
    slotBtnOpenDevice();
    QThread::msleep(500);
    if(!m_bOpenDevice )
    {
        QMessageBox::information(this,"error","device is not open");
        return;
    }

    m_pGrabThread = new GrabThread(m_pMvCamera);

    connect(m_pGrabThread,&GrabThread::grabImg,this,&Widget::slotDisImg);

    int nRet = m_pMvCamera->StartGrabbing();

    if (MV_OK != nRet)
    {
        qDebug()<<"Start grabbing fail : "<< nRet;
        return;
    }
    qDebug()<<"Start grabbing sussess";

    m_pGrabThread->setThreadState(true);
    m_pGrabThread->start();

    updateState(GrabState_OpenGrab);


}

void Widget::slotBtnStopGrab()
{
    if(true != m_bOpenDevice || m_pMvCamera == nullptr|| m_pGrabThread == nullptr)
    {
        return;
    }

    m_pGrabThread->setThreadState(false);
    m_pGrabThread->wait();
    m_pGrabThread->quit();
    disconnect(m_pGrabThread,&GrabThread::grabImg,this,&Widget::slotDisImg);

    int nRet = m_pMvCamera->StopGrabbing();

    delete m_pGrabThread;
//    m_pGrabThread->deleteLater();
    m_pGrabThread = nullptr;

    if (MV_OK != nRet)
    {
        qDebug()<<"Stop grabbing fail";
        return;
    }
    qDebug()<<"Stop grabbing sussess";

    QThread::msleep(500);

    slotBtnCloseDevice();

    updateState(GrabState_CloseGrab);

}

void Widget::slotDisImg(QImage &img)
{
    qDebug()<<"slot frame";
    ui->label->clear();
//    img = img.scaled(ui->label->width(), ui->label->height());
    img = img.scaled(ui->label->size(),Qt::IgnoreAspectRatio);
    ui->label->setScaledContents(true);

    Algorithm alg;
    cv::Mat matImg = alg.QImage2cvMat(img);

//    cv::threshold(matImg,matImg,128,255,1);

    QImage retImg = alg.Mat2QImage(matImg);

//    QImage retImg =  img;
    ui->label->setPixmap(QPixmap::fromImage(retImg));

    if(m_bSaveImgBMP == true)
    {
        m_bSaveImgBMP = false;
        // add code to save img
        QString filename = QFileDialog::getSaveFileName(NULL, tr("Save Image"), "", tr("Images (*.bmp)"));
        cv::imwrite(filename.toStdString(), matImg);
    }
    if(m_bSaveImgJPG == true)
    {
        m_bSaveImgJPG = false;
        // add code to save img
        QString filename = QFileDialog::getSaveFileName(NULL, tr("Save Image"), "", tr("Images (*.jpg)"));
        cv::imwrite(filename.toStdString(), matImg);
    }
    if(m_bSaveImgTIFF == true)
    {
        m_bSaveImgTIFF = false;
        // add code to save img
        QString filename = QFileDialog::getSaveFileName(NULL, tr("Save Image"), "", tr("Images (*.tiff)"));
        cv::imwrite(filename.toStdString(), matImg);
    }
    if(m_bSaveImgPNG == true)
    {
        m_bSaveImgPNG = false;
        // add code to save img
        QString filename = QFileDialog::getSaveFileName(NULL, tr("Save Image"), "", tr("Images (*.png)"));
        cv::imwrite(filename.toStdString(), matImg);
    }

}

void Widget::slotBtnSaveBMP()
{
    m_bSaveImgBMP = true;
}

void Widget::slotBtnSaveJPG()
{
    m_bSaveImgJPG = true;
}
void Widget::slotBtnSaveTIFF()
{
    m_bSaveImgTIFF = true;
}
void Widget::slotBtnSavePNG()
{
    m_bSaveImgPNG= true;
}

void Widget::slotBtnGetParam()
{
    int nRet = GetExposureTime();
        if (nRet != MV_OK)
        {
            QMessageBox::information(this,"error","Get Exposure Time Fail");

        }

        nRet = GetGain();
        if (nRet != MV_OK)
        {
            QMessageBox::information(this,"error","Get Gain Fail");
        }

        nRet = GetFrameRate();
        if (nRet != MV_OK)
        {
            QMessageBox::information(this,"error","Get Frame Rate Fail");
        }

        m_bUpdateData = false;
}

void Widget::slotBtnSetParam()
{
    m_bUpdateData = true;

        bool bIsSetSucceed = true;
        int nRet = SetExposureTime();
        if (nRet != MV_OK)
        {
            bIsSetSucceed = false;
            QMessageBox::information(this,"error","Set Exposure Time Fail");

        }
        nRet = SetGain();
        if (nRet != MV_OK)
        {
            bIsSetSucceed = false;
            QMessageBox::information(this,"error","设置增益失败,增益范围:0~15.0062");
        }
        nRet = SetFrameRate();
        if (nRet != MV_OK)
        {
            bIsSetSucceed = false;
            QMessageBox::information(this,"error","Set Frame Rate Fail");
        }

        if (true == bIsSetSucceed)
        {
            QMessageBox::information(this,"Succeed","Set Parameter Succeed");
        }
}

void Widget::updateState(GrabState ret)
{
    if(ret == GrabState_OpenGrab)
    {
        ui->btnCloseWin->setEnabled(false);
        ui->B_getParam->setEnabled(true);
        ui->B_setParam->setEnabled(true);
        ui->B_getGamma->setEnabled(true);
        ui->B_setGamma->setEnabled(true);
        ui->btnSaveBMP->setEnabled(true);
        ui->btnSaveJPG->setEnabled(true);
        ui->btnSaveTIFF->setEnabled(true);
        ui->btnSavePNG->setEnabled(true);
    }
    else
    {
        ui->btnCloseWin->setEnabled(true);
        ui->B_getParam->setEnabled(false);
        ui->B_setParam->setEnabled(false);
        ui->B_getGamma->setEnabled(false);
        ui->B_setGamma->setEnabled(false);
        ui->btnSaveBMP->setEnabled(false);
        ui->btnSaveJPG->setEnabled(false);
        ui->btnSaveTIFF->setEnabled(false);
        ui->btnSavePNG->setEnabled(false);
    }
}



// 设置相机参数
int Widget::GetExposureTime()
{
    MVCC_FLOATVALUE stFloatValue = {0};

        int nRet = m_pMvCamera->GetFloatValue("ExposureTime", &stFloatValue);
        if (MV_OK != nRet)
        {
            return nRet;
        }

        m_dExposureEdit = stFloatValue.fCurValue;
        QString exposure_str = QString::number(m_dExposureEdit, 'f', 2);
        ui->B__expose->setPlainText(exposure_str);
        return MV_OK;
}

int Widget::SetExposureTime()
{
    // ch:调节这两个曝光模式,才能让曝光时间生效
        int nRet = m_pMvCamera->SetEnumValue("ExposureMode", MV_EXPOSURE_MODE_TIMED);
        if (MV_OK != nRet)
        {
            return nRet;
        }

        m_pMvCamera->SetEnumValue("ExposureAuto", MV_EXPOSURE_AUTO_MODE_OFF);
        m_dExposureEdit = ui->B__expose->toPlainText().toDouble();
        return m_pMvCamera->SetFloatValue("ExposureTime", (float)m_dExposureEdit);
}

int Widget::GetGain()
{
    MVCC_FLOATVALUE stFloatValue = {0};
        int nRet = m_pMvCamera->GetFloatValue("Gain", &stFloatValue);
        if (MV_OK != nRet)
        {
            return nRet;
        }
        m_dGainEdit = stFloatValue.fCurValue;
        QString gain_str = QString::number(m_dGainEdit, 'f', 2);
        ui->B__gamma_2->setPlainText(gain_str);
        return MV_OK;
}

int Widget::SetGain()
{
    // ch:设置增益前先把自动增益关闭,失败无需返回
        m_pMvCamera->SetEnumValue("GainAuto", 100);
        m_dGainEdit = ui->B__gamma_2->toPlainText().toDouble();
        return m_pMvCamera->SetFloatValue("Gain", (float)m_dGainEdit);
}

int Widget::GetFrameRate()
{
    MVCC_FLOATVALUE stFloatValue = {0};
        int nRet = m_pMvCamera->GetFloatValue("ResultingFrameRate", &stFloatValue);
        if (MV_OK != nRet)
        {
            return nRet;
        }
        m_dFrameRateEdit = stFloatValue.fCurValue;
        QString frameRate_str = QString::number(m_dFrameRateEdit, 'f', 2);
        ui->B__fps->setPlainText(frameRate_str);
        return MV_OK;
}

int Widget::SetFrameRate()
{
    int nRet = m_pMvCamera->SetBoolValue("AcquisitionFrameRateEnable", true);
        if (MV_OK != nRet)
        {
            return nRet;
        }
        m_dFrameRateEdit = ui->B__fps->toPlainText().toDouble();
        return m_pMvCamera->SetFloatValue("AcquisitionFrameRate", (float)m_dFrameRateEdit);
}

int Widget::GetGamma()
{
    MVCC_FLOATVALUE stFloatValue = {0};
        int nRet = m_pMvCamera->GetFloatValue("Gamma", &stFloatValue);
        if (MV_OK != nRet)
        {
            return nRet;
        }
        m_dGammaEdit = stFloatValue.fCurValue;
        QString gamma_str = QString::number(m_dGammaEdit, 'f', 2);
        ui->B__Gamma->setPlainText(gamma_str);
        return MV_OK;
}

int Widget::SetGamma()
{
    int nRet = m_pMvCamera->SetBoolValue("GammaEnable", true);
        if (MV_OK != nRet)
        {
            return nRet;
        }
        m_dGammaEdit = ui->B__Gamma->toPlainText().toDouble();
        return m_pMvCamera->SetFloatValue("Gamma", (float)m_dGammaEdit);
}

void Widget::slotBtnGetGamma()
{
    int nRet = GetGamma();
        if (nRet != MV_OK)
        {
            QMessageBox::information(this,"error","Get Gamma Fail");
        }
        m_bUpdateData = false;
}

void Widget::slotBtnSetGamma()
{
    m_bUpdateData = true;

        bool bIsSetSucceed = true;
        int nRet = SetGamma();
        if (nRet != MV_OK)
        {
            bIsSetSucceed = false;
            QMessageBox::information(this,"error","设置伽马校正失败, gamma取值范围:0~4");

        }

        if (true == bIsSetSucceed)
        {
            QMessageBox::information(this,"Succeed","Gamma Correction Succeed");
        }
}

widget.ui:(自动生成)



 Widget
 
  
   
    0
    0
    798
    548
   
  
  
   Widget
  
  
   
    
     9
     9
     551
     521
    
   
   
    QLabel{
border-image:url(1.jpg);
border:5px solid red;
border-image:url(1.jpg);
}

   
   
    
   
  
  
   
    
     570
     90
     221
     106
    
   
   
    font: 12pt "楷体";
background-color: rgb(255, 255, 255);
   
   
    图像保存
   
   
    
     
      
       
        
         
          100
          30
         
        
        
         <html><head/><body><p align="center"><span style=" color:#ffffff;"><br/></span></p></body></html>
        
        
         font: 12pt "楷体";
background-color: qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5, stop:0 rgba(255, 235, 235, 206), stop:0.35 rgba(255, 188, 188, 80), stop:0.4 rgba(255, 162, 162, 80), stop:0.425 rgba(255, 132, 132, 156), stop:0.44 rgba(252, 128, 128, 80), stop:1 rgba(255, 255, 255, 0));
        
        
         保存BMP
        
       
      
      
       
        
         
          100
          30
         
        
        
         <html><head/><body><p align="center"><span style=" color:#ffffff;"><br/></span></p></body></html>
        
        
         font: 12pt "楷体";
background-color: qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5, stop:0 rgba(255, 235, 235, 206), stop:0.35 rgba(255, 188, 188, 80), stop:0.4 rgba(255, 162, 162, 80), stop:0.425 rgba(255, 132, 132, 156), stop:0.44 rgba(252, 128, 128, 80), stop:1 rgba(255, 255, 255, 0));
        
        
         保存JPG
        
       
      
     
    
    
     
      
       
        
         
          100
          30
         
        
        
         <html><head/><body><p align="center"><span style=" color:#ffffff;"><br/></span></p></body></html>
        
        
         font: 12pt "楷体";
background-color: qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5, stop:0 rgba(255, 235, 235, 206), stop:0.35 rgba(255, 188, 188, 80), stop:0.4 rgba(255, 162, 162, 80), stop:0.425 rgba(255, 132, 132, 156), stop:0.44 rgba(252, 128, 128, 80), stop:1 rgba(255, 255, 255, 0));
        
        
         保存TIFF
        
       
      
      
       
        
         
          100
          30
         
        
        
         <html><head/><body><p align="center"><span style=" color:#ffffff;"><br/></span></p></body></html>
        
        
         font: 12pt "楷体";
background-color: qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5, stop:0 rgba(255, 235, 235, 206), stop:0.35 rgba(255, 188, 188, 80), stop:0.4 rgba(255, 162, 162, 80), stop:0.425 rgba(255, 132, 132, 156), stop:0.44 rgba(252, 128, 128, 80), stop:1 rgba(255, 255, 255, 0));
        
        
         保存PNG
        
       
      
     
    
   
  
  
   
    
     570
     20
     221
     68
    
   
   
    font: 12pt "楷体";
background-color: rgb(255, 255, 255);
   
   
    图像采集
   
   
    
     
      10
      30
      208
      32
     
    
    
     
      
       
        
         100
         30
        
       
       
        <html><head/><body><p align="center"><span style=" color:#ffffff;"><br/></span></p></body></html>
       
       
        font: 12pt "楷体";
background-color: qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5, stop:0 rgba(255, 235, 235, 206), stop:0.35 rgba(255, 188, 188, 80), stop:0.4 rgba(255, 162, 162, 80), stop:0.425 rgba(255, 132, 132, 156), stop:0.44 rgba(252, 128, 128, 80), stop:1 rgba(255, 255, 255, 0));
       
       
        开始抓图
       
      
     
     
      
       
        
         100
         30
        
       
       
        <html><head/><body><p align="center"><span style=" color:#ffffff;"><br/></span></p></body></html>
       
       
        font: 12pt "楷体";
background-color: qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5, stop:0 rgba(255, 235, 235, 206), stop:0.35 rgba(255, 188, 188, 80), stop:0.4 rgba(255, 162, 162, 80), stop:0.425 rgba(255, 132, 132, 156), stop:0.44 rgba(252, 128, 128, 80), stop:1 rgba(255, 255, 255, 0));
       
       
        停止抓图
       
      
     
    
   
  
  
   
    
     570
     200
     228
     182
    
   
   
    font: 12pt "楷体";
background-color: rgb(255, 255, 255);
   
   
    参数设置
   
   
    
     
      
       
        
         background-color: rgb(255, 255, 174);
        
        
         曝光
        
       
      
      
       
        
         
          150
          30
         
        
        
         background-color: qradialgradient(spread:repeat, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5, stop:0 rgba(255, 170, 0, 62), stop:1 rgba(255, 255, 255, 255));
        
        
         请输入曝光值
        
       
      
     
    
    
     
      
       
        
         background-color: rgb(255, 255, 174);
        
        
         增益
        
       
      
      
       
        
         
          150
          30
         
        
        
         background-color: qradialgradient(spread:repeat, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5, stop:0 rgba(255, 170, 0, 62), stop:1 rgba(255, 255, 255, 255));
        
        
         取值范围:0~15
        
       
      
     
    
    
     
      
       
        
         background-color: rgb(255, 255, 174);
        
        
         帧率
        
       
      
      
       
        
         
          150
          30
         
        
        
         background-color: qradialgradient(spread:repeat, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5, stop:0 rgba(255, 170, 0, 62), stop:1 rgba(255, 255, 255, 255));
        
        
         请输入帧率值
        
       
      
     
    
    
     
      
       
        
         
          100
          30
         
        
        
         <html><head/><body><p align="center"><span style=" color:#ffffff;"><br/></span></p></body></html>
        
        
         font: 12pt "楷体";
background-color: qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5, stop:0 rgba(255, 235, 235, 206), stop:0.35 rgba(255, 188, 188, 80), stop:0.4 rgba(255, 162, 162, 80), stop:0.425 rgba(255, 132, 132, 156), stop:0.44 rgba(252, 128, 128, 80), stop:1 rgba(255, 255, 255, 0));
        
        
         参数获取
        
       
      
      
       
        
         
          100
          30
         
        
        
         <html><head/><body><p align="center"><span style=" color:#ffffff;"><br/></span></p></body></html>
        
        
         font: 12pt "楷体";
background-color: qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5, stop:0 rgba(255, 235, 235, 206), stop:0.35 rgba(255, 188, 188, 80), stop:0.4 rgba(255, 162, 162, 80), stop:0.425 rgba(255, 132, 132, 156), stop:0.44 rgba(252, 128, 128, 80), stop:1 rgba(255, 255, 255, 0));
        
        
         参数设置
        
       
      
     
    
   
  
  
   
    
     570
     380
     221
     111
    
   
   
    font: 12pt "楷体";
background-color: rgb(255, 255, 255);
   
   
    伽马校正
   
   
    
     
      
       
        
         background-color: rgb(255, 255, 174);
        
        
         伽马值
        
       
      
      
       
        
         
          150
          30
         
        
        
         background-color: qradialgradient(spread:repeat, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5, stop:0 rgba(255, 170, 0, 62), stop:1 rgba(255, 255, 255, 255));
        
        
         取值范围:0~4
        
       
      
     
    
    
     
      
       
        
         
          100
          30
         
        
        
         <html><head/><body><p align="center"><span style=" color:#ffffff;"><br/></span></p></body></html>
        
        
         font: 12pt "楷体";
background-color: qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5, stop:0 rgba(255, 235, 235, 206), stop:0.35 rgba(255, 188, 188, 80), stop:0.4 rgba(255, 162, 162, 80), stop:0.425 rgba(255, 132, 132, 156), stop:0.44 rgba(252, 128, 128, 80), stop:1 rgba(255, 255, 255, 0));
        
        
         参数获取
        
       
      
      
       
        
         
          100
          30
         
        
        
         <html><head/><body><p align="center"><span style=" color:#ffffff;"><br/></span></p></body></html>
        
        
         font: 12pt "楷体";
background-color: qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5, stop:0 rgba(255, 235, 235, 206), stop:0.35 rgba(255, 188, 188, 80), stop:0.4 rgba(255, 162, 162, 80), stop:0.425 rgba(255, 132, 132, 156), stop:0.44 rgba(252, 128, 128, 80), stop:1 rgba(255, 255, 255, 0));
        
        
         伽马校正
        
       
      
     
    
   
  
  
   
    
     690
     500
     100
     30
    
   
   
    
     100
     30
    
   
   
    <html><head/><body><p align="center"><span style=" color:#ffffff;"><br/></span></p></body></html>
   
   
    font: 12pt "楷体";
background-color: qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5, stop:0 rgba(255, 235, 235, 206), stop:0.35 rgba(255, 188, 188, 80), stop:0.4 rgba(255, 162, 162, 80), stop:0.425 rgba(255, 132, 132, 156), stop:0.44 rgba(252, 128, 128, 80), stop:1 rgba(255, 255, 255, 0));
   
   
    关闭程序
   
  
 
 
 

你可能感兴趣的:(qt)