WIN10 C++调用海康工业相机

1.安装海康工业相机套件MVS,需要下载的MVS如下图所示。链接为https://www.hikrobotics.com/cn/machinevision/service/download?module=0在这里插入图片描述

2.在VS中添加头文件在这里插入图片描述

3.在VS中添加库目录在这里插入图片描述

4.在VS中添加依赖项在这里插入图片描述

5.代码如下

#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <string>

#include "MvCameraControl.h"
#include "opencv2/opencv.hpp"


bool printDeviceInfo(MV_CC_DEVICE_INFO* hk_device);
bool hk2cv(MV_FRAME_OUT_INFO_EX* hk_imginfo, unsigned char* data, cv::Mat& src_img);

using namespace std;
int main(int argv, char** argc) {

    //1.获取SDK版本信息
    unsigned int sdk_V = MV_CC_GetSDKVersion();
    printf("SDK version is [0x%x]\n", sdk_V);


    //2.查找目标设备
    int ret = MV_OK;
    void* handle = NULL;

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

    ret = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &hk_devices);
    if (ret != MV_OK) {
        std::cout << "enum devices faild!" << std::endl;
        return -1;
    }
    std::cout << "共检测到相机的个数为:" << hk_devices.nDeviceNum << std::endl;


    //3.判断设备是否可访问
    if (hk_devices.nDeviceNum > 0)
    {
        for (unsigned int i = 0; i < hk_devices.nDeviceNum; i++)
        {
            printf("[device %d]:\n", i);
            MV_CC_DEVICE_INFO* pDeviceInfo = hk_devices.pDeviceInfo[i];

            bool access = MV_CC_IsDeviceAccessible(pDeviceInfo, 1);

            if (!access){
                printf("[device %d]:could not access... nRet:[0x%x]\n", i, ret);
            }
            else {
                printf("[device %d]:is OK... nRet:[0x%x]\n", i, ret);
            }
        }
    }
    else
    {
        printf("Find No Devices!\n");
        return -1;
    }

    if (hk_devices.nDeviceNum > 0) {
        MV_CC_DEVICE_INFO* hk_camera = hk_devices.pDeviceInfo[0];
        if (printDeviceInfo(hk_camera) == false) {
            return -1;
        }
    }
    else {
        std::cout << "no device found" << std::endl;
        return -1;
    }


    //4.创建相机句柄
    ret = MV_CC_CreateHandle(&handle, hk_devices.pDeviceInfo[0]);
    if (ret != MV_OK) {
        return -1;
    }


    //5.打开设备
    ret = MV_CC_OpenDevice(handle);
    if (ret != MV_OK) {
        cout << "设备未打开" << endl;
        return -1;
    }
    else {
        std::cout << "设备已经打开" << std::endl;
    }


    //6.设置触发模式为off
    ret = MV_CC_SetEnumValue(handle, "TriggerMode", 0);
    if (ret != MV_OK) {
        return -1;
    }


    //7.获取数据包的大小
    MVCC_INTVALUE hk_param;
    memset(&hk_param, 0, sizeof(MVCC_INTVALUE));
    ret = MV_CC_GetIntValue(handle, "PayloadSize", &hk_param);
    if (ret != MV_OK) {
        return -1;
    }
    unsigned int payload_size = hk_param.nCurValue;

    // load config
    //ret = MV_CC_FeatureLoad(handle, "FeatureFile.ini");
    //if (ret != MV_OK) {
    //    cout << "loading config file faild" << endl;
    //    return -1;
    //}

    // save config
    //ret = MV_CC_FeatureSave(handle, "FeatureFile.ini");
    //if (ret != MV_OK) {
    //    return -1;
    //}

    //8.开始截流
    ret = MV_CC_StartGrabbing(handle);
    if (ret != MV_OK) {
        cout << "grab image failed!" << endl;
        return -1;
    }
    MV_FRAME_OUT_INFO_EX hk_imginfo;
    memset(&hk_imginfo, 0, sizeof(MV_FRAME_OUT_INFO_EX));
    unsigned char* data = (unsigned char*)malloc(sizeof(unsigned char) * (payload_size));
    if (data == NULL) {
        return -1;
    }

    cv::Mat src_img;
    while (!(_kbhit() && _getch() == 0x1b)) {
        ret = MV_CC_GetOneFrameTimeout(handle, data, payload_size, &hk_imginfo, 1000);
        if (ret != MV_OK) {
            free(data);
            data = NULL;
            return -1;
        }
        if (hk2cv(&hk_imginfo, data, src_img) == false) {
            continue;
        }
        cv::imshow("test", src_img);
        cv::waitKey(30);
    }


    //9.停止截流
    ret = MV_CC_StopGrabbing(handle);
    if (ret != MV_OK) {
        return -1;

    }


    //10.关闭设备
    ret = MV_CC_CloseDevice(handle);
    if (ret != MV_OK) {
        return -1;

    }


    //11.销毁相机句柄
    ret = MV_CC_DestroyHandle(handle);
    if (ret != MV_OK) {
        return -1;
    }
    system("pause");
    return 0;
}



bool printDeviceInfo(MV_CC_DEVICE_INFO* hk_device) {
    if (NULL == hk_device)
    {
        printf("The Pointer of hk_device is NULL!\n");
        return false;
    }
    if (hk_device->nTLayerType == MV_GIGE_DEVICE)
    {
        int nIp1 = ((hk_device->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
        int nIp2 = ((hk_device->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
        int nIp3 = ((hk_device->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
        int nIp4 = (hk_device->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);

        // print current ip and user defined name
        printf("CurrentIp: %d.%d.%d.%d\n", nIp1, nIp2, nIp3, nIp4);
        printf("UserDefinedName: %s\n\n", hk_device->SpecialInfo.stGigEInfo.chUserDefinedName);
    }
    else if (hk_device->nTLayerType == MV_USB_DEVICE)
    {
        printf("UserDefinedName: %s\n", hk_device->SpecialInfo.stUsb3VInfo.chUserDefinedName);
        printf("Serial Number: %s\n", hk_device->SpecialInfo.stUsb3VInfo.chSerialNumber);
        printf("Device Number: %d\n\n", hk_device->SpecialInfo.stUsb3VInfo.nDeviceNumber);
    }
    else
    {
        printf("Not support.\n");
    }

    return true;
}

bool hk2cv(MV_FRAME_OUT_INFO_EX* hk_imginfo, unsigned char* data, cv::Mat& src_img) {
    cv::Mat cv_img;
    if (hk_imginfo->enPixelType == PixelType_Gvsp_Mono8) {
        cv_img = cv::Mat(hk_imginfo->nHeight, hk_imginfo->nWidth, CV_8UC1, data);
    }
    else if (hk_imginfo->enPixelType == PixelType_Gvsp_RGB8_Packed) {


        for (unsigned int j = 0; j < hk_imginfo->nHeight; j++) {
            for (unsigned int i = 0; i < hk_imginfo->nWidth; i++) {
                unsigned char red = data[j * (hk_imginfo->nWidth * 3) + i * 3];
                data[j * (hk_imginfo->nWidth * 3) + i * 3] = data[j * (hk_imginfo->nWidth * 3) + i * 3 + 2];
                data[j * (hk_imginfo->nWidth * 3) + i * 3 + 2] = red;
            }
        }
        cv_img = cv::Mat(hk_imginfo->nHeight, hk_imginfo->nWidth, CV_8UC3, data);
    }
    else {
        printf("unsupported pixel format\n");
        return false;
    }

    if (cv_img.data == NULL) {
        return false;
    }
    cv_img.copyTo(src_img);
    return true;
}
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值