海康相机GrabImage

打印 上一主题 下一主题

主题 537|帖子 537|积分 1611

  1. #include <stdio.h>
  2. #include <Windows.h>
  3. #include <process.h>
  4. #include <conio.h>
  5. #include "MvCameraControl.h"
  6. bool g_bExit = false;
  7. // ch:等待按键输入 | en:Wait for key press
  8. void WaitForKeyPress(void)
  9. {
  10.     while(!_kbhit())
  11.     {
  12.         Sleep(10);
  13.     }
  14.     _getch();
  15. }
  16. //相机参数
  17. bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
  18. {
  19.     if (NULL == pstMVDevInfo)
  20.     {
  21.         printf("The Pointer of pstMVDevInfo is NULL!\n");
  22.         return false;
  23.     }
  24.     if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
  25.     {
  26.         int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
  27.         int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
  28.         int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
  29.         int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
  30.         // ch:打印当前相机ip和用户自定义名字 | en:print current ip and user defined name
  31.         printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
  32.         printf("UserDefinedName: %s\n\n" , pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
  33.     }
  34.     else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
  35.     {
  36.         printf("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
  37.         printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chSerialNumber);
  38.         printf("Device Number: %d\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.nDeviceNumber);
  39.     }
  40.     else
  41.     {
  42.         printf("Not support.\n");
  43.     }
  44.     return true;
  45. }
  46. static  unsigned int __stdcall WorkThread(void* pUser)
  47. {
  48.     int nRet = MV_OK;
  49.     MV_FRAME_OUT stOutFrame = {0};
  50.         MV_DISPLAY_FRAME_INFO stDisplayInfo = { 0 };
  51.        
  52.         MV_DISPLAY_FRAME_INFO 成员变量
  53.         //void *  hWnd  //窗口句柄
  54.         //unsigned char *  pData //显示的数据
  55.         //unsigned int  nDataLen//数据长度
  56.         //unsigned short  nWidth//图像宽
  57.         //unsigned short  nHeight//图像高
  58.         //enum MvGvspPixelType  enPixelType//像素格式
  59.         //unsigned int  nRes[4]//保留
  60.         HWND g_hwnd = GetForegroundWindow();//获取当前DOS窗口句柄
  61.     while(true)
  62.     {
  63.         nRet = MV_CC_GetImageBuffer(pUser, &stOutFrame, 1000);
  64.         if (nRet == MV_OK)
  65.         {
  66.             printf("Get Image Buffer: Width[%d], Height[%d], FrameNum[%d]\n",
  67.                 stOutFrame.stFrameInfo.nWidth, stOutFrame.stFrameInfo.nHeight, stOutFrame.stFrameInfo.nFrameNum);
  68.                         //将图片显示在DOS窗口上
  69.                         if (g_hwnd)
  70.                         {
  71.                                 stDisplayInfo.hWnd = g_hwnd;
  72.                                 stDisplayInfo.pData = stOutFrame.pBufAddr;// 指向图像数据的指针
  73.                                 stDisplayInfo.nDataLen = stOutFrame.stFrameInfo.nFrameLen;//数据长度
  74.                                 stDisplayInfo.nWidth = stOutFrame.stFrameInfo.nWidth;   // 图像的宽度(像素)
  75.                                 stDisplayInfo.nHeight = stOutFrame.stFrameInfo.nHeight; // 图像的高度(像素)
  76.                                 stDisplayInfo.enPixelType = stOutFrame.stFrameInfo.enPixelType;  // 像素类型,如黑白、RGB、Bayer等
  77.                                 MV_CC_DisplayOneFrame(pUser, &stDisplayInfo);
  78.                         }
  79.             nRet = MV_CC_FreeImageBuffer(pUser, &stOutFrame);
  80.             if(nRet != MV_OK)
  81.             {
  82.                 printf("Free Image Buffer fail! nRet [0x%x]\n", nRet);
  83.             }
  84.         }
  85.         else
  86.         {
  87.             printf("Get Image fail! nRet [0x%x]\n", nRet);
  88.         }
  89.         if(g_bExit)
  90.         {
  91.             break;
  92.         }
  93.     }
  94.     return 0;
  95. }
  96. int main()
  97. {
  98.     int nRet = MV_OK;
  99.     void* handle = NULL;
  100.     do
  101.     {
  102.         // ch:枚举设备 | en:Enum device
  103.         MV_CC_DEVICE_INFO_LIST stDeviceList;
  104.         memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
  105.         nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &stDeviceList);
  106.         if (MV_OK != nRet)
  107.         {
  108.             printf("Enum Devices fail! nRet [0x%x]\n", nRet);
  109.             break;
  110.         }
  111.         if (stDeviceList.nDeviceNum > 0)
  112.         {
  113.             for (unsigned int i = 0; i < stDeviceList.nDeviceNum; i++)
  114.             {
  115.                 printf("[device %d]:\n", i);
  116.                 MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
  117.                 if (NULL == pDeviceInfo)
  118.                 {
  119.                     break;
  120.                 }
  121.                 PrintDeviceInfo(pDeviceInfo);            
  122.             }  
  123.         }
  124.         else
  125.         {
  126.             printf("Find No Devices!\n");
  127.             break;
  128.         }
  129.         printf("Please Input camera index(0-%d):", stDeviceList.nDeviceNum-1);
  130.         unsigned int nIndex = 0;
  131.         scanf_s("%d", &nIndex);
  132.         if (nIndex >= stDeviceList.nDeviceNum)
  133.         {
  134.             printf("Input error!\n");
  135.             break;
  136.         }
  137.         // ch:选择设备并创建句柄 | en:Select device and create handle
  138.         nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
  139.         if (MV_OK != nRet)
  140.         {
  141.             printf("Create Handle fail! nRet [0x%x]\n", nRet);
  142.             break;
  143.         }
  144.         // ch:打开设备 | en:Open device
  145.         nRet = MV_CC_OpenDevice(handle);
  146.         if (MV_OK != nRet)
  147.         {
  148.             printf("Open Device fail! nRet [0x%x]\n", nRet);
  149.             break;
  150.         }
  151.         // ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
  152.         if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
  153.         {
  154.             int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
  155.             if (nPacketSize > 0)
  156.             {
  157.                 nRet = MV_CC_SetIntValue(handle,"GevSCPSPacketSize",nPacketSize);
  158.                 if(nRet != MV_OK)
  159.                 {
  160.                     printf("Warning: Set Packet Size fail nRet [0x%x]!", nRet);
  161.                 }
  162.             }
  163.             else
  164.             {
  165.                 printf("Warning: Get Packet Size fail nRet [0x%x]!", nPacketSize);
  166.             }
  167.         }
  168.         // ch:设置触发模式为off | en:Set trigger mode as off
  169.         nRet = MV_CC_SetEnumValue(handle, "TriggerMode", 0);
  170.         if (MV_OK != nRet)
  171.         {
  172.             printf("Set Trigger Mode fail! nRet [0x%x]\n", nRet);
  173.             break;
  174.         }
  175.         // ch:开始取流 | en:Start grab image
  176.         nRet = MV_CC_StartGrabbing(handle);
  177.         if (MV_OK != nRet)
  178.         {
  179.             printf("Start Grabbing fail! nRet [0x%x]\n", nRet);
  180.             break;
  181.         }
  182.         unsigned int nThreadID = 0;
  183.         void* hThreadHandle = (void*) _beginthreadex( NULL , 0 , WorkThread , handle, 0 , &nThreadID );
  184.         if (NULL == hThreadHandle)
  185.         {
  186.             break;
  187.         }
  188.                 //在C/C++编程中,_beginthreadex 是一个用于在Microsoft Visual Studio环境下创建线程的函数。
  189.                 //这个函数定义在<process.h>头文件中,它允许你在一个独立的线程中启动一个新的执行路径。下面是对给定代码行的解释:
  190.                 //NULL:这是线程属性指针,通常设置为NULL表示使用默认属性。
  191.                 //0:这是线程堆栈大小。如果设置为0,那么系统会根据默认值或编译器设置来决定堆栈大小。
  192.                 //WorkThread:这是指向线程函数的指针,当线程开始运行时,将从这里开始执行。
  193.                 //handle:这是传递给WorkThread函数的参数。当WorkThread被调用时,它的第一个参数将接收这个值。
  194.                 //0:这是线程的优先级增量。通常设置为0,意味着线程将具有默认的优先级。
  195.                 //&nThreadID:这是一个指向整型变量的指针,_beginthreadex函数将返回创建的新线程的ID到这个变量中。
  196.         printf("Press a key to stop grabbing.\n");
  197.         WaitForKeyPress();
  198.         g_bExit = true;
  199.         Sleep(1000);
  200.         // ch:停止取流 | en:Stop grab image
  201.         nRet = MV_CC_StopGrabbing(handle);
  202.         if (MV_OK != nRet)
  203.         {
  204.             printf("Stop Grabbing fail! nRet [0x%x]\n", nRet);
  205.             break;
  206.         }
  207.         // ch:关闭设备 | Close device
  208.         nRet = MV_CC_CloseDevice(handle);
  209.         if (MV_OK != nRet)
  210.         {
  211.             printf("ClosDevice fail! nRet [0x%x]\n", nRet);
  212.             break;
  213.         }
  214.         // ch:销毁句柄 | Destroy handle
  215.         nRet = MV_CC_DestroyHandle(handle);
  216.         if (MV_OK != nRet)
  217.         {
  218.             printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
  219.             break;
  220.         }
  221.     } while (0);
  222.     if (nRet != MV_OK)
  223.     {
  224.         if (handle != NULL)
  225.         {
  226.             MV_CC_DestroyHandle(handle);
  227.             handle = NULL;
  228.         }
  229.     }
  230.     printf("Press a key to exit.\n");
  231.     WaitForKeyPress();
  232.     return 0;
  233. }
复制代码


本代码将会把工业相机拍摄图像显示在DOS窗体上
本文仅供学习交流,严禁用于商业用途,如本文涉及侵权请及时接洽将于24小时内删除

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

悠扬随风

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表