This article refers to   Haikang Official example of prestige TV

First create HkCamera.cs class
using System; using System.Windows.Forms; using HalconDotNet; using
MvCamCtrl.NET; using System.Runtime.InteropServices; using System.Threading;
namespace VisionSystemHK { class HkCamera { MyCamera.MV_CC_DEVICE_INFO_LIST
m_pDeviceList; private MyCamera m_pMyCamera; HWindow m_Window; bool
m_bGrabbing; byte[] m_pDataForRed = new byte[20 * 1024 * 1024]; byte[]
m_pDataForGreen = new byte[20 * 1024 * 1024]; byte[] m_pDataForBlue = new
byte[20 * 1024 * 1024]; uint g_nPayloadSize = 0; public void HkCameraInit() {
m_pDeviceList = new MyCamera.MV_CC_DEVICE_INFO_LIST(); m_pMyCamera = new
MyCamera(); m_bGrabbing = false; m_Window = new HWindow(); //
DisplayWindowsInitial(); DeviceListAcq(); return; } // Displays a drop-down list of found cameras public void
DeviceListAcq() { int nRet; // ch: Create device list || en: Create device list
System.GC.Collect(); // cbDeviceList.Items.Clear(); nRet =
MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE |
MyCamera.MV_USB_DEVICE, ref m_pDeviceList); if (MyCamera.MV_OK != nRet) {
MessageBox.Show("Enum Devices Fail"); return; } // ch: Displays the device name in the list of forms || Display
the device'name on window's list for (int i = 0; i < m_pDeviceList.nDeviceNum;
i++) { MyCamera.MV_CC_DEVICE_INFO device =
(MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_pDeviceList.pDeviceInfo[i],
typeof(MyCamera.MV_CC_DEVICE_INFO)); if (device.nTLayerType ==
MyCamera.MV_GIGE_DEVICE) { IntPtr buffer =
Marshal.UnsafeAddrOfPinnedArrayElement(device.SpecialInfo.stGigEInfo, 0);
MyCamera.MV_GIGE_DEVICE_INFO gigeInfo =
(MyCamera.MV_GIGE_DEVICE_INFO)Marshal.PtrToStructure(buffer,
typeof(MyCamera.MV_GIGE_DEVICE_INFO)); if (gigeInfo.chUserDefinedName != "") {
// cbDeviceList.Items.Add("GigE: " + gigeInfo.chUserDefinedName + " (" +
gigeInfo.chSerialNumber + ")"); } else { // cbDeviceList.Items.Add("GigE: " +
gigeInfo.chManufacturerName + " " + gigeInfo.chModelName + " (" +
gigeInfo.chSerialNumber + ")"); } } else if (device.nTLayerType ==
MyCamera.MV_USB_DEVICE) { IntPtr buffer =
Marshal.UnsafeAddrOfPinnedArrayElement(device.SpecialInfo.stUsb3VInfo, 0);
MyCamera.MV_USB3_DEVICE_INFO usbInfo =
(MyCamera.MV_USB3_DEVICE_INFO)Marshal.PtrToStructure(buffer,
typeof(MyCamera.MV_USB3_DEVICE_INFO)); if (usbInfo.chUserDefinedName != "") {
// cbDeviceList.Items.Add("USB: " + usbInfo.chUserDefinedName + " (" +
usbInfo.chSerialNumber + ")"); } else { // cbDeviceList.Items.Add("USB: " +
usbInfo.chManufacturerName + " " + usbInfo.chModelName + " (" +
usbInfo.chSerialNumber + ")"); } } } //.ch: Choose the first one || en: Select the first item
if (m_pDeviceList.nDeviceNum != 0) { // cbDeviceList.SelectedIndex = 0; } }
// Turn on the camera public void OpenCamera() { //if (m_pDeviceList.nDeviceNum == 0 ||
cbDeviceList.SelectedIndex == -1) if (m_pDeviceList.nDeviceNum == 0 ) {
MessageBox.Show("No device,please select"); return; } int nRet = -1;
//ch: Get selected device information | en:Get selected device information int index = 0;
MyCamera.MV_CC_DEVICE_INFO device =
(MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_pDeviceList.pDeviceInfo[index],
typeof(MyCamera.MV_CC_DEVICE_INFO)); nRet =
m_pMyCamera.MV_CC_CreateDevice_NET(ref device); if (MyCamera.MV_OK != nRet) {
return; } // ch: open device | en:Open device nRet =
m_pMyCamera.MV_CC_OpenDevice_NET(); if (MyCamera.MV_OK != nRet) {
MessageBox.Show("Open Device Fail"); return; } // ch: Get package size || en: Get Payload
Size MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE(); nRet =
m_pMyCamera.MV_CC_GetIntValue_NET("PayloadSize", ref stParam); if
(MyCamera.MV_OK != nRet) { MessageBox.Show("Get PayloadSize Fail"); return; }
g_nPayloadSize = stParam.nCurValue; // ch: Get high || en: Get Height nRet =
m_pMyCamera.MV_CC_GetIntValue_NET("Height", ref stParam); if (MyCamera.MV_OK !=
nRet) { MessageBox.Show("Get Height Fail"); return; } uint nHeight =
stParam.nCurValue; // ch: Get wide || en: Get Width nRet =
m_pMyCamera.MV_CC_GetIntValue_NET("Width", ref stParam); if (MyCamera.MV_OK !=
nRet) { MessageBox.Show("Get Width Fail"); return; } uint nWidth =
stParam.nCurValue; m_pDataForRed = new byte[nWidth * nHeight]; m_pDataForGreen
= new byte[nWidth * nHeight]; m_pDataForBlue = new byte[nWidth * nHeight]; //
ch: Set the trigger mode to off || en:set trigger mode as off
m_pMyCamera.MV_CC_SetEnumValue_NET("AcquisitionMode", 2);
m_pMyCamera.MV_CC_SetEnumValue_NET("TriggerMode", 0); } // Turn off the camera public void
CloseCamera() { if (m_bGrabbing) { m_bGrabbing = false; // ch: Stop capturing || en:Stop
grab image m_pMyCamera.MV_CC_StopGrabbing_NET(); // ch: Control operation || en: Control
operation //SetCtrlWhenStopGrab(); } // ch: Turn off the device || en: Close device
m_pMyCamera.MV_CC_CloseDevice_NET(); // ch: Control operation || en: Control operation
//SetCtrlWhenClose(); m_bGrabbing = false; } // Set continuous mode public void
SetContinuesMode() { int nRet = MyCamera.MV_OK; if (true) { nRet =
m_pMyCamera.MV_CC_SetEnumValue_NET("TriggerMode", 0); if (nRet !=
MyCamera.MV_OK) { MessageBox.Show("Set TriggerMode Fail"); return; } } }
// Set trigger mode public void SetTriggerMode() { int nRet = MyCamera.MV_OK; if (true) {
nRet = m_pMyCamera.MV_CC_SetEnumValue_NET("TriggerMode", 1); if (nRet !=
MyCamera.MV_OK) { MessageBox.Show("Set TriggerMode Fail"); return; } // ch:
Trigger source selection :0 - Line0 || en :TriggerMode select; // 1 - Line1; // 2 - Line2; // 3 -
Line3; // 4 - Counter; // 7 - Software; // Software trigger if (true) { nRet =
m_pMyCamera.MV_CC_SetEnumValue_NET("TriggerSource", 7); if (nRet !=
MyCamera.MV_OK) { MessageBox.Show("Set TriggerSource Fail"); return; } } else {
nRet = m_pMyCamera.MV_CC_SetEnumValue_NET("TriggerSource", 0); if (nRet !=
MyCamera.MV_OK) { MessageBox.Show("Set TriggerSource Fail"); return; } } } }
// Start collection public void StartGrab() { int nRet; // ch: Open snapshot | en:start grab nRet =
m_pMyCamera.MV_CC_StartGrabbing_NET(); if (MyCamera.MV_OK != nRet) {
MessageBox.Show("Start Grabbing Fail"); return; } m_bGrabbing = true; Thread
hReceiveImageThreadHandle = new Thread(ReceiveImageWorkThread);
hReceiveImageThreadHandle.Start(m_pMyCamera); // ch: Control operation || en: Control
operation // SetCtrlWhenStartGrab(); } // Stop acquisition public void StopGrab() { int nRet
= -1; // ch: Stop capturing || en:Stop grab image nRet =
m_pMyCamera.MV_CC_StopGrabbing_NET(); if (nRet != MyCamera.MV_OK) {
MessageBox.Show("Stop Grabbing Fail"); } m_bGrabbing = false; // ch: Control operation ||
en: Control operation // SetCtrlWhenStopGrab(); } // Trigger once public void
SoftTriggerExec() { int nRet; // ch: Trigger command || en: Trigger command nRet =
m_pMyCamera.MV_CC_SetCommandValue_NET("TriggerSoftware"); if (MyCamera.MV_OK !=
nRet) { MessageBox.Show("Trigger Fail"); } } // Receive image thread private void
ReceiveImageWorkThread(object obj) { int nRet = MyCamera.MV_OK; MyCamera device
= obj as MyCamera; MyCamera.MV_FRAME_OUT_INFO_EX pFrameInfo = new
MyCamera.MV_FRAME_OUT_INFO_EX(); IntPtr pData =
Marshal.AllocHGlobal((int)g_nPayloadSize * 3); if (pData == IntPtr.Zero) {
return ; } IntPtr pImageBuffer = Marshal.AllocHGlobal((int)g_nPayloadSize * 3);
if (pImageBuffer == IntPtr.Zero) { return ; } uint nDataSize = g_nPayloadSize *
3; HObject Hobj = new HObject(); IntPtr RedPtr = IntPtr.Zero; IntPtr GreenPtr =
IntPtr.Zero; IntPtr BluePtr = IntPtr.Zero; IntPtr pTemp = IntPtr.Zero; while
(m_bGrabbing) { nRet = device.MV_CC_GetOneFrameTimeout_NET(pData, nDataSize,
ref pFrameInfo, 1000); if (MyCamera.MV_OK == nRet) { if
(IsColorPixelFormat(pFrameInfo.enPixelType)) { if (pFrameInfo.enPixelType ==
MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed) { pTemp = pData; } else {
nRet = ConvertToRGB(obj, pData, pFrameInfo.nHeight, pFrameInfo.nWidth,
pFrameInfo.enPixelType, pImageBuffer); if (MyCamera.MV_OK != nRet) { return ; }
pTemp = pImageBuffer; } unsafe { byte* pBufForSaveImage = (byte*)pTemp; UInt32
nSupWidth = (pFrameInfo.nWidth + (UInt32)3) & 0xfffffffc; for (int nRow = 0;
nRow < pFrameInfo.nHeight; nRow++) { for (int col = 0; col < pFrameInfo.nWidth;
col++) { m_pDataForRed[nRow * nSupWidth + col] = pBufForSaveImage[nRow *
pFrameInfo.nWidth * 3 + (3 * col)]; m_pDataForGreen[nRow * nSupWidth + col] =
pBufForSaveImage[nRow * pFrameInfo.nWidth * 3 + (3 * col + 1)];
m_pDataForBlue[nRow * nSupWidth + col] = pBufForSaveImage[nRow *
pFrameInfo.nWidth * 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 Hobj, (HTuple)"byte", pFrameInfo.nWidth,
pFrameInfo.nHeight, (new HTuple(RedPtr)), (new HTuple(GreenPtr)), (new
HTuple(BluePtr)), IntPtr.Zero); } catch (System.Exception ex) {
MessageBox.Show(ex.ToString()); } } else if
(IsMonoPixelFormat(pFrameInfo.enPixelType)) { if (pFrameInfo.enPixelType ==
MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8) { pTemp = pData; } else { nRet =
ConvertToMono8(device, pData, pImageBuffer, pFrameInfo.nHeight,
pFrameInfo.nWidth, pFrameInfo.enPixelType); if (MyCamera.MV_OK != nRet) {
return ; } pTemp = pImageBuffer; } try { HOperatorSet.GenImage1Extern(out Hobj,
"byte", pFrameInfo.nWidth, pFrameInfo.nHeight, pTemp, IntPtr.Zero); } catch
(System.Exception ex) { MessageBox.Show(ex.ToString()); return; } } else {
continue; } if (null != Hobj) { Global.myHobj = Hobj; //Global.iSGetImgObj =
true; } else { MessageBox.Show("No picObj!"); } // HalconDisplay(m_Window,
Hobj, pFrameInfo.nHeight, pFrameInfo.nWidth); } else { continue; } } if (pData
!= IntPtr.Zero) { Marshal.FreeHGlobal(pData); } if (pImageBuffer !=
IntPtr.Zero) { Marshal.FreeHGlobal(pImageBuffer); } return ; } // private bool
IsColorPixelFormat(MyCamera.MvGvspPixelType enType) { switch (enType) { case
MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed: case
MyCamera.MvGvspPixelType.PixelType_Gvsp_BGR8_Packed: case
MyCamera.MvGvspPixelType.PixelType_Gvsp_RGBA8_Packed: case
MyCamera.MvGvspPixelType.PixelType_Gvsp_BGRA8_Packed: case
MyCamera.MvGvspPixelType.PixelType_Gvsp_YUV422_Packed: case
MyCamera.MvGvspPixelType.PixelType_Gvsp_YUV422_YUYV_Packed: 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_BayerGB10: case
MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB10_Packed: case
MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG10: case
MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG10_Packed: case
MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG10: case
MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG10_Packed: case
MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR10: case
MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR10_Packed: case
MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB12: case
MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB12_Packed: case
MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG12: case
MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG12_Packed: case
MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG12: case
MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG12_Packed: case
MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR12: case
MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR12_Packed: return true; default:
return false; } } // public Int32 ConvertToRGB(object obj, IntPtr pSrc, ushort
nHeight, ushort nWidth, MyCamera.MvGvspPixelType nPixelType, IntPtr pDst) { if
(IntPtr.Zero == pSrc || IntPtr.Zero == pDst) { return MyCamera.MV_E_PARAMETER;
} int nRet = MyCamera.MV_OK; MyCamera device = obj as MyCamera;
MyCamera.MV_PIXEL_CONVERT_PARAM stPixelConvertParam = new
MyCamera.MV_PIXEL_CONVERT_PARAM(); stPixelConvertParam.pSrcData = pSrc;// Source data if
(IntPtr.Zero == stPixelConvertParam.pSrcData) { return -1; }
stPixelConvertParam.nWidth = nWidth;// Image width stPixelConvertParam.nHeight =
nHeight;// Image height stPixelConvertParam.enSrcPixelType = nPixelType;// Format of source data
stPixelConvertParam.nSrcDataLen = (uint)(nWidth * nHeight *
((((uint)nPixelType) >> 16) & 0x00ff) >> 3); stPixelConvertParam.nDstBufferSize
= (uint)(nWidth * nHeight *
((((uint)MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed) >> 16) & 0x00ff)
>> 3); stPixelConvertParam.pDstBuffer = pDst;// Converted data
stPixelConvertParam.enDstPixelType =
MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed;
stPixelConvertParam.nDstBufferSize = (uint)nWidth * nHeight * 3; nRet =
device.MV_CC_ConvertPixelType_NET(ref stPixelConvertParam);// format conversion if
(MyCamera.MV_OK != nRet) { return -1; } return MyCamera.MV_OK; } private bool
IsMonoPixelFormat(MyCamera.MvGvspPixelType enType) { switch (enType) { 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; } } public Int32 ConvertToMono8(object obj, IntPtr pInData,
IntPtr pOutData, ushort nHeight, ushort nWidth, MyCamera.MvGvspPixelType
nPixelType) { if (IntPtr.Zero == pInData || IntPtr.Zero == pOutData) { return
MyCamera.MV_E_PARAMETER; } int nRet = MyCamera.MV_OK; MyCamera device = obj as
MyCamera; MyCamera.MV_PIXEL_CONVERT_PARAM stPixelConvertParam = new
MyCamera.MV_PIXEL_CONVERT_PARAM(); stPixelConvertParam.pSrcData = pInData;// Source data
if (IntPtr.Zero == stPixelConvertParam.pSrcData) { return -1; }
stPixelConvertParam.nWidth = nWidth;// Image width stPixelConvertParam.nHeight =
nHeight;// Image height stPixelConvertParam.enSrcPixelType = nPixelType;// Format of source data
stPixelConvertParam.nSrcDataLen = (uint)(nWidth * nHeight *
((((uint)nPixelType) >> 16) & 0x00ff) >> 3); stPixelConvertParam.nDstBufferSize
= (uint)(nWidth * nHeight *
((((uint)MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed) >> 16) & 0x00ff)
>> 3); stPixelConvertParam.pDstBuffer = pOutData;// Converted data
stPixelConvertParam.enDstPixelType =
MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8;
stPixelConvertParam.nDstBufferSize = (uint)(nWidth * nHeight * 3); nRet =
device.MV_CC_ConvertPixelType_NET(ref stPixelConvertParam);// format conversion if
(MyCamera.MV_OK != nRet) { return -1; } return nRet; } // public void
HalconDisplay(HTuple hWindow, HObject Hobj, HTuple hHeight, HTuple hWidth) { //
ch: display || display try { HOperatorSet.SetPart(hWindow, 0, 0, hHeight - 1, hWidth
- 1);// ch: Fit image display to window size || en: Make the image adapt the window size } catch
(System.Exception ex) { MessageBox.Show(ex.ToString()); return; } if (hWindow
== null) { return; } try { HOperatorSet.DispObj(Hobj, hWindow);// ch display || en:
display } catch (System.Exception ex) { MessageBox.Show(ex.ToString()); return;
} return; } private void DisplayWindowsInitial() { // ch: Define the start point and width and height of the display || en:
Definition the width and height of the display window HTuple hWindowRow,
hWindowColumn, hWindowWidth, hWindowHeight; // ch: Set the start point, width and height of the display window || en: Set the
width and height of the display window hWindowRow = 0; hWindowColumn = 0; //
hWindowWidth = pictureBox1.Width; // hWindowHeight = pictureBox1.Height; try {
// HTuple hWindowID = (HTuple)pictureBox1.Handle; //
m_Window.OpenWindow(hWindowRow, hWindowColumn, hWindowWidth, hWindowHeight,
hWindowID, "visible", ""); } catch (System.Exception ex) {
MessageBox.Show(ex.ToString()); return; } } } }
Then write a management class
using System; using System.Collections.Generic; using System.Linq; using
System.Text; using System.Threading.Tasks; namespace VisionSystemHK { class
CameraManage { public static HkCamera myHkCamera; public static bool[]
isCameraConnect = new bool[1]; // Camera connection status public static string[]
deviceStateString = new string[1]; // Status display to be transmitted to the interface public CameraManage() {
myHkCamera = new HkCamera(); // The camera was initially disconnected for (int i = 0; i < 1; i++) {
isCameraConnect[i] = false; } for (int i = 0; i < 1; i++) {
deviceStateString[i] = ""; } } public static bool ConnectCamera(int id, string
CameraType, bool useStartTrigger) { if (isCameraConnect[id] == false) { int
ReturnNum = 0; switch (CameraType) { case "HkCamera":
myHkCamera.HkCameraInit(); myHkCamera.OpenCamera();
myHkCamera.SetTriggerMode(); // myHkCamera.SetContinuesMode();
myHkCamera.StartGrab(); //ReturnNum = break; } if (ReturnNum == 0) {
isCameraConnect[id] = true; deviceStateString[id] = id.ToString() + " Camera 2 connected successfully !";
return true; } else { deviceStateString[id] += " warning :" + id.ToString() +
" Camera 2 failed to connect !"; return false; } } else { deviceStateString[id] = " warning :" +
id.ToString() + " Camera 2 is connected !"; return false; } } // Disconnect the camera public static void
Disconnect(int id) { switch (id) { case 0: if (isCameraConnect[0]) {
isCameraConnect[0] = false; myHkCamera.StopGrab(); myHkCamera.CloseCamera();
deviceStateString[0] = "1 Camera two is disconnected !"; } else { deviceStateString[0] = "1 Failed to disconnect camera No.2 !";
} break; default: break; } } public static void SoftTrigger() {
myHkCamera.SoftTriggerExec(); } } }
Finally, it is called in the main program
Global class public static CameraManage myCameraManage; public Global() {
myCameraManage = new CameraManage(); } MainForm Global myGlobal = new Global();
// Trigger image capture CameraManage.SoftTrigger();
 

Technology