C#、工业相机和Halcon的混合编程
提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
其中最重要的两个命名空间:
using HalconDotNet; (Halcon使用)
using MvCamCtrl.NET;(海康工业相机使用)
代码如下(示例):
private void FindDevice()
{
// ch:创建设备列表 | en:Create Device List
System.GC.Collect();
cbDeviceList.Items.Clear();
m_stDeviceList.nDeviceNum = 0;
int nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref m_stDeviceList);
if (0 != nRet)
{
ShowErrorMsg("Enumerate devices fail!",0);
return;
}
// ch:在窗体列表中显示设备名 | en:Display device name in the form list
for (int i = 0; i < m_stDeviceList.nDeviceNum; i++)
{
MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_stDeviceList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO));
if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
{
MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(device.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO));
if (gigeInfo.chUserDefinedName != "")
{
cbDeviceList.Items.Add("GEV: " + gigeInfo.chUserDefinedName + " (" + gigeInfo.chSerialNumber + ")");
}
else
{
cbDeviceList.Items.Add("GEV: " + gigeInfo.chManufacturerName + " " + gigeInfo.chModelName + " (" + gigeInfo.chSerialNumber + ")");
}
}
else if (device.nTLayerType == MyCamera.MV_USB_DEVICE)
{
MyCamera.MV_USB3_DEVICE_INFO usbInfo = (MyCamera.MV_USB3_DEVICE_INFO)MyCamera.ByteToStruct(device.SpecialInfo.stUsb3VInfo, typeof(MyCamera.MV_USB3_DEVICE_INFO));
if (usbInfo.chUserDefinedName != "")
{
cbDeviceList.Items.Add("U3V: " + usbInfo.chUserDefinedName + " (" + usbInfo.chSerialNumber + ")");
}
else
{
cbDeviceList.Items.Add("U3V: " + usbInfo.chManufacturerName + " " + usbInfo.chModelName + " (" + usbInfo.chSerialNumber + ")");
}
}
}
// ch:选择第一项 | en:Select the first item
if (m_stDeviceList.nDeviceNum != 0)
{
cbDeviceList.SelectedIndex = 0;
}
}
代码如下(示例):
private void OpenDevicce_Click(object sender, EventArgs e)
{
if (m_stDeviceList.nDeviceNum == 0 || cbDeviceList.SelectedIndex == -1)
{
ShowErrorMsg("No device, please select", 0);
return;
}
// ch:获取选择的设备信息 | en:Get selected device information
MyCamera.MV_CC_DEVICE_INFO device =
(MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_stDeviceList.pDeviceInfo[cbDeviceList.SelectedIndex],
typeof(MyCamera.MV_CC_DEVICE_INFO));
// ch:打开设备 | en:Open device
if (null == m_MyCamera)
{
m_MyCamera = new MyCamera();
if (null == m_MyCamera)
{
ShowErrorMsg("Applying resource fail!", MyCamera.MV_E_RESOURCE);
return;
}
}
int nRet = m_MyCamera.MV_CC_CreateDevice_NET(ref device);
if (MyCamera.MV_OK != nRet)
{
ShowErrorMsg("Create device fail!", nRet);
return;
}
nRet = m_MyCamera.MV_CC_OpenDevice_NET();
if (MyCamera.MV_OK != nRet)
{
m_MyCamera.MV_CC_DestroyDevice_NET();
ShowErrorMsg("Device open fail!", nRet);
return;
}
// ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
{
int nPacketSize = m_MyCamera.MV_CC_GetOptimalPacketSize_NET();
if (nPacketSize > 0)
{
nRet = m_MyCamera.MV_CC_SetIntValueEx_NET("GevSCPSPacketSize", nPacketSize);
if (nRet != MyCamera.MV_OK)
{
ShowErrorMsg("Set Packet Size failed!", nRet);
}
}
else
{
ShowErrorMsg("Get Packet Size failed!", nPacketSize);
}
}
// ch:设置采集连续模式 | en:Set Continues Aquisition Mode
m_MyCamera.MV_CC_SetEnumValue_NET("AcquisitionMode", (uint)MyCamera.MV_CAM_ACQUISITION_MODE.MV_ACQ_MODE_CONTINUOUS);
m_MyCamera.MV_CC_SetEnumValue_NET("TriggerMode", (uint)MyCamera.MV_CAM_TRIGGER_MODE.MV_TRIGGER_MODE_OFF);
bnGetParam_Click(null, null);// ch:获取参数 | en:Get parameters
}
代码如下(示例):
private void StartGrab_Click(object sender, EventArgs e)
{
// ch:前置配置 | en:pre-operation
int nRet = NecessaryOperBeforeGrab();
if (MyCamera.MV_OK != nRet)
{
return;
}
// ch:标志位置true | en:Set position bit true
m_bGrabbing = true;
m_stFrameInfo.nFrameLen = 0;//取流之前先清除帧长度
m_stFrameInfo.enPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_Undefined;
m_hReceiveThread = new Thread(ReceiveThreadProcess);
m_hReceiveThread.Start();
// ch:开始采集 | en:Start Grabbing
nRet = m_MyCamera.MV_CC_StartGrabbing_NET();
if (MyCamera.MV_OK != nRet)
{
m_bGrabbing = false;
m_hReceiveThread.Join();
ShowErrorMsg("Start Grabbing Fail!", nRet);
return;
}
}
代码如下(示例):
private void CloseDevice_Click(object sender, EventArgs e)
{
// ch:标志位设为false | en:Set flag bit false
m_bGrabbing = false;
m_hReceiveThread.Join();
// ch:停止采集 | en:Stop Grabbing
int nRet = m_MyCamera.MV_CC_StopGrabbing_NET();
if (nRet != MyCamera.MV_OK)
{
ShowErrorMsg("Stop Grabbing Fail!" , nRet);
}
// ch:控件操作 | en:Control Operation
SetCtrlWhenStopGrab();
}
代码如下(示例):
public void ReceiveThreadProcess()
{
HImage image = new HImage();
MyCamera.MV_FRAME_OUT stFrameInfo = new MyCamera.MV_FRAME_OUT();
MyCamera.MV_DISPLAY_FRAME_INFO stDisplayInfo = new MyCamera.MV_DISPLAY_FRAME_INFO();
MyCamera.MV_PIXEL_CONVERT_PARAM stConvertInfo = new
MyCamera.MV_PIXEL_CONVERT_PARAM();
int nRet = MyCamera.MV_OK;
nRet = m_MyCamera.MV_CC_GetImageBuffer_NET(ref stFrameInfo, 1000);
if (nRet == MyCamera.MV_OK)
{
lock (BufForDriverLock)
{
m_stFrameInfo = stFrameInfo.stFrameInfo;
CopyMemory(m_BufForDriver, stFrameInfo.pBufAddr, stFrameInfo.stFrameInfo.nFrameLen);
stConvertInfo.nWidth = stFrameInfo.stFrameInfo.nWidth;
stConvertInfo.nHeight = stFrameInfo.stFrameInfo.nHeight;
stConvertInfo.enSrcPixelType = stFrameInfo.stFrameInfo.enPixelType;
stConvertInfo.pSrcData = stFrameInfo.pBufAddr;
stConvertInfo.nSrcDataLen = stFrameInfo.stFrameInfo.nFrameLen;
stConvertInfo.pDstBuffer = m_ConvertDstBuf;
stConvertInfo.nDstBufferSize = m_nConvertDstBufLen;
if (PixelFormat.Format8bppIndexed == m_bitmap.PixelFormat)
{
stConvertInfo.enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8;
m_MyCamera.MV_CC_ConvertPixelType_NET(ref stConvertInfo);
}
else
{
stConvertInfo.enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_BGR8_Packed;
m_MyCamera.MV_CC_ConvertPixelType_NET(ref stConvertInfo);
}
}
image.GenImage1("bety",PWidth,PHeight,PData);
imG = image;
HOperatorSet.ClearWindows();
HOperatorSet.SetPart(HWindow , 0 , 0 , PWidth-1 , PHeight-1);
HOperatorSet.DispObj(imG, HWindow);
}
else
{}
}
C#和Halcon联合编程完成海康相机的连续触发和软触发。