1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using Cognex.VisionPro;
10 using Cognex.VisionPro.ImageFile;
11 using System.IO;
12 using PylonC.NET;
13 using System.Runtime.InteropServices;
14 using System.Threading;
15
16 namespace Demo
17 {
18 public partial class Form1 : Form
19 {
20 private CogImageFileTool m_ImageFileTool;
21 private Thread ThreadObject; //线程
22 private bool ThreadStop = false;
23
24 public Form1()
25 {
26 InitializeComponent();
27
28 //线程对象实例化
29 ThreadObject = new Thread(new ThreadStart(ThreadFunction));
30
31 //打开相机
32 OpenCameraSoftTrigger();
33
34
35 }
36
37 private void DisplayImage(object sender, EventArgs e)
38 {
39
40 if (ThreadObject.ThreadState == System.Threading.ThreadState.Unstarted)
41 {
42 ThreadObject.Start();
43
44 }
45
46 }
47
48
49 private void StopPlay_button_Click(object sender, EventArgs e)
50 {
51 ThreadStop = true;
52
53 //停止采集图像
54 Pylon.DeviceClose(hDev);
55 Pylon.DestroyDevice(hDev);
56 }
57
58
59 //线程回调函数
60 public void ThreadFunction()
61 {
62 int ImageWidth = 1280;
63 int ImageHeight = 1024;
64 CogImage8Grey Image = new CogImage8Grey();
65 var cogRoot = new CogImage8Root();
66 IntPtr ImageBufferPtr = Marshal.AllocHGlobal(ImageWidth * ImageHeight);
67 byte[] ImageBuffer = new byte[ImageWidth * ImageHeight];
68 while (!ThreadStop)
69 {
70 //采集单张图像
71 SnapAcquisitionSoftTrigger(ref ImageBuffer);
72
73 //将图像数据从托管区拷贝到非托管区
74 Marshal.Copy(ImageBuffer, 0, ImageBufferPtr, ImageWidth * ImageHeight);
75
76 //初始化
77 cogRoot.Initialize(ImageWidth, ImageHeight, ImageBufferPtr, ImageWidth, null);
78
79 //指定Image图像数据为cogRoot
80 Image.SetRoot(cogRoot);
81
82 //将图像数据传给cogRecordDisplay1控件
83 cogRecordDisplay1.Image = Image as CogImage8Grey;
84
85 //显示图像
86 cogRecordDisplay1.Fit(true);
87
88 }
89 }
90
91
92 ///balser sdk/
93 private PYLON_DEVICE_HANDLE hDev = new PYLON_DEVICE_HANDLE(); /* Handle for the pylon device. */
94 private uint numDevices; /* Number of available devices. */
95 private const int numGrabs = 1000; /* Number of images to grab. */
96 private PylonBuffer<Byte> imgBuf = null; /* Buffer used for grabbing. */
97
98 //打开相机
99 public void OpenCameraSoftTrigger()
100 {
101 bool isAvail;
102
103 Pylon.Initialize();
104
105 /* Enumerate all camera devices. You must call PylonEnumerateDevices() before creating a device. */
106 numDevices = Pylon.EnumerateDevices();
107
108 if (0 == numDevices)
109 {
110 MessageBox.Show("没有找到相机");
111 return;
112 }
113 else
114 {
115 /* Get a handle for the first device found. */
116 hDev = Pylon.CreateDeviceByIndex(0);
117 }
118
119 /* Before using the device, it must be opened. Open it for configuring parameters and for grabbing images. */
120 Pylon.DeviceOpen(hDev, Pylon.cPylonAccessModeControl | Pylon.cPylonAccessModeStream);
121
122 /* Set the pixel format to Mono8, where gray values will be output as 8 bit values for each pixel. */
123 /* ... Check first to see if the device supports the Mono8 format. */
124 isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_PixelFormat_Mono8");
125
126 if (!isAvail)
127 {
128 /* Feature is not available. */
129 MessageBox.Show("设备不支持8位灰度图像");
130 }
131
132 /* ... Set the pixel format to Mono8. */
133 Pylon.DeviceFeatureFromString(hDev, "PixelFormat", "Mono8");
134
135 /* Disable acquisition start trigger if available. */
136 isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_AcquisitionStart");
137 if (isAvail)
138 {
139 Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "AcquisitionStart");
140 Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
141 }
142
143
144 /* Disable frame burst start trigger if available */
145 isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_FrameBurstStart");
146 if (isAvail)
147 {
148 Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "FrameBurstStart");
149 Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
150 }
151
152 /* Disable frame start trigger if available */
153 isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_FrameStart");
154 if (isAvail)
155 {
156 Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "FrameStart");
157 Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
158 }
159
160 /* For GigE cameras, we recommend increasing the packet size for better
161 performance. If the network adapter supports jumbo frames, set the packet
162 size to a value > 1500, e.g., to 8192. In this sample, we only set the packet size
163 to 1500. */
164 /* ... Check first to see if the GigE camera packet size parameter is supported and if it is writable. */
165 isAvail = Pylon.DeviceFeatureIsWritable(hDev, "GevSCPSPacketSize");
166
167 if (isAvail)
168 {
169 /* ... The device supports the packet size feature. Set a value. */
170 Pylon.DeviceSetIntegerFeature(hDev, "GevSCPSPacketSize", 1500);
171 }
172
173 }
174
175
176 //采集单张图像
177 public void SnapAcquisitionSoftTrigger(ref byte[] ImageBufferPtr)
178 {
179
180 PylonGrabResult_t grabResult;
181
182 /* Grab one single frame from stream channel 0. The
183 camera is set to "single frame" acquisition mode.
184 Wait up to 500 ms for the image to be grabbed.
185 If imgBuf is null a buffer is automatically created with the right size.*/
186 Pylon.DeviceGrabSingleFrame(hDev, 0, ref imgBuf, out grabResult, 500);
187 IntPtr dataAddress = imgBuf.Pointer;
188
189 /* Check to see if the image was grabbed successfully. */
190 if (grabResult.Status == EPylonGrabStatus.Grabbed)
191 {
192 Marshal.Copy(dataAddress, ImageBufferPtr, 0, 1280 * 1024 - 1);
193
194 }
195 else
196 {
197 Console.WriteLine("图像抓取失败!n");
198 }
199
200 }
201
202 }
203 }