C
s
h
a
r
p
和
V
i
s
i
o
n
p
r
o
混
合
编
程
实
现
工
业
相
机
实
时
图
像
采
集
Csharp和Visionpro混合编程实现工业相机实时图像采集
Csharp和Visionpro混合编程实现工业相机实时图像采集
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using Cognex.VisionPro;using Cognex.VisionPro.ImageFile;using System.IO;using PylonC.NET;using System.Runtime.InteropServices;using System.Threading;namespace Demo
{publicpartialclassForm1:Form{privateCogImageFileTool m_ImageFileTool;privateThread ThreadObject;//线程privatebool ThreadStop =false;publicForm1(){InitializeComponent();//线程对象实例化
ThreadObject =newThread(newThreadStart(ThreadFunction));//打开相机OpenCameraSoftTrigger();}privatevoidDisplayImage(object sender,EventArgs e){if(ThreadObject.ThreadState == System.Threading.ThreadState.Unstarted){
ThreadObject.Start();}}privatevoidStopPlay_button_Click(object sender,EventArgs e){
ThreadStop =true;//停止采集图像
Pylon.DeviceClose(hDev);
Pylon.DestroyDevice(hDev);}//线程回调函数publicvoidThreadFunction(){int ImageWidth =1280;int ImageHeight =1024;CogImage8Grey Image =newCogImage8Grey();var cogRoot =newCogImage8Root();IntPtr ImageBufferPtr = Marshal.AllocHGlobal(ImageWidth * ImageHeight);byte[] ImageBuffer =newbyte[ImageWidth* ImageHeight];while(!ThreadStop){//采集单张图像SnapAcquisitionSoftTrigger(ref ImageBuffer);//将图像数据从托管区拷贝到非托管区
Marshal.Copy(ImageBuffer,0, ImageBufferPtr, ImageWidth * ImageHeight);//初始化
cogRoot.Initialize(ImageWidth, ImageHeight, ImageBufferPtr, ImageWidth,null);//指定Image图像数据为cogRoot
Image.SetRoot(cogRoot);//将图像数据传给cogRecordDisplay1控件
cogRecordDisplay1.Image =Imageas CogImage8Grey;//显示图像
cogRecordDisplay1.Fit(true);}}///balser sdk/privatePYLON_DEVICE_HANDLE hDev =newPYLON_DEVICE_HANDLE();/* Handle for the pylon device. */privateuint numDevices;/* Number of available devices. */privateconstint numGrabs =1000;/* Number of images to grab. */private PylonBuffer<Byte> imgBuf =null;/* Buffer used for grabbing. *///打开相机publicvoidOpenCameraSoftTrigger(){bool isAvail;
Pylon.Initialize();/* Enumerate all camera devices. You must call PylonEnumerateDevices() before creating a device. */
numDevices = Pylon.EnumerateDevices();if(0== numDevices){
MessageBox.Show("没有找到相机");return;}else{/* Get a handle for the first device found. */
hDev = Pylon.CreateDeviceByIndex(0);}/* Before using the device, it must be opened. Open it for configuring parameters and for grabbing images. */
Pylon.DeviceOpen(hDev, Pylon.cPylonAccessModeControl | Pylon.cPylonAccessModeStream);/* Set the pixel format to Mono8, where gray values will be output as 8 bit values for each pixel. *//* ... Check first to see if the device supports the Mono8 format. */
isAvail = Pylon.DeviceFeatureIsAvailable(hDev,"EnumEntry_PixelFormat_Mono8");if(!isAvail){/* Feature is not available. */
MessageBox.Show("设备不支持8位灰度图像");}/* ... Set the pixel format to Mono8. */
Pylon.DeviceFeatureFromString(hDev,"PixelFormat","Mono8");/* Disable acquisition start trigger if available. */
isAvail = Pylon.DeviceFeatureIsAvailable(hDev,"EnumEntry_TriggerSelector_AcquisitionStart");if(isAvail){
Pylon.DeviceFeatureFromString(hDev,"TriggerSelector","AcquisitionStart");
Pylon.DeviceFeatureFromString(hDev,"TriggerMode","Off");}/* Disable frame burst start trigger if available */
isAvail = Pylon.DeviceFeatureIsAvailable(hDev,"EnumEntry_TriggerSelector_FrameBurstStart");if(isAvail){
Pylon.DeviceFeatureFromString(hDev,"TriggerSelector","FrameBurstStart");
Pylon.DeviceFeatureFromString(hDev,"TriggerMode","Off");}/* Disable frame start trigger if available */
isAvail = Pylon.DeviceFeatureIsAvailable(hDev,"EnumEntry_TriggerSelector_FrameStart");if(isAvail){
Pylon.DeviceFeatureFromString(hDev,"TriggerSelector","FrameStart");
Pylon.DeviceFeatureFromString(hDev,"TriggerMode","Off");}/* For GigE cameras, we recommend increasing the packet size for better
performance. If the network adapter supports jumbo frames, set the packet
size to a value > 1500, e.g., to 8192. In this sample, we only set the packet size
to 1500. *//* ... Check first to see if the GigE camera packet size parameter is supported and if it is writable. */
isAvail = Pylon.DeviceFeatureIsWritable(hDev,"GevSCPSPacketSize");if(isAvail){/* ... The device supports the packet size feature. Set a value. */
Pylon.DeviceSetIntegerFeature(hDev,"GevSCPSPacketSize",1500);}}//采集单张图像publicvoidSnapAcquisitionSoftTrigger(refbyte[] ImageBufferPtr){PylonGrabResult_t grabResult;/* Grab one single frame from stream channel 0. The
camera is set to "single frame" acquisition mode.
Wait up to 500 ms for the image to be grabbed.
If imgBuf is null a buffer is automatically created with the right size.*/
Pylon.DeviceGrabSingleFrame(hDev,0,ref imgBuf,out grabResult,500);IntPtr dataAddress = imgBuf.Pointer;/* Check to see if the image was grabbed successfully. */if(grabResult.Status == EPylonGrabStatus.Grabbed){
Marshal.Copy(dataAddress, ImageBufferPtr,0,1280*1024-1);}else{
Console.WriteLine("图像抓取失败!n");}}}}