当前位置:   article > 正文

C# 上位机之海康相机开发(SDK)_c#海康sdk

c#海康sdk

一 前言

发现工作中好多计算机视觉上位机项目都用海康相机,为了能够更好的学习和工作,我自己依据同事的源码和网上的一些总结编写本博客。通过本次学习,让我明白一点,无论学习什么技术都要学会自己看技术文档,而不是第一时间上网找源码。以工业相机SDK使用说明.chm为例,如何检索相机,打开相机,取流,抓取,保存图片,关闭相机,释放资源都有详细的介绍。

二 海康相机使用流程

以下部分图片引用自工业相机SDK使用手册 v2.4.1,仅作为学习使用,以下简称使用手册

这里要说明以下几点

1 这个接口流程是C接口流程,除了C接口流程以外还有C++接口流程,有朋友可能会问为什么标题是C#工业相机操作,这里却介绍C接口。实际上通过DoNet源码可以看出虽然方法名以_NET结尾,但是方法的实现引用了C接口,如下图所示

2 可能随着使用手册版本的更新导致变量名,方法名等发生小幅度改变,掌握方法是关键

3 由上述流程图可以看出使用相机的流程分为:查找子网内相机设备->创建管理相机的句柄->打开设备->获取数据包->数据包处理->关闭设备->释放资源

三 相机操作

3.1 检索网络设备

3.1.1 枚举相机类型

示例代码如下

  1. using System;
  2. using System.IO;
  3. using MvCamCtrl.NET;
  4. int nTransLayers = MyCamera.MV_CC_EnumerateTls_NET();
  5. if ((nTransLayers & MyCamera.MV_GIGE_DEVICE) == MyCamera.MV_GIGE_DEVICE)
  6. {
  7. Console.WriteLine("MV_GIGE_DEVICE");
  8. }
3.1.2 枚举相机信息       

        大部分情况是不需要检索相机类型的,相机是你根据项目需求买的,你应该知道它是什么相机。而且相机通过网线与计算机联系,通过配置IP就可以知道是正在对哪台相机操作。所以正常情是直接从网络中枚举相机。也就是如下的方法

注意:此枚举方法的第二个参数类型是MV_CC_DEVICE_INFO_LIST 相机设备信息列表,以下分别是使用手册中的数据类型定义和VS中的数据类型定义。

       可以看到VS源码中多了一行代码[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)],这里的MarshalAs在VS里官方的注释是指示如何在托管代码与非托管代码之间封送数据,那么有牵扯了两个名词。通过百度百科得到以下总结

托管代码与非托管代码
名称释义与区别
托管代码托管代码 (managed code)同受管制的代码,由公共语言运行库环境(而不是直接由操作系统)执行的代码。托管代码应用程序可以获得公共语言运行库服务,例如自动垃圾回收、运行库类型检查和安全支持等。这些服务帮助提供独立于平台和语言的、统一的托管代码应用程序行为。
非托管代码公共语言运行库环境的外部,由操作系统直接执行的代码托管代码必须提供自己的垃圾回收、类型检查、安全支持等服务,它与托管代码不同,后者从公共语言运行库中获得这些服务,而非托管代码是在运行库之外运行的代码。例如COM 组件、ActiveX 接口和 Win32 API 函数都是非托管代码的示例。

        通过VS追踪MarshalAS可以看出其来自public sealed class MarshalAsAttribute : Attribute这个类中的方法。至于方法内部实现暂时不是我们需要考虑的,我们需要知道以下几点(引用自MarshalAs的简单总结-CSDN博客)

[MarshalAs(UnmanagedType unmanagedType, 命名参数)]

        实际上相当于构造一个MarshalAsAttribute类的对象

        常用的UnmanagedType枚举值:

        BStr   长度前缀为双字节的 Unicode 字符串;

        LPStr  单字节、空终止的 ANSI 字符串。;

        LPWStr  一个 2 字节、空终止的 Unicode 字符串;

        ByValArray 用于在结构中出现的内联定长字符数组,应始终使用MarshalAsAttribute的                    SizeConst字段来指示数组的大小。   

注意:

在用Marshal.SizeOf(),即获取对象的非托管大小时,获得的是自己定义的大小;

但在实际处理的时候,是按照实际的大小来获取的。

接下来介绍相关相机信息数据类型,注意这几个结构体在使用手册查找的时候要到索引中找

那么到底如何进行网络检索相机设备呢
  1. /*虽然系统有专门对相机信息列表数据类型的定义,但是我们最好还是自定义一个相机信息数据类
  2. 为什么要专门定义一个类呢,原因如下
  3. 1 MV_CC_DEVICE_INFO通用结构体转化为专用的相机结构体中对于相机IP的并不是192.168.*.*的形式,需要们自行偏移获取
  4. 如MV_CC_DEVICE_INFO->MV_GIGE_DEVICE_INFO
  5. MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(device.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO));
  6. UInt32 nNetIp1 = (gigeInfo.nNetExport & 0xFF000000) >> 24;
  7. UInt32 nNetIp2 = (gigeInfo.nNetExport & 0x00FF0000) >> 16;
  8. UInt32 nNetIp3 = (gigeInfo.nNetExport & 0x0000FF00) >> 8;
  9. UInt32 nNetIp4 = (gigeInfo.nNetExport & 0x000000FF);
  10. // ch:显示IP | en:Display IP
  11. UInt32 nIp1 = (gigeInfo.nCurrentIp & 0xFF000000) >> 24;
  12. UInt32 nIp2 = (gigeInfo.nCurrentIp & 0x00FF0000) >> 16;
  13. UInt32 nIp3 = (gigeInfo.nCurrentIp & 0x0000FF00) >> 8;
  14. UInt32 nIp4 = (gigeInfo.nCurrentIp & 0x000000FF);
  15. */
  16. //定义一个相机信息类 这个类定义的位置建议在一个全局模块中
  17. public class CameraInfor
  18. {
  19. public string userDefinedName { get; set; } //用户定义的名字
  20. public string serialNumber { get; set; } //序列号
  21. public string deviceIPAdd { get; set; } //设备IP地址
  22. public string manufacturerName { get; set; }//制造厂商名
  23. public string modelName { get; set; } //型号名称
  24. public string deviceType { get; set; } //型号名称
  25. public IntPtr pDeviceInfo; //相机句柄
  26. public float ExposureTime { get; set; } //曝光时间
  27. public float Gain { get; set; } //增益
  28. public float ResultingFrameRate { get; set; } //帧率
  29. public bool isOpenTriggerMode { get; set; } //是否打开触发方式
  30. }
  31. //*********************************************************************
  32. //相机模块部分
  33. //创建一个全局的相机字典,IP为键,相机信息(CameraInfor)为值
  34. public static Dictionary<string, CameraInfor> mapOfGigeCameras = new Dictionary<string, CameraInfor>();
  35. //创建一个全局的相机对象来保存当前正在使用的相机信息
  36. private CameraInfor currentCameraInfor;
  37. //根据相机枚举方法参数声明变量
  38. MyCamera.MV_CC_DEVICE_INFO_LIST m_stDeviceList = new MyCamera.MV_CC_DEVICE_INFO_LIST();
  39. //创建一个相机对象 注意只是创建了一个相机对象,并没有指定管理哪个相机
  40. private MyCamera m_MyCamera = new MyCamera();
  41. //回调函数声明 用来处理拍照
  42. public delegate void Process(Mat src);
  43. public Process callback;
  44. //抓图标志
  45. public bool m_bGrabbing=false;
  46. Thread m_hReceiveThread = null;
  47. MyCamera.MV_FRAME_OUT_INFO_EX m_stFrameInfo = new MyCamera.MV_FRAME_OUT_INFO_EX();
  48. // ch:用于从驱动获取图像的缓存
  49. UInt32 m_nBufSizeForDriver = 0;
  50. IntPtr m_BufForDriver = IntPtr.Zero;
  51. private static Object BufForDriverLock = new Object();
  52. //封装枚举相机的方法
  53. public void enumDevice()
  54. {
  55. System.GC.Collect();//强制对所有代进行及时垃圾回收
  56. m_stDeviceList.nDeviceNum = 0; //初始化信息列表
  57. int nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref m_stDeviceList);
  58. if (0 != nRet)
  59. {
  60. return;
  61. }
  62. mapOfGigeCameras.Clear(); //清空设备列表
  63. CameraInfor cameraInfor ;
  64. for (int i = 0; i < m_stDeviceList.nDeviceNum; i++)
  65. {
  66. MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_stDeviceList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO));//获取第i个相机句柄的相机信息
  67. cameraInfor = new CameraInfor();
  68. cameraInfor.pDeviceInfo = m_stDeviceList.pDeviceInfo[i];//获取相机句柄
  69. if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
  70. {
  71. MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(device.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO));//将通用相机信息结构体转化为Gige相机结构体
  72. UInt32 nNetIp1 = (gigeInfo.nNetExport & 0xFF000000) >> 24;
  73. UInt32 nNetIp2 = (gigeInfo.nNetExport & 0x00FF0000) >> 16;
  74. UInt32 nNetIp3 = (gigeInfo.nNetExport & 0x0000FF00) >> 8;
  75. UInt32 nNetIp4 = (gigeInfo.nNetExport & 0x000000FF);
  76. // ch:显示IP | en:Display IP
  77. UInt32 nIp1 = (gigeInfo.nCurrentIp & 0xFF000000) >> 24;
  78. UInt32 nIp2 = (gigeInfo.nCurrentIp & 0x00FF0000) >> 16;
  79. UInt32 nIp3 = (gigeInfo.nCurrentIp & 0x0000FF00) >> 8;
  80. UInt32 nIp4 = (gigeInfo.nCurrentIp & 0x000000FF);
  81. cameraInfor.serialNumber = gigeInfo.chSerialNumber;
  82. cameraInfor.userDefinedName = gigeInfo.chUserDefinedName;
  83. cameraInfor.manufacturerName = gigeInfo.chManufacturerName;
  84. cameraInfor.modelName = gigeInfo.chModelName;
  85. cameraInfor.deviceType = "GEV"; //千兆网工业相机技术之GigE Vision(GEV)
  86. cameraInfor.deviceIPAdd = nIp1.ToString() + "." + nIp2.ToString() + "." + nIp3.ToString() + "." + nIp4.ToString();
  87. mapOfGigeCameras.Add(cameraInfor.deviceIPAdd, cameraInfor);
  88. }
  89. else if (device.nTLayerType == MyCamera.MV_USB_DEVICE)
  90. {
  91. MyCamera.MV_USB3_DEVICE_INFO usbInfo = (MyCamera.MV_USB3_DEVICE_INFO)MyCamera.ByteToStruct(device.SpecialInfo.stUsb3VInfo, typeof(MyCamera.MV_USB3_DEVICE_INFO));
  92. //本文主要讲解Gige相机的使用,实际工作中可以根据需要编写此部分代码
  93. }
  94. }
  95. }

3.2 打开相机

3.2.1 打开相机的流程:

        1  创建管理相机的句柄:通过创建句柄,指定我们之前创建的相机对象管理哪个相机

        2  打开相机对象

3.2.2 打开相机的相关方法

3.2.3 打开相机操作
  1. public void OpenDeviceByIP(string deviceIp)
  2. {
  3. if (m_stDeviceList.nDeviceNum == 0 || deviceIp=="")//枚举设备个数为0或者传入IP为空
  4. {
  5. return;
  6. }
  7. currentCameraInfor = mapOfGigeCameras[deviceIp];//之前创建的全局当前相机信息用于获取指定IP的相机
  8. MyCamera.MV_CC_DEVICE_INFO device =(MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(mapOfGigeCameras[deviceIp].pDeviceInfo,typeof(MyCamera.MV_CC_DEVICE_INFO));//IP索引到指定相机句柄的信息
  9. if (null == m_MyCamera) //判断之前创建的全局相机对象是否创建成功
  10. {
  11. m_MyCamera = new MyCamera();
  12. if (null == m_MyCamera)
  13. {
  14. return;
  15. }
  16. }
  17. int nRet = m_MyCamera.MV_CC_CreateDevice_NET(ref device);//指定对象管理哪一个相机
  18. if (MyCamera.MV_OK != nRet)
  19. {
  20. return;
  21. }
  22. nRet = m_MyCamera.MV_CC_OpenDevice_NET();//打开对象管理的相机
  23. if (MyCamera.MV_OK != nRet)
  24. {
  25. m_MyCamera.MV_CC_DestroyDevice_NET();
  26. return;
  27. }
  28. // ch:探测网络最佳包大小(只对GigE相机有效)
  29. /*这里介绍一下相关方法 当我们要获取或者设置相机对象参数的时候注意查看使用手册中对应的参数信息
  30. 1 目标参数是什么数据类型
  31. 2 调用对应的获取整形,浮点型的方法通过参数名获取
  32. 后面详细介绍
  33. */
  34. if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
  35. {
  36. int nPacketSize = m_MyCamera.MV_CC_GetOptimalPacketSize_NET();
  37. if (nPacketSize > 0)
  38. {
  39. nRet = m_MyCamera.MV_CC_SetIntValue_NET("GevSCPSPacketSize", (uint)nPacketSize);//设置网络包大小
  40. if (nRet != MyCamera.MV_OK)
  41. {
  42. }
  43. }
  44. else
  45. {
  46. }
  47. }
  48. // ch:设置采集连续模式
  49. m_MyCamera.MV_CC_SetEnumValue_NET("AcquisitionMode", (uint)MyCamera.MV_CAM_ACQUISITION_MODE.MV_ACQ_MODE_CONTINUOUS);//采集模式:单帧,多帧,连续
  50. m_MyCamera.MV_CC_SetEnumValue_NET("TriggerMode", (uint)MyCamera.MV_CAM_TRIGGER_MODE.MV_TRIGGER_MODE_OFF);//触发模式 0-关 1-开
  51. // ch:获取参数
  52. MyCamera.MVCC_FLOATVALUE stParam = new MyCamera.MVCC_FLOATVALUE();
  53. nRet = m_MyCamera.MV_CC_GetFloatValue_NET("ExposureTime", ref stParam);//获取曝光时间
  54. if (MyCamera.MV_OK == nRet)
  55. {
  56. mapOfGigeCameras[deviceIp].ExposureTime = stParam.fCurValue;
  57. }
  58. nRet = m_MyCamera.MV_CC_GetFloatValue_NET("Gain", ref stParam);//获取相机增益
  59. if (MyCamera.MV_OK == nRet)
  60. {
  61. mapOfGigeCameras[deviceIp].Gain = stParam.fCurValue;
  62. }
  63. nRet = m_MyCamera.MV_CC_GetFloatValue_NET("ResultingFrameRate", ref stParam);//采集帧率
  64. if (MyCamera.MV_OK == nRet)
  65. {
  66. mapOfGigeCameras[deviceIp].ResultingFrameRate = stParam.fCurValue;
  67. }
  68. }

3.3. 拍照

3.3.1 拍照流程

          1 采集图像

          2 取流抓图

          3 保存图片

          4 释放流数据

3.3.2 相关方法与数据类型

MV_FRAME_OUT在使用手册中并没有找到但是在VS中反编译中有源码,其中pBufAddr是帧输出结果在内存中的地址

3.3.3 相关操作
  1. public void CaptureFile(Process proc) //Process为委托数据类型 之前在全局代码中生命
  2. {
  3. this.callback = proc;
  4. m_bGrabbing = true;
  5. m_hReceiveThread = new Thread(ReceiveThreadProcess);
  6. m_hReceiveThread.Start();
  7. m_stFrameInfo.nFrameLen = 0;//取流之前先清除帧长度
  8. m_stFrameInfo.enPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_Undefined;
  9. // ch:开始采集
  10. int nRet = m_MyCamera.MV_CC_StartGrabbing_NET();
  11. if (MyCamera.MV_OK != nRet)
  12. {
  13. m_bGrabbing = false;
  14. m_hReceiveThread.Join();
  15. return;
  16. }
  17. }
  18. public void ReceiveThreadProcess()
  19. {
  20. MyCamera.MV_FRAME_OUT stFrameInfo = new MyCamera.MV_FRAME_OUT();//定义输出的帧信息结构体
  21. MyCamera.MV_DISPLAY_FRAME_INFO stDisplayInfo = new MyCamera.MV_DISPLAY_FRAME_INFO();
  22. int nRet = MyCamera.MV_OK;
  23. while (m_bGrabbing)
  24. {
  25. nRet = m_MyCamera.MV_CC_GetImageBuffer_NET(ref stFrameInfo, 1000);//获取一帧图片 并将图片信息保留在stFrameInfo中,超时时间设置为1秒 加的这一秒就是等待上一个线程采集完毕
  26. if (nRet == MyCamera.MV_OK)
  27. {
  28. lock (BufForDriverLock) //同步互斥操作
  29. {
  30. if (m_BufForDriver == IntPtr.Zero || stFrameInfo.stFrameInfo.nFrameLen > m_nBufSizeForDriver) //帧长度大于0
  31. {
  32. if (m_BufForDriver != IntPtr.Zero)
  33. {
  34. Marshal.Release(m_BufForDriver);
  35. m_BufForDriver = IntPtr.Zero;
  36. }
  37. m_BufForDriver = Marshal.AllocHGlobal((Int32)stFrameInfo.stFrameInfo.nFrameLen);//申请数据流大小的内存
  38. if (m_BufForDriver == IntPtr.Zero)//申请失败
  39. {
  40. return;
  41. }
  42. m_nBufSizeForDriver = stFrameInfo.stFrameInfo.nFrameLen;//帧长度赋值
  43. }
  44. m_stFrameInfo = stFrameInfo.stFrameInfo;//帧信息
  45. CopyMemory(m_BufForDriver, stFrameInfo.pBufAddr, stFrameInfo.stFrameInfo.nFrameLen);//到内存指定的地址去获取帧数据
  46. }
  47. if (RemoveCustomPixelFormats(stFrameInfo.stFrameInfo.enPixelType))
  48. {
  49. m_MyCamera.MV_CC_FreeImageBuffer_NET(ref stFrameInfo);
  50. continue;
  51. }
  52. stDisplayInfo.hWnd = hWnd_pictureBox;
  53. stDisplayInfo.pData = stFrameInfo.pBufAddr;
  54. stDisplayInfo.nDataLen = stFrameInfo.stFrameInfo.nFrameLen;
  55. stDisplayInfo.nWidth = stFrameInfo.stFrameInfo.nWidth;
  56. stDisplayInfo.nHeight = stFrameInfo.stFrameInfo.nHeight;
  57. stDisplayInfo.enPixelType = stFrameInfo.stFrameInfo.enPixelType;//像素格式
  58. if (isDisplayOnPictureBox)//初始化完成之后
  59. {
  60. m_MyCamera.MV_CC_DisplayOneFrame_NET(ref stDisplayInfo);//显示一帧图像 将这帧图像的信息保存在变量中
  61. }
  62. Mat dst1 = new Mat(stDisplayInfo.nHeight, stDisplayInfo.nWidth,MatType.CV_8UC3, m_BufForDriver);
  63. callback(dst1); //回调函数对图像进行相应的处理操作
  64. m_MyCamera.MV_CC_FreeImageBuffer_NET(ref stFrameInfo);
  65. }
  66. else
  67. {
  68. if (currentCameraInfor.isOpenTriggerMode)
  69. {
  70. Thread.Sleep(5);
  71. }
  72. }
  73. }
  74. }
  75. //去除自定义的像素格式
  76. private bool RemoveCustomPixelFormats(MyCamera.MvGvspPixelType enPixelFormat)
  77. {
  78. Int32 nResult = ((int)enPixelFormat) & (unchecked((Int32)0x80000000));
  79. if (0x80000000 == nResult) //错误或者无效句柄
  80. {
  81. return true;
  82. }
  83. else
  84. {
  85. return false;
  86. }
  87. }
  88. public void SaveJPG(string path)
  89. {
  90. MyCamera.MV_SAVE_IMG_TO_FILE_PARAM stSaveFileParam = new MyCamera.MV_SAVE_IMG_TO_FILE_PARAM();
  91. if (m_stFrameInfo.nFrameLen == 0)
  92. {
  93. return;
  94. }
  95. stSaveFileParam.enImageType = MyCamera.MV_SAVE_IAMGE_TYPE.MV_Image_Jpeg;
  96. stSaveFileParam.enPixelType = m_stFrameInfo.enPixelType;
  97. stSaveFileParam.pData = m_BufForDriver;
  98. stSaveFileParam.nDataLen = m_stFrameInfo.nFrameLen;
  99. stSaveFileParam.nHeight = m_stFrameInfo.nHeight;
  100. stSaveFileParam.nWidth = m_stFrameInfo.nWidth;
  101. stSaveFileParam.nQuality = 80;
  102. stSaveFileParam.iMethodValue = 2;
  103. if (string.IsNullOrEmpty(path))
  104. stSaveFileParam.pImagePath = System.Environment.CurrentDirectory + "\\Image\\" + picSavePath + "Image_w" + stSaveFileParam.nWidth.ToString() + "_h" + stSaveFileParam.nHeight.ToString() + "_fn" + m_stFrameInfo.nFrameNum.ToString() + ".jpg";
  105. else
  106. {
  107. stSaveFileParam.pImagePath = path;
  108. }
  109. int nRet = m_MyCamera.MV_CC_SaveImageToFile_NET(ref stSaveFileParam);
  110. //Mat dst1 = new Mat(stSaveFileParam.nHeight, stSaveFileParam.nWidth,MatType.CV_8UC3, m_BufForDriver);
  111. //dst1.SaveImage("D:\\111.jpg");
  112. if (MyCamera.MV_OK != nRet)
  113. {
  114. return;
  115. }
  116. }

4 关闭相机

  1. public void CloseDevice()
  2. {
  3. // ch:取流标志位清零
  4. if (m_BufForDriver != IntPtr.Zero) //如果用于存储流数据的内存不为空则清空
  5. {
  6. Marshal.Release(m_BufForDriver);
  7. }
  8. // ch:关闭设备
  9. m_MyCamera.MV_CC_CloseDevice_NET();
  10. m_MyCamera.MV_CC_DestroyDevice_NET();
  11. }

5 完整相机模块 里面还有其他模块的数据类型声明但是本博客不在扩展

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using MvCamCtrl;
  9. using MvCamCtrl.NET;
  10. using OpenCvSharp;
  11. namespace GH
  12. {
  13. public class HKCameraHelper
  14. {
  15. [DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)] //非重叠区域的拷贝CopyMemory 完全拷贝MoveMemory
  16. public static extern void CopyMemory(IntPtr dest, IntPtr src, uint count); //复制内存块的目的地址,复制内存块的源地址 复制大小(单位字节)
  17. /// <summary>
  18. /// 枚举出来的子网信息,都在相机结构体 MV_CC_DEVICE_INFO_LIST 里面
  19. /// </summary>
  20. MyCamera.MV_CC_DEVICE_INFO_LIST m_stDeviceList = new MyCamera.MV_CC_DEVICE_INFO_LIST();
  21. private MyCamera m_MyCamera = new MyCamera();
  22. bool m_bGrabbing = false;
  23. Thread m_hReceiveThread = null;
  24. MyCamera.MV_FRAME_OUT_INFO_EX m_stFrameInfo = new MyCamera.MV_FRAME_OUT_INFO_EX();
  25. // ch:用于从驱动获取图像的缓存 | en:Buffer for getting image from driver
  26. UInt32 m_nBufSizeForDriver = 0;
  27. IntPtr m_BufForDriver = IntPtr.Zero;
  28. private static Object BufForDriverLock = new Object();
  29. //枚举所有相机设备列表
  30. public static Dictionary<string, CameraInfor> mapOfCameraDevice = new Dictionary<string, CameraInfor>();
  31. //保存获取的当前相机
  32. private CameraInfor currentCameraInfor;
  33. public string picSavePath { get; set; }
  34. public IntPtr hWnd_pictureBox { get; set; }
  35. public ASCameraParamInfor aSCameraParamInfor { get; set; } //相机参数
  36. //回调函数声明
  37. public delegate void ProcessBussess(Mat src);
  38. public ProcessBussess callback;
  39. public bool isDisplayOnPictureBox { get; set; }
  40. //获取相机设备列表
  41. public void DeviceListAcq()
  42. {
  43. // ch:创建设备列表 | en:Create Device List
  44. System.GC.Collect();
  45. m_stDeviceList.nDeviceNum = 0;
  46. int nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref m_stDeviceList);
  47. if (0 != nRet)
  48. {
  49. return;
  50. }
  51. mapOfCameraDevice.Clear(); //清空设备列表
  52. CameraInfor cameraInfor ;
  53. // ch:在窗体列表中显示设备名 | en:Display device name in the form list
  54. for (int i = 0; i < m_stDeviceList.nDeviceNum; i++)
  55. {
  56. MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_stDeviceList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO));
  57. cameraInfor = new CameraInfor();
  58. cameraInfor.pDeviceInfo = m_stDeviceList.pDeviceInfo[i];
  59. if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
  60. {
  61. MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(device.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO));
  62. UInt32 nNetIp1 = (gigeInfo.nNetExport & 0xFF000000) >> 24;
  63. UInt32 nNetIp2 = (gigeInfo.nNetExport & 0x00FF0000) >> 16;
  64. UInt32 nNetIp3 = (gigeInfo.nNetExport & 0x0000FF00) >> 8;
  65. UInt32 nNetIp4 = (gigeInfo.nNetExport & 0x000000FF);
  66. // ch:显示IP | en:Display IP
  67. UInt32 nIp1 = (gigeInfo.nCurrentIp & 0xFF000000) >> 24;
  68. UInt32 nIp2 = (gigeInfo.nCurrentIp & 0x00FF0000) >> 16;
  69. UInt32 nIp3 = (gigeInfo.nCurrentIp & 0x0000FF00) >> 8;
  70. UInt32 nIp4 = (gigeInfo.nCurrentIp & 0x000000FF);
  71. cameraInfor.serialNumber = gigeInfo.chSerialNumber;
  72. cameraInfor.userDefinedName = gigeInfo.chUserDefinedName;
  73. cameraInfor.manufacturerName = gigeInfo.chManufacturerName;
  74. cameraInfor.modelName = gigeInfo.chModelName;
  75. cameraInfor.deviceType = "GEV"; //千兆网工业相机技术之GigE Vision(GEV)
  76. cameraInfor.deviceIPAdd = nIp1.ToString() + "." + nIp2.ToString() + "." + nIp3.ToString() + "." + nIp4.ToString();
  77. mapOfCameraDevice.Add(cameraInfor.deviceIPAdd, cameraInfor);
  78. }
  79. else if (device.nTLayerType == MyCamera.MV_USB_DEVICE)
  80. {
  81. MyCamera.MV_USB3_DEVICE_INFO usbInfo = (MyCamera.MV_USB3_DEVICE_INFO)MyCamera.ByteToStruct(device.SpecialInfo.stUsb3VInfo, typeof(MyCamera.MV_USB3_DEVICE_INFO));
  82. cameraInfor.serialNumber = usbInfo.chSerialNumber;
  83. cameraInfor.userDefinedName = usbInfo.chUserDefinedName;
  84. cameraInfor.manufacturerName = usbInfo.chManufacturerName;
  85. cameraInfor.modelName = usbInfo.chModelName;
  86. cameraInfor.deviceType = "U3V";
  87. mapOfCameraDevice.Add(cameraInfor.deviceIPAdd, cameraInfor);
  88. }
  89. }
  90. }
  91. //打开相机
  92. public void OpenDeviceByIPAdd(string deviceIpAdd)
  93. {
  94. if (m_stDeviceList.nDeviceNum == 0 || deviceIpAdd=="")
  95. {
  96. return;
  97. }
  98. currentCameraInfor = mapOfCameraDevice[deviceIpAdd];
  99. // ch:获取选择的设备信息 | en:Get selected device information
  100. MyCamera.MV_CC_DEVICE_INFO device =(MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(mapOfCameraDevice[deviceIpAdd].pDeviceInfo,
  101. typeof(MyCamera.MV_CC_DEVICE_INFO));
  102. // ch:打开设备 | en:Open device
  103. if (null == m_MyCamera)
  104. {
  105. m_MyCamera = new MyCamera();
  106. if (null == m_MyCamera)
  107. {
  108. return;
  109. }
  110. }
  111. int nRet = m_MyCamera.MV_CC_CreateDevice_NET(ref device);
  112. if (MyCamera.MV_OK != nRet)
  113. {
  114. return;
  115. }
  116. nRet = m_MyCamera.MV_CC_OpenDevice_NET();
  117. if (MyCamera.MV_OK != nRet)
  118. {
  119. m_MyCamera.MV_CC_DestroyDevice_NET();
  120. return;
  121. }
  122. // ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
  123. if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
  124. {
  125. int nPacketSize = m_MyCamera.MV_CC_GetOptimalPacketSize_NET();
  126. if (nPacketSize > 0)
  127. {
  128. nRet = m_MyCamera.MV_CC_SetIntValue_NET("GevSCPSPacketSize", (uint)nPacketSize);
  129. if (nRet != MyCamera.MV_OK)
  130. {
  131. }
  132. }
  133. else
  134. {
  135. }
  136. }
  137. // ch:设置采集连续模式 | en:Set Continues Aquisition Mode
  138. m_MyCamera.MV_CC_SetEnumValue_NET("AcquisitionMode", (uint)MyCamera.MV_CAM_ACQUISITION_MODE.MV_ACQ_MODE_CONTINUOUS);
  139. m_MyCamera.MV_CC_SetEnumValue_NET("TriggerMode", (uint)MyCamera.MV_CAM_TRIGGER_MODE.MV_TRIGGER_MODE_OFF);
  140. // ch:获取参数 | en:Get parameters
  141. MyCamera.MVCC_FLOATVALUE stParam = new MyCamera.MVCC_FLOATVALUE();
  142. nRet = m_MyCamera.MV_CC_GetFloatValue_NET("ExposureTime", ref stParam);
  143. if (MyCamera.MV_OK == nRet)
  144. {
  145. mapOfCameraDevice[deviceIpAdd].ExposureTime = stParam.fCurValue;
  146. }
  147. nRet = m_MyCamera.MV_CC_GetFloatValue_NET("Gain", ref stParam);
  148. if (MyCamera.MV_OK == nRet)
  149. {
  150. mapOfCameraDevice[deviceIpAdd].Gain = stParam.fCurValue;
  151. }
  152. nRet = m_MyCamera.MV_CC_GetFloatValue_NET("ResultingFrameRate", ref stParam);
  153. if (MyCamera.MV_OK == nRet)
  154. {
  155. mapOfCameraDevice[deviceIpAdd].ResultingFrameRate = stParam.fCurValue;
  156. }
  157. // ch:控件操作 | en:Control operation
  158. }
  159. //关闭设备
  160. public void CloseDevice()
  161. {
  162. // ch:取流标志位清零 | en:Reset flow flag bit
  163. if (m_bGrabbing == true)
  164. {
  165. m_bGrabbing = false;
  166. m_hReceiveThread.Join();
  167. }
  168. if (m_BufForDriver != IntPtr.Zero)
  169. {
  170. Marshal.Release(m_BufForDriver);
  171. }
  172. // ch:关闭设备 | en:Close Device
  173. m_MyCamera.MV_CC_CloseDevice_NET();
  174. m_MyCamera.MV_CC_DestroyDevice_NET();
  175. }
  176. //设置为连续模式,关闭触发方式
  177. public void SetContinuesMode()
  178. {
  179. m_MyCamera.MV_CC_SetEnumValue_NET("TriggerMode", (uint)MyCamera.MV_CAM_TRIGGER_MODE.MV_TRIGGER_MODE_OFF);
  180. currentCameraInfor.isOpenTriggerMode = false;
  181. }
  182. public void OpenTiggerMode()
  183. {
  184. m_MyCamera.MV_CC_SetEnumValue_NET("TriggerMode", (uint)MyCamera.MV_CAM_TRIGGER_MODE.MV_TRIGGER_MODE_ON);
  185. currentCameraInfor.isOpenTriggerMode = true;
  186. }
  187. public void SetTiggerMode(int tiggerMode)
  188. {
  189. m_MyCamera.MV_CC_SetEnumValue_NET("TriggerMode", (uint)MyCamera.MV_CAM_TRIGGER_MODE.MV_TRIGGER_MODE_ON);
  190. currentCameraInfor.isOpenTriggerMode = true;
  191. // ch:触发源选择:0 - Line0; | en:Trigger source select:0 - Line0;
  192. // 1 - Line1;
  193. // 2 - Line2;
  194. // 3 - Line3;
  195. // 4 - Counter;
  196. // 7 - Software;
  197. m_MyCamera.MV_CC_SetEnumValue_NET("TriggerSource",(uint) tiggerMode);
  198. }
  199. public void CaptureFile(ProcessBussess proc)
  200. {
  201. this.callback = proc;
  202. m_bGrabbing = true;
  203. m_hReceiveThread = new Thread(ReceiveThreadProcess);
  204. m_hReceiveThread.Start();
  205. m_stFrameInfo.nFrameLen = 0;//取流之前先清除帧长度
  206. m_stFrameInfo.enPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_Undefined;
  207. // ch:开始采集 | en:Start Grabbing
  208. int nRet = m_MyCamera.MV_CC_StartGrabbing_NET();
  209. if (MyCamera.MV_OK != nRet)
  210. {
  211. m_bGrabbing = false;
  212. m_hReceiveThread.Join();
  213. return;
  214. }
  215. }
  216. public void ReceiveThreadProcess()
  217. {
  218. MyCamera.MV_FRAME_OUT stFrameInfo = new MyCamera.MV_FRAME_OUT();
  219. MyCamera.MV_DISPLAY_FRAME_INFO stDisplayInfo = new MyCamera.MV_DISPLAY_FRAME_INFO();
  220. int nRet = MyCamera.MV_OK;
  221. while (m_bGrabbing)
  222. {
  223. nRet = m_MyCamera.MV_CC_GetImageBuffer_NET(ref stFrameInfo, 1000);//获取一帧图片 并将图片信息保留在stFrameInfo中,超时时间设置为1秒 加的这一秒就是等待上一个线程抓取完毕
  224. if (nRet == MyCamera.MV_OK)
  225. {
  226. lock (BufForDriverLock) //同步互斥操作
  227. {
  228. if (m_BufForDriver == IntPtr.Zero || stFrameInfo.stFrameInfo.nFrameLen > m_nBufSizeForDriver) //帧长度大于0
  229. {
  230. if (m_BufForDriver != IntPtr.Zero)
  231. {
  232. Marshal.Release(m_BufForDriver);
  233. m_BufForDriver = IntPtr.Zero;
  234. }
  235. m_BufForDriver = Marshal.AllocHGlobal((Int32)stFrameInfo.stFrameInfo.nFrameLen);
  236. if (m_BufForDriver == IntPtr.Zero)
  237. {
  238. return;
  239. }
  240. m_nBufSizeForDriver = stFrameInfo.stFrameInfo.nFrameLen;
  241. }
  242. m_stFrameInfo = stFrameInfo.stFrameInfo;
  243. CopyMemory(m_BufForDriver, stFrameInfo.pBufAddr, stFrameInfo.stFrameInfo.nFrameLen);
  244. }
  245. if (RemoveCustomPixelFormats(stFrameInfo.stFrameInfo.enPixelType))
  246. {
  247. m_MyCamera.MV_CC_FreeImageBuffer_NET(ref stFrameInfo);
  248. continue;
  249. }
  250. stDisplayInfo.hWnd = hWnd_pictureBox;
  251. stDisplayInfo.pData = stFrameInfo.pBufAddr;
  252. stDisplayInfo.nDataLen = stFrameInfo.stFrameInfo.nFrameLen;
  253. stDisplayInfo.nWidth = stFrameInfo.stFrameInfo.nWidth;
  254. stDisplayInfo.nHeight = stFrameInfo.stFrameInfo.nHeight;
  255. stDisplayInfo.enPixelType = stFrameInfo.stFrameInfo.enPixelType;//像素格式
  256. if (isDisplayOnPictureBox)//初始化完成之后
  257. {
  258. m_MyCamera.MV_CC_DisplayOneFrame_NET(ref stDisplayInfo);//显示一帧图像 将这帧图像的信息保存在变量中
  259. }
  260. Mat dst1 = new Mat(stDisplayInfo.nHeight, stDisplayInfo.nWidth,MatType.CV_8UC3, m_BufForDriver);
  261. callback(dst1); //回调函数 识别
  262. /*Rect rectangle = new Rect(20, 20, 300, 300);
  263. Cv2.Rectangle(dst1, rectangle, new Scalar(0, 0, 255), 2, LineTypes.AntiAlias);
  264. //dst1.SaveImage("D:\\333.jpg");
  265. */
  266. m_MyCamera.MV_CC_FreeImageBuffer_NET(ref stFrameInfo);
  267. }
  268. else
  269. {
  270. if (currentCameraInfor.isOpenTriggerMode)
  271. {
  272. Thread.Sleep(5);
  273. }
  274. }
  275. }
  276. }
  277. // ch:去除自定义的像素格式 | en:Remove custom pixel formats
  278. private bool RemoveCustomPixelFormats(MyCamera.MvGvspPixelType enPixelFormat)
  279. {
  280. Int32 nResult = ((int)enPixelFormat) & (unchecked((Int32)0x80000000));
  281. if (0x80000000 == nResult)
  282. {
  283. return true;
  284. }
  285. else
  286. {
  287. return false;
  288. }
  289. }
  290. public bool SetCameraParam()
  291. {
  292. m_MyCamera.MV_CC_SetEnumValue_NET("ExposureAuto", 0);
  293. int nRet = m_MyCamera.MV_CC_SetFloatValue_NET("ExposureTime", aSCameraParamInfor.ExposureTime);
  294. if (nRet != MyCamera.MV_OK)
  295. {
  296. return false;
  297. }
  298. m_MyCamera.MV_CC_SetEnumValue_NET("GainAuto", 0);
  299. nRet = m_MyCamera.MV_CC_SetFloatValue_NET("Gain", aSCameraParamInfor.Gain);
  300. if (nRet != MyCamera.MV_OK)
  301. {
  302. return false;
  303. }
  304. nRet = m_MyCamera.MV_CC_SetFloatValue_NET("AcquisitionFrameRate", aSCameraParamInfor.FrameRate);
  305. if (nRet != MyCamera.MV_OK)
  306. {
  307. return false;
  308. }
  309. return true;
  310. }
  311. public void SavePNG()
  312. {
  313. if (false == m_bGrabbing)
  314. {
  315. return;
  316. }
  317. if (RemoveCustomPixelFormats(m_stFrameInfo.enPixelType))
  318. {
  319. return;
  320. }
  321. MyCamera.MV_SAVE_IMG_TO_FILE_PARAM stSaveFileParam = new MyCamera.MV_SAVE_IMG_TO_FILE_PARAM();
  322. lock (BufForDriverLock)
  323. {
  324. if (m_stFrameInfo.nFrameLen == 0)
  325. {
  326. return;
  327. }
  328. stSaveFileParam.enImageType = MyCamera.MV_SAVE_IAMGE_TYPE.MV_Image_Png;
  329. stSaveFileParam.enPixelType = m_stFrameInfo.enPixelType;
  330. stSaveFileParam.pData = m_BufForDriver;
  331. stSaveFileParam.nDataLen = m_stFrameInfo.nFrameLen;
  332. stSaveFileParam.nHeight = m_stFrameInfo.nHeight;
  333. stSaveFileParam.nWidth = m_stFrameInfo.nWidth;
  334. stSaveFileParam.nQuality = 8;
  335. stSaveFileParam.iMethodValue = 2;
  336. if (picSavePath == "")
  337. {
  338. picSavePath = System.Environment.CurrentDirectory + "\\Image\\";
  339. }
  340. stSaveFileParam.pImagePath = picSavePath + "Image_w" + stSaveFileParam.nWidth.ToString() + "_h" + stSaveFileParam.nHeight.ToString() + "_fn" + m_stFrameInfo.nFrameNum.ToString() + ".png";
  341. int nRet = m_MyCamera.MV_CC_SaveImageToFile_NET(ref stSaveFileParam);
  342. if (MyCamera.MV_OK != nRet)
  343. {
  344. return;
  345. }
  346. }
  347. }
  348. public void SaveTiff()
  349. {
  350. if (false == m_bGrabbing)
  351. {
  352. return;
  353. }
  354. if (RemoveCustomPixelFormats(m_stFrameInfo.enPixelType))
  355. {
  356. return;
  357. }
  358. MyCamera.MV_SAVE_IMG_TO_FILE_PARAM stSaveFileParam = new MyCamera.MV_SAVE_IMG_TO_FILE_PARAM();
  359. lock (BufForDriverLock)
  360. {
  361. if (m_stFrameInfo.nFrameLen == 0)
  362. {
  363. return;
  364. }
  365. stSaveFileParam.enImageType = MyCamera.MV_SAVE_IAMGE_TYPE.MV_Image_Tif;
  366. stSaveFileParam.enPixelType = m_stFrameInfo.enPixelType;
  367. stSaveFileParam.pData = m_BufForDriver;
  368. stSaveFileParam.nDataLen = m_stFrameInfo.nFrameLen;
  369. stSaveFileParam.nHeight = m_stFrameInfo.nHeight;
  370. stSaveFileParam.nWidth = m_stFrameInfo.nWidth;
  371. stSaveFileParam.iMethodValue = 2;
  372. if (picSavePath == "")
  373. {
  374. picSavePath = System.Environment.CurrentDirectory + "\\Image\\";
  375. }
  376. stSaveFileParam.pImagePath = picSavePath + "Image_w" + stSaveFileParam.nWidth.ToString() + "_h" + stSaveFileParam.nHeight.ToString() + "_fn" + m_stFrameInfo.nFrameNum.ToString() + ".tif";
  377. int nRet = m_MyCamera.MV_CC_SaveImageToFile_NET(ref stSaveFileParam);
  378. if (MyCamera.MV_OK != nRet)
  379. {
  380. return;
  381. }
  382. }
  383. }
  384. public void SaveBMP()
  385. {
  386. if (false == m_bGrabbing)
  387. {
  388. return;
  389. }
  390. if (RemoveCustomPixelFormats(m_stFrameInfo.enPixelType))
  391. {
  392. return;
  393. }
  394. MyCamera.MV_SAVE_IMG_TO_FILE_PARAM stSaveFileParam = new MyCamera.MV_SAVE_IMG_TO_FILE_PARAM();
  395. lock (BufForDriverLock)
  396. {
  397. if (m_stFrameInfo.nFrameLen == 0)
  398. {
  399. return;
  400. }
  401. stSaveFileParam.enImageType = MyCamera.MV_SAVE_IAMGE_TYPE.MV_Image_Bmp;
  402. stSaveFileParam.enPixelType = m_stFrameInfo.enPixelType;
  403. stSaveFileParam.pData = m_BufForDriver;
  404. stSaveFileParam.nDataLen = m_stFrameInfo.nFrameLen;
  405. stSaveFileParam.nHeight = m_stFrameInfo.nHeight;
  406. stSaveFileParam.nWidth = m_stFrameInfo.nWidth;
  407. stSaveFileParam.iMethodValue = 2;
  408. if (picSavePath == "")
  409. {
  410. picSavePath = System.Environment.CurrentDirectory + "\\Image\\";
  411. }
  412. stSaveFileParam.pImagePath = picSavePath + "Image_w" + stSaveFileParam.nWidth.ToString() + "_h" + stSaveFileParam.nHeight.ToString() + "_fn" + m_stFrameInfo.nFrameNum.ToString() + ".bmp";
  413. int nRet = m_MyCamera.MV_CC_SaveImageToFile_NET(ref stSaveFileParam);
  414. if (MyCamera.MV_OK != nRet)
  415. {
  416. return;
  417. }
  418. }
  419. }
  420. public void SaveJPG(string path)
  421. {
  422. //if (false == m_bGrabbing)
  423. //{
  424. // return;
  425. //}
  426. //if (RemoveCustomPixelFormats(m_stFrameInfo.enPixelType))
  427. //{
  428. // return;
  429. //}
  430. //MyCamera.MV_SAVE_IMG_TO_FILE_PARAM stSaveFileParam = new MyCamera.MV_SAVE_IMG_TO_FILE_PARAM();
  431. //lock (BufForDriverLock)
  432. //{
  433. // if (m_stFrameInfo.nFrameLen == 0)
  434. // {
  435. // return;
  436. // }
  437. // stSaveFileParam.enImageType = MyCamera.MV_SAVE_IAMGE_TYPE.MV_Image_Jpeg;
  438. // stSaveFileParam.enPixelType = m_stFrameInfo.enPixelType;
  439. // stSaveFileParam.pData = m_BufForDriver;
  440. // stSaveFileParam.nDataLen = m_stFrameInfo.nFrameLen;
  441. // stSaveFileParam.nHeight = m_stFrameInfo.nHeight;
  442. // stSaveFileParam.nWidth = m_stFrameInfo.nWidth;
  443. // stSaveFileParam.nQuality = 80;
  444. // stSaveFileParam.iMethodValue = 2;
  445. // if (picSavePath == "" || picSavePath==null)
  446. // {
  447. // picSavePath = System.Environment.CurrentDirectory + "\\Image\\";
  448. // }
  449. // stSaveFileParam.pImagePath = picSavePath + "Image_w" + stSaveFileParam.nWidth.ToString() + "_h" + stSaveFileParam.nHeight.ToString() + "_fn" + m_stFrameInfo.nFrameNum.ToString() + ".jpg";
  450. // int nRet = m_MyCamera.MV_CC_SaveImageToFile_NET(ref stSaveFileParam);
  451. // //Mat dst1 = new Mat(stSaveFileParam.nHeight, stSaveFileParam.nWidth,MatType.CV_8UC3, m_BufForDriver);
  452. // //dst1.SaveImage("D:\\111.jpg");
  453. // if (MyCamera.MV_OK != nRet)
  454. // {
  455. // return;
  456. // }
  457. //}
  458. if (false == m_bGrabbing)
  459. {
  460. return;
  461. }
  462. if (RemoveCustomPixelFormats(m_stFrameInfo.enPixelType))
  463. {
  464. return;
  465. }
  466. MyCamera.MV_SAVE_IMG_TO_FILE_PARAM stSaveFileParam = new MyCamera.MV_SAVE_IMG_TO_FILE_PARAM();
  467. lock (BufForDriverLock)
  468. {
  469. if (m_stFrameInfo.nFrameLen == 0)
  470. {
  471. return;
  472. }
  473. stSaveFileParam.enImageType = MyCamera.MV_SAVE_IAMGE_TYPE.MV_Image_Jpeg;
  474. stSaveFileParam.enPixelType = m_stFrameInfo.enPixelType;
  475. stSaveFileParam.pData = m_BufForDriver;
  476. stSaveFileParam.nDataLen = m_stFrameInfo.nFrameLen;
  477. stSaveFileParam.nHeight = m_stFrameInfo.nHeight;
  478. stSaveFileParam.nWidth = m_stFrameInfo.nWidth;
  479. stSaveFileParam.nQuality = 80;
  480. stSaveFileParam.iMethodValue = 2;
  481. if (string.IsNullOrEmpty(path))
  482. stSaveFileParam.pImagePath = System.Environment.CurrentDirectory + "\\Image\\" + picSavePath + "Image_w" + stSaveFileParam.nWidth.ToString() + "_h" + stSaveFileParam.nHeight.ToString() + "_fn" + m_stFrameInfo.nFrameNum.ToString() + ".jpg";
  483. else
  484. {
  485. stSaveFileParam.pImagePath = path;
  486. }
  487. int nRet = m_MyCamera.MV_CC_SaveImageToFile_NET(ref stSaveFileParam);
  488. //Mat dst1 = new Mat(stSaveFileParam.nHeight, stSaveFileParam.nWidth,MatType.CV_8UC3, m_BufForDriver);
  489. //dst1.SaveImage("D:\\111.jpg");
  490. if (MyCamera.MV_OK != nRet)
  491. {
  492. return;
  493. }
  494. }
  495. }
  496. static bool IsMonoPixelFormat(MyCamera.MvGvspPixelType enType)
  497. {
  498. switch (enType)
  499. {
  500. case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8:
  501. case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono10:
  502. case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono10_Packed:
  503. case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono12:
  504. case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono12_Packed:
  505. return true;
  506. default:
  507. return false;
  508. }
  509. }
  510. }
  511. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/455138
推荐阅读
相关标签
  

闽ICP备14008679号