本文的硬件是海康彩色相机MV CE013-50GC, 传感器类型CCD,相机分辨率12809603。
软件平台是.NET,视觉算法库是Halcon。
本文利用的开发框架是海康的官方Demo: BasicDemo。
如图:
本Demo 的界面如下:
只是在右侧多开了个HWindowControl 而已。
具体开发步骤:
首先引入MvCameraControl.Net.dll ,
具体路径是:
C:\Program Files (x86)\MVS\Development\Bin\win64
AnyCpu 或者32位 还是64位,根据你的程序决定。我的程序用的是64位,所以我选择的是win64文件夹下的 MvCameraControl.Net.dll。
如何引入dll我就不多说了。
然后引入命名空间,即
using HalconDotNet;
using MvCamCtrl.NET;
另外Halcon 的命名空间也要引入,具体可浏览别的博客。
将BasicDemo的各项功能一一复制到新建的项目中,赋值黏贴,你懂的。
然后,请查看上图中我的程序界面,新建一个HWindowControl 和两个Button和一个Timer。
具体的源代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;
using MvCamCtrl.NET;
namespace 海康相机与Halcon
{
public partial class Form1 : Form
{
MyCamera.MV_CC_DEVICE_INFO_LIST m_stDeviceList = new MyCamera.MV_CC_DEVICE_INFO_LIST();
private MyCamera m_MyCamera = new MyCamera();
private MyCamera.MVCC_INTVALUE stParam;//用于接收特定的参数
bool m_bGrabbing = false;
Thread m_hReceiveThread = null;
MyCamera.MV_FRAME_OUT_INFO_EX m_stFrameInfo = new MyCamera.MV_FRAME_OUT_INFO_EX();
// ch:用于从驱动获取图像的缓存 | en:Buffer for getting image from driver
UInt32 m_nBufSizeForDriver = 0;
UInt32 m_nBufSizeForDriver_halcon = 1280 * 960 * 3;
IntPtr m_BufForDriver;
byte[] m_pBufForDriver = new byte[1280 * 960 * 3];
private static Object BufForDriverLock = new Object();
// ch:用于保存图像的缓存 | en:Buffer for saving image
UInt32 m_nBufSizeForSaveImage = 0;
IntPtr m_BufForSaveImage;
byte[] m_pBufForSaveImage = new byte[1280 * 960 * 3 * 3 + 960];
HObject image = null;
byte[] m_pDataForRed = new byte[1280 * 960];
byte[] m_pDataForGreen = new byte[1280 * 960];
byte[] m_pDataForBlue = new byte[1280 * 960];
public Form1()
{
InitializeComponent();
DeviceListAcq();
Control.CheckForIllegalCrossThreadCalls = false;
}
private void Form1_Load(object sender, EventArgs e)
{
halconShowBtn.Enabled = false;
stopHalocnShow_Btn.Enabled = false;
}
private void DeviceListAcq()
{
// 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 bnOpen_Click(object sender, EventArgs e)
{
//halconShowBtn.Enabled = true;
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)
{
return;
}
}
int nRet = m_MyCamera.MV_CC_CreateDevice_NET(ref device);
if (MyCamera.MV_OK != 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_SetIntValue_NET("GevSCPSPacketSize", (uint)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
// ch:控件操作 | en:Control operation
SetCtrlWhenOpen();
}
private void bnGetParam_Click(object sender, EventArgs e)
{
MyCamera.MVCC_FLOATVALUE stParam = new MyCamera.MVCC_FLOATVALUE();
int nRet = m_MyCamera.MV_CC_GetFloatValue_NET("ExposureTime", ref stParam);
if (MyCamera.MV_OK == nRet)
{
tbExposure.Text = stParam.fCurValue.ToString("F1");
}
nRet = m_MyCamera.MV_CC_GetFloatValue_NET("ExposureTime", ref stParam);
if (MyCamera.MV_OK == nRet)
{
m_MyCamera.MV_CC_GetFloatValue_NET("Gain", ref stParam);
tbGain.Text = stParam.fCurValue.ToString("F1");
}
nRet = m_MyCamera.MV_CC_GetFloatValue_NET("ExposureTime", ref stParam);
if (MyCamera.MV_OK == nRet)
{
m_MyCamera.MV_CC_GetFloatValue_NET("ResultingFrameRate", ref stParam);
tbFrameRate.Text = stParam.fCurValue.ToString("F1");
}
}
private void SetCtrlWhenOpen()
{
bnOpen.Enabled = false;
bnClose.Enabled = true;
bnStartGrab.Enabled = true;
bnStopGrab.Enabled = false;
bnContinuesMode.Enabled = true;
bnContinuesMode.Checked = true;
bnTriggerMode.Enabled = true;
cbSoftTrigger.Enabled = false;
bnTriggerExec.Enabled = false;
tbExposure.Enabled = true;
tbGain.Enabled = true;
tbFrameRate.Enabled = true;
bnGetParam.Enabled = true;
bnSetParam.Enabled = true;
}
private void ShowErrorMsg(string csMessage, int nErrorNum)
{
string errorMsg;
if (nErrorNum == 0)
{
errorMsg = csMessage;
}
else
{
errorMsg = csMessage + ": Error =" + String.Format("{0:X}", nErrorNum);
}
switch (nErrorNum)
{
case MyCamera.MV_E_HANDLE: errorMsg += " Error or invalid handle "; break;
case MyCamera.MV_E_SUPPORT: errorMsg += " Not supported function "; break;
case MyCamera.MV_E_BUFOVER: errorMsg += " Cache is full "; break;
case MyCamera.MV_E_CALLORDER: errorMsg += " Function calling order error "; break;
case MyCamera.MV_E_PARAMETER: errorMsg += " Incorrect parameter "; break;
case MyCamera.MV_E_RESOURCE: errorMsg += " Applying resource failed "; break;
case MyCamera.MV_E_NODATA: errorMsg += " No data "; break;
case MyCamera.MV_E_PRECONDITION: errorMsg += " Precondition error, or running environment changed "; break;
case MyCamera.MV_E_VERSION: errorMsg += " Version mismatches "; break;
case MyCamera.MV_E_NOENOUGH_BUF: errorMsg += " Insufficient memory "; break;
case MyCamera.MV_E_UNKNOW: errorMsg += " Unknown error "; break;
case MyCamera.MV_E_GC_GENERIC: errorMsg += " General error "; break;
case MyCamera.MV_E_GC_ACCESS: errorMsg += " Node accessing condition error "; break;
case MyCamera.MV_E_ACCESS_DENIED: errorMsg += " No permission "; break;
case MyCamera.MV_E_BUSY: errorMsg += " Device is busy, or network disconnected "; break;
case MyCamera.MV_E_NETER: errorMsg += " Network error "; break;
}
MessageBox.Show(errorMsg, "PROMPT");
}
private void bnEnum_Click(object sender, EventArgs e)
{
DeviceListAcq();
}
private void bnClose_Click(object sender, EventArgs e)
{
halconShowBtn.Enabled = false;
stopHalocnShow_Btn.Enabled = false;
// ch:取流标志位清零 | en:Reset flow flag bit
if (m_bGrabbing == true)
{
m_bGrabbing = false;
m_hReceiveThread.Join();
}
if (m_BufForDriver != IntPtr.Zero)
{
Marshal.Release(m_BufForDriver);
}
if (m_BufForSaveImage != IntPtr.Zero)
{
Marshal.Release(m_BufForSaveImage);
}
// ch:关闭设备 | en:Close Device
m_MyCamera.MV_CC_CloseDevice_NET();
m_MyCamera.MV_CC_DestroyDevice_NET();
// ch:控件操作 | en:Control Operation
SetCtrlWhenClose();
}
private void SetCtrlWhenClose()
{
bnOpen.Enabled = true;
bnClose.Enabled = false;
bnStartGrab.Enabled = false;
bnStopGrab.Enabled = false;
bnContinuesMode.Enabled = false;
bnTriggerMode.Enabled = false;
cbSoftTrigger.Enabled = false;
bnTriggerExec.Enabled = false;
bnSaveBmp.Enabled = false;
bnSaveJpg.Enabled = false;
bnSaveTiff.Enabled = false;
bnSavePng.Enabled = false;
tbExposure.Enabled = false;
tbGain.Enabled = false;
tbFrameRate.Enabled = false;
bnGetParam.Enabled = false;
bnSetParam.Enabled = false;
}
private void bnStartGrab_Click(object sender, EventArgs e)
{
halconShowBtn.Enabled = true;
stopHalocnShow_Btn.Enabled = true;
// ch:标志位置位true | en:Set position bit true
m_bGrabbing = true;
m_hReceiveThread = new Thread(ReceiveThreadProcess);
m_hReceiveThread.Start();
m_stFrameInfo.nFrameLen = 0;//取流之前先清除帧长度
m_stFrameInfo.enPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_Undefined;
// ch:开始采集 | en:Start Grabbing
int nRet = m_MyCamera.MV_CC_StartGrabbing_NET();
if (MyCamera.MV_OK != nRet)
{
m_bGrabbing = false;
m_hReceiveThread.Join();
ShowErrorMsg("Start Grabbing Fail!", nRet);
return;
}
// ch:控件操作 | en:Control Operation
SetCtrlWhenStartGrab();
}
public void ReceiveThreadProcess()
{
MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
int nRet = m_MyCamera.MV_CC_GetIntValue_NET("PayloadSize", ref stParam);
if (MyCamera.MV_OK != nRet)
{
ShowErrorMsg("Get PayloadSize failed", nRet);
return;
}
UInt32 nPayloadSize = stParam.nCurValue;
if (nPayloadSize > m_nBufSizeForDriver)
{
if (m_BufForDriver != IntPtr.Zero)
{
Marshal.Release(m_BufForDriver);
}
m_nBufSizeForDriver = nPayloadSize;
m_BufForDriver = Marshal.AllocHGlobal((Int32)m_nBufSizeForDriver);
}
if (m_BufForDriver == IntPtr.Zero)
{
return;
}
MyCamera.MV_FRAME_OUT_INFO_EX stFrameInfo = new MyCamera.MV_FRAME_OUT_INFO_EX();
MyCamera.MV_DISPLAY_FRAME_INFO stDisplayInfo = new MyCamera.MV_DISPLAY_FRAME_INFO();
while (m_bGrabbing)
{
lock (BufForDriverLock)
{
nRet = m_MyCamera.MV_CC_GetOneFrameTimeout_NET(m_BufForDriver, nPayloadSize, ref stFrameInfo, 1000);
if (nRet == MyCamera.MV_OK)
{
m_stFrameInfo = stFrameInfo;
}
}
if (nRet == MyCamera.MV_OK)
{
stDisplayInfo.hWnd = pictureBox1.Handle;
stDisplayInfo.pData = m_BufForDriver;
stDisplayInfo.nDataLen = stFrameInfo.nFrameLen;
stDisplayInfo.nWidth = stFrameInfo.nWidth;
stDisplayInfo.nHeight = stFrameInfo.nHeight;
stDisplayInfo.enPixelType = stFrameInfo.enPixelType;
m_MyCamera.MV_CC_DisplayOneFrame_NET(ref stDisplayInfo);
}
else
{
if (bnTriggerMode.Checked)
{
Thread.Sleep(5);
}
}
}
}
private void SetCtrlWhenStartGrab()
{
bnStartGrab.Enabled = false;
bnStopGrab.Enabled = true;
if (bnTriggerMode.Checked && cbSoftTrigger.Checked)
{
bnTriggerExec.Enabled = true;
}
bnSaveBmp.Enabled = true;
bnSaveJpg.Enabled = true;
bnSaveTiff.Enabled = true;
bnSavePng.Enabled = true;
}
private void bnSaveBmp_Click(object sender, EventArgs e)
{
if (false == m_bGrabbing)
{
ShowErrorMsg("Not Start Grabbing", 0);
return;
}
IntPtr pTemp = IntPtr.Zero;
MyCamera.MvGvspPixelType enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_Undefined;
if (m_stFrameInfo.enPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8 || m_stFrameInfo.enPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_BGR8_Packed)
{
pTemp = m_BufForDriver;
enDstPixelType = m_stFrameInfo.enPixelType;
}
else
{
UInt32 nSaveImageNeedSize = 0;
MyCamera.MV_PIXEL_CONVERT_PARAM stConverPixelParam = new MyCamera.MV_PIXEL_CONVERT_PARAM();
lock (BufForDriverLock)
{
if (m_stFrameInfo.nFrameLen == 0)
{
ShowErrorMsg("Save Bmp Fail!", 0);
return;
}
if (IsMonoData(m_stFrameInfo.enPixelType))
{
enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8;
nSaveImageNeedSize = (uint)m_stFrameInfo.nWidth * m_stFrameInfo.nHeight;
}
else if (IsColorData(m_stFrameInfo.enPixelType))
{
enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_BGR8_Packed;
nSaveImageNeedSize = (uint)m_stFrameInfo.nWidth * m_stFrameInfo.nHeight * 3;
}
else
{
ShowErrorMsg("No such pixel type!", 0);
return;
}
if (m_nBufSizeForSaveImage < nSaveImageNeedSize)
{
if (m_BufForSaveImage != IntPtr.Zero)
{
Marshal.Release(m_BufForSaveImage);
}
m_nBufSizeForSaveImage = nSaveImageNeedSize;
m_BufForSaveImage = Marshal.AllocHGlobal((Int32)m_nBufSizeForSaveImage);
}
stConverPixelParam.nWidth = m_stFrameInfo.nWidth;
stConverPixelParam.nHeight = m_stFrameInfo.nHeight;
stConverPixelParam.pSrcData = m_BufForDriver;
stConverPixelParam.nSrcDataLen = m_stFrameInfo.nFrameLen;
stConverPixelParam.enSrcPixelType = m_stFrameInfo.enPixelType;
stConverPixelParam.enDstPixelType = enDstPixelType;
stConverPixelParam.pDstBuffer = m_BufForSaveImage;
stConverPixelParam.nDstBufferSize = m_nBufSizeForSaveImage;
int nRet = m_MyCamera.MV_CC_ConvertPixelType_NET(ref stConverPixelParam);
if (MyCamera.MV_OK != nRet)
{
ShowErrorMsg("Convert Pixel Type Fail!", nRet);
return;
}
pTemp = m_BufForSaveImage;
}
}
}
private Boolean IsMonoData(MyCamera.MvGvspPixelType enGvspPixelType)
{
switch (enGvspPixelType)
{
case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono10:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono10_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono12:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono12_Packed:
return true;
default:
return false;
}
}
private Boolean IsColorData(MyCamera.MvGvspPixelType enGvspPixelType)
{
switch (enGvspPixelType)
{
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR8:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG8:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB8:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG8:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR10:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG10:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB10:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG10:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR12:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG12:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB12:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG12:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR10_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG10_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB10_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG10_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR12_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG12_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB12_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG12_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_YUV422_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_YUV422_YUYV_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_YCBCR411_8_CBYYCRYY:
return true;
default:
return false;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
bnClose_Click(sender, e);
}
//读取图片转换成Halcon图像
private void stopHalocnShow_Btn_Click(object sender, EventArgs e)
{
timer1.Enabled =false;
bnStopGrab.Enabled = true;
bnClose.Enabled = true;
}
private void halconShowBtn_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
bnStopGrab.Enabled = false;
bnClose.Enabled = false;
//采集的是黑白图像,利用Halcon图像库中的GenImage1算子来构建图像
//image.GenImage1("byte", (int)stFrameInfo.nWidth, (int)stFrameInfo.nHeight, pData);
//HOperatorSet.GenImage1(out image, "byte", 1280, 960, pData);//取内存数据,生成图像,halcon实现
//HOperatorSet.GenImage3(out image, "byte", 1280, 960, pData, pData, pData);
//HOperatorSet.GenImageInterleaved(out image,"byte",)
//HOperatorSet.DispObj(image, hWindowControl1.HalconWindow);
//image.Dispose();
}
private void timer1_Tick(object sender, EventArgs e)
{
int nRet;
IntPtr RedPtr = IntPtr.Zero;
IntPtr GreenPtr = IntPtr.Zero;
IntPtr BluePtr = IntPtr.Zero;
//MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
UInt32 nPayloadSize = 0;
nRet = m_MyCamera.MV_CC_GetIntValue_NET("PayloadSize", ref stParam);
if (MyCamera.MV_OK != nRet)
{
MessageBox.Show("Error1");
return;
}
nPayloadSize = stParam.nCurValue;
if (nPayloadSize > m_nBufSizeForDriver_halcon)
{
m_nBufSizeForDriver_halcon = nPayloadSize;
m_pBufForDriver = new byte[m_nBufSizeForDriver_halcon];
m_nBufSizeForSaveImage = m_nBufSizeForDriver_halcon * 3 + 960;
m_pBufForSaveImage = new byte[m_nBufSizeForSaveImage];
}
IntPtr pData = Marshal.UnsafeAddrOfPinnedArrayElement(m_pBufForDriver, 0);
MyCamera.MV_FRAME_OUT_INFO_EX stFrameInfo = new MyCamera.MV_FRAME_OUT_INFO_EX();
nRet = m_MyCamera.MV_CC_GetOneFrameTimeout_NET(pData, m_nBufSizeForDriver_halcon, ref stFrameInfo, 100);//获取一帧图像,超时时间设置为1000
if (MyCamera.MV_OK != nRet)
{
MessageBox.Show("Error2");
return;
}
unsafe
{
byte* pBufForSaveImage = (byte*)pData;
//由以下代码可以窥探,彩色相机拍到的图片在内存中是一个长长的一维byte数组,每个元素自然而然就是该通道灰度值。
//而且彩色相机拍到的图片在内存中是按照以下格式存储的:
// byteR byteG byteB byteR byteG byteB byteR byteG byteB byteR byteG byteB byteR byteG byteB byteR byteG byteB byteR byteG byteB byteR byteG byteB byteR byteG byteB
for (int nRow = 0; nRow < 1280; nRow++)
{
for (int col = 0; col < 960; col++)
{
m_pDataForRed[nRow * 960 + col] = pBufForSaveImage[nRow * 960 * 3 + (3 * col)];
//Console.Write(pBufForSaveImage[nRow * 960 * 3 + (3 * col)]+" ");
m_pDataForGreen[nRow * 960 + col] = pBufForSaveImage[nRow * 960 * 3 + (3 * col + 1)];
//Console.Write(pBufForSaveImage[nRow * 960 * 3 + (3 * col+1)] + " ");
m_pDataForBlue[nRow * 960 + col] = pBufForSaveImage[nRow * 960 * 3 + (3 * col + 2)];
//Console.Write(pBufForSaveImage[nRow * 960 * 3 + (3 * col+2)] + " ");
}
}
}
RedPtr = Marshal.UnsafeAddrOfPinnedArrayElement(m_pDataForRed, 0);
GreenPtr = Marshal.UnsafeAddrOfPinnedArrayElement(m_pDataForGreen, 0);
BluePtr = Marshal.UnsafeAddrOfPinnedArrayElement(m_pDataForBlue, 0);
try
{
HOperatorSet.GenImage3Extern(out image, (HTuple)"byte", 1280, 960,
(new HTuple(RedPtr)), (new HTuple(GreenPtr)), (new HTuple(BluePtr)), IntPtr.Zero);
HOperatorSet.DispObj(image, hWindowControl1.HalconWindow);
image.Dispose();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void bnStopGrab_Click(object sender, EventArgs e)
{
halconShowBtn.Enabled = false;
stopHalocnShow_Btn.Enabled = false;
// 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();
}
private void SetCtrlWhenStopGrab()
{
bnStartGrab.Enabled = true;
bnStopGrab.Enabled = false;
bnTriggerExec.Enabled = false;
bnSaveBmp.Enabled = false;
bnSaveJpg.Enabled = false;
bnSaveTiff.Enabled = false;
bnSavePng.Enabled = false;
}
}
}