Qt+海康SDK實現硬件觸發一次拍照數張圖片


繼上篇文章:https://www.cnblogs.com/ivint/p/14200731.html (稍作改動。。。)

1、在子線程中實現先由硬件觸發再轉換成軟件觸發3次

子線程代碼:

//回調函數
void GrabImgThread::getCameraImageCallback(unsigned char *pData, MV_FRAME_OUT_INFO_EX *stImageInfo, void *arg)
{
      (void)(stImageInfo);

    GrabImgThread *tmp = (GrabImgThread *)arg;
    memcpy(tmp->mpData,pData,tmp->nDataSize);
    qDebug() << "getCameraImageCallback回調函數執行了";

    tmp->displayCameraImage();     //刷新圖像
    if(tmp->i ==4){ tmp->i = 0;}   //將i清零

}
//初始化相機
int GrabImgThread::initCamera()
{
    int nRet = -1;

    MV_CC_DEVICE_INFO_LIST stDeviceList;
    memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));

    // 枚舉相機設備
    nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &stDeviceList);
    if (MV_OK != nRet)
    {
        qDebug()<<"MV_CC_EnumDevices fail!";
        return -1;
    }

    unsigned int nIndex = 0;
    MV_CC_DEVICE_INFO* pDeviceInfo;
    if (stDeviceList.nDeviceNum > 0)
    {
        for (unsigned int i = 0; i < stDeviceList.nDeviceNum; i++)
        {
            qDebug() <<QString("[device %1]").arg(i);
            pDeviceInfo = stDeviceList.pDeviceInfo[i];
            if (NULL == pDeviceInfo)
            {
                break;
            }
        }
    }
    else
    {
        qDebug()<<"Find No Devices!";
        return -1;
    }

    pDeviceInfo = stDeviceList.pDeviceInfo[0];
    //打印相機型號及序列號
    qDebug() << QString::fromLocal8Bit(reinterpret_cast<char*>(pDeviceInfo->SpecialInfo.stGigEInfo.chModelName));
    qDebug() << QString::fromLocal8Bit(reinterpret_cast<char*>(pDeviceInfo->SpecialInfo.stGigEInfo.chSerialNumber));

    //彩色相機判別
    if('C' == pDeviceInfo->SpecialInfo.stGigEInfo.chModelName[12])
    {
        m_isColor = true;
        qDebug() << "==============color=================";
    }

    // 選擇當前設備創建句柄
    nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
    if (MV_OK != nRet)
    {
        qDebug()<<"MV_CC_CreateHandle fail!";
        return -1;
    }

    // 打開設備
    nRet = MV_CC_OpenDevice(handle);
    if (MV_OK != nRet)
    {
        qDebug()<<"MV_CC_OpenDevice fail!";
        return -1;
    }

    //獲取圖像的寬度和高度
    MVCC_INTVALUE ptValue;
//    int w,h;
    MV_CC_GetWidth(handle, &ptValue);
    m_imageWidth = ptValue.nCurValue;
    MV_CC_GetHeight(handle, &ptValue);
    m_imageHeight = ptValue.nCurValue;

    if(m_isColor)
        nDataSize = m_imageWidth * m_imageHeight * 3;
    else
        nDataSize = m_imageWidth * m_imageHeight;

    //設置硬觸發模式
    nRet = MV_CC_SetEnumValue(handle, "TriggerMode", 1);
    nRet = MV_CC_SetEnumValue(handle, "TriggerSource", 0);

    mpData = (unsigned char *)malloc(nDataSize); //獲取一幀圖像的內存大小

    return 1;
}
//顯示圖像函數
int GrabImgThread::displayCameraImage()
{

   image = QImage((uchar*)mpData,m_imageWidth,m_imageHeight,QImage::Format_RGB888);

   // ui->lb_image->setPixmap(QPixmap::fromImage(image).scaled(560,420));
   qDebug() << "發送圖像:" << i << image;
   emit doneFinsh(image);
    //qDebug() << "displayCameraImage發送完信號執行了";

    //獲取Enum型參數:獲取觸發模式
    MVCC_ENUMVALUE struEnumValue = {0};
    int  nRet = MV_CC_GetEnumValue(handle, "TriggerSource", &struEnumValue);
    qDebug() << "134行:" << struEnumValue.nCurValue;
    //判斷是否為硬觸發
    if(0 == struEnumValue.nCurValue){
        QThread::msleep(5);
//        nRet = MV_CC_StopGrabbing(handle);//關閉采集
//        if(nRet != 0){qDebug() << "StopGrabbing 失敗";}
        //設置為軟觸發
        int  nRet = MV_CC_SetEnumValue(handle, "TriggerMode", 1);
        nRet = MV_CC_SetEnumValue(handle, "TriggerSource", 7);
        if(nRet != 0){qDebug() << "設置軟觸發-- 失敗";}
        qDebug() << "設置軟觸發成功";
        nRet = MV_CC_GetEnumValue(handle, "TriggerSource", &struEnumValue);
        qDebug() << "146行-----:" << struEnumValue.nCurValue;
        //注冊軟觸發回調函數
//         int ret = MV_CC_RegisterImageCallBackForRGB(handle, GrabImgThread::softImageCallback,this);
//         if(MV_OK != ret)
//            qDebug() << "=======faild =========" ;
        //qDebug() <<  nRet;
//        nRet = MV_CC_StartGrabbing(handle);//開啟采集
//        if(nRet != 0){qDebug() << "StartGrabbing 失敗";}


            //qDebug() << comdValue;  //0
            //softBuffImg();
            //延遲並發送軟觸發信號
            i++;
            if(i<4){
            QThread::sleep(1);
            int comdValue= MV_CC_SetCommandValue(handle, "TriggerSoftware");
            if(comdValue != 0){qDebug() << "發送一次軟觸發命令-- 失敗";}
            qDebug() << "發送一次軟觸發命令";
            //i = 0;
            }
            //qDebug() << image;
            //emit doneFinsh(image);
        nRet = MV_CC_GetEnumValue(handle, "TriggerSource", &struEnumValue);
        if(nRet != 0){qDebug() << "獲取TriggerSource 失敗";}
        qDebug() << "173行+++++++" << struEnumValue.nCurValue; //打印當前觸發源
        if(7 == struEnumValue.nCurValue){
            QThread::msleep(5);
//            nRet = MV_CC_StopGrabbing(handle);//關閉采集
//            if(nRet != 0){qDebug() << "StopGrabbing 失敗";}
            //設置硬觸發模式
            nRet = MV_CC_SetEnumValue(handle, "TriggerMode", 1);
            nRet = MV_CC_SetEnumValue(handle, "TriggerSource", 0);
            if(nRet != 0){qDebug() << "軟觸發改為硬觸發-- 失敗";}
            qDebug() << "軟觸發改為硬觸發";
            nRet = MV_CC_GetEnumValue(handle, "TriggerSource", &struEnumValue);
            qDebug() << "184==========行:" << struEnumValue.nCurValue;
//            //step2: 注冊取圖回調函數
//            int ret = MV_CC_RegisterImageCallBackForRGB(handle, GrabImgThread::getCameraImageCallback,this);
//            if(MV_OK != ret)
//                qDebug() << "=======faild =======" ;
//            //step 3: 開始取圖
//            nRet = MV_CC_StartGrabbing(handle);
//            if(nRet != 0){qDebug() << "MV_CC_StartGrabbing-- 失敗";}
        }
    }
    return 1;
}
//線程啟動函數
void GrabImgThread::run()
{
    //step 1: 初始化相機
    initCamera();

    //獲取Enum型參數:獲取觸發模式
    MVCC_ENUMVALUE struEnumValue = {0};
    int  nRet = MV_CC_GetEnumValue(handle, "TriggerSource", &struEnumValue);
    //判斷是否為硬觸發
    if(0 == struEnumValue.nCurValue){
        //step2: 注冊取圖回調函數
        int ret = MV_CC_RegisterImageCallBackForRGB(handle, GrabImgThread::getCameraImageCallback,this);
        if(MV_OK != ret)
            qDebug() << "=======faild =======" ;

        //step 3: 開始取圖
        MV_CC_StartGrabbing(handle);
    }    
    qDebug() << "子線程id:" << QThread::currentThread();
}

 

2、在主線程中創建子線程實現獲取抓取的圖像

主線程代碼:

qDebug() << "主線程id:" << QThread::currentThread();
    this->setWindowTitle("OCR");

    //啟動子線程獲取圖像
    grabThread = new GrabImgThread;
    grabThread->start();
    connect(grabThread, &GrabImgThread::doneFinsh, this,
    [=](QImage image){
        if(intImage == 0){
             ui->label_img->setPixmap(QPixmap::fromImage(image).scaled(560,420));
        }
        if(intImage == 1){
             ui->label_img2->setPixmap(QPixmap::fromImage(image).scaled(560,420));
        }
        if(intImage == 2){
             ui->label_img3->setPixmap(QPixmap::fromImage(image).scaled(560,420));
        }
        if(intImage == 3){
             ui->label_img4->setPixmap(QPixmap::fromImage(image).scaled(560,420));
        }
        intImage++;
        if(intImage == 4){ intImage = 0;}
        //emit softTrigger();
    });

運行結果如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM