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
{
public partial class Form1 : Form
{
private CogImageFileTool m_ImageFileTool;
private Thread ThreadObject; //线程
private bool ThreadStop = false;
public Form1()
{
InitializeComponent();
//打开相机
OpenCameraSoftTrigger();
//线程对象实例化
ThreadObject = new Thread(new ThreadStart(ThreadFunction));
}
private void DisplayImage(object sender, EventArgs e)
{
if (ThreadObject.ThreadState == System.Threading.ThreadState.Unstarted)
{
ThreadObject.Start();
}
}
private void StopPlay_button_Click(object sender, EventArgs e)
{
ThreadStop = true;
//停止采集图像
Pylon.DeviceClose(hDev);
Pylon.DestroyDevice(hDev);
}
//线程回调函数
public void ThreadFunction()
{
int ImageWidth = 1280;
int ImageHeight = 1024;
CogImage8Grey Image = new CogImage8Grey();
var cogRoot = new CogImage8Root();
IntPtr ImageBufferPtr = Marshal.AllocHGlobal(ImageWidth * ImageHeight);
byte[] ImageBuffer = new byte[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 = Image as CogImage8Grey;
//显示图像
cogRecordDisplay1.Fit(true);
}
}
///////////////////////////////////////////////////balser sdk/////////////////////////////////////////////////////
private PYLON_DEVICE_HANDLE hDev = new PYLON_DEVICE_HANDLE(); /* Handle for the pylon device. */
private uint numDevices; /* Number of available devices. */
private const int numGrabs = 1000; /* Number of images to grab. */
private PylonBuffer<Byte> imgBuf = null; /* Buffer used for grabbing. */
//打开相机
public void OpenCameraSoftTrigger()
{
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);
}
}
//采集单张图像
public void SnapAcquisitionSoftTrigger(ref byte[] 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");
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
VS2015联合VisionPro源码

共658个文件
dll:226个
xml:143个
bmp:45个

需积分: 5 22 浏览量
2023-06-08
08:54:54
上传
评论
收藏 487.95MB RAR 举报
温馨提示
里面有大量的学习实例,适合新手入门,适合老手参考。
资源推荐
资源详情
资源评论










收起资源包目录





































































































共 658 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论


观天小蚁
- 粉丝: 13
- 资源: 95
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


安全验证
文档复制为VIP权益,开通VIP直接复制
