1,安装basler相机的驱动程序,选择安装pylonc.net
2,创建c#程序,项目添加引用pylonc.net.dll动态库
3,最简单使用gige千兆网口basler工业相机
a,using PylonC.NET;
b, //声明basler相机变量
PYLON_DEVICE_HANDLE hDev = null;
PylonBuffer
PylonGrabResult_t grabResult;
c, /* This is a special debug setting needed only for GigE cameras.
See 'Building Applications with pylon' in the Programmer's Guide. */
//初始化相机
#if DEBUG
Environment.SetEnvironmentVariable("PYLON_GIGE_HEARTBEAT", "300000" /*ms*/);
#endif
Pylon.Initialize();//初始化相机
Thread.Sleep(1000);
//GigE basler相机
hDev = new PYLON_DEVICE_HANDLE();
#region Bsler相机
uint numDevices; /*有效设备数 */
numDevices = Pylon.EnumerateDevices();
if (0 == numDevices)
{
throw new Exception("No devices found.");//没有相机
}
else
try{
bool isAvail;
//以下只使用一个相机,其index=0,只针对黑白相机 the Mono8 pixel format
hDev = Pylon.CreateDeviceByIndex(0);
Pylon.DeviceOpen(hDev, Pylon.cPylonAccessModeControl
| Pylon.cPylonAccessModeStream);
isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_PixelFormat_Mono8");
if (!isAvail)
{
/* Feature is not available. */
throw new Exception("Device doesn't support the Mono8 pixel format.");
}
Pylon.DeviceFeatureFromString(hDev, "PixelFormat", "Mono8");
isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_AcquisitionStart");
if (isAvail)
{
Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "AcquisitionStart");
Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
}
isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_FrameBurstStart");
if (isAvail)
{
Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "FrameBurstStart");
Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
}
isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_FrameStart");
if (isAvail)
{
Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "FrameStart");
Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
}
isAvail = Pylon.DeviceFeatureIsWritable(hDev, "GevSCPSPacketSize");
if (isAvail)
{
/* ... The device supports the packet size feature. Set a value. */
Pylon.DeviceSetIntegerFeature(hDev, "GevSCPSPacketSize", 1500);
}
Pylon.DeviceGrabSingleFrame(hDev, 0, ref imgBuf, out grabResult, 500);
//此处只针对80万,200万,500万相机
if (grabResult.SizeX <= 1074 && grabResult.SizeX >= 1024)
{//相机像素略大于80万情况,只取80万
pictureBox1.Size = new Size(512, 384);
}
else if (grabResult.SizeX <= 1650 && grabResult.SizeX >= 1600)
{//相机像素略大于200万情况,只取200万
pictureBox1.Size = new Size(400, 300);
}
else if (grabResult.SizeX <= 2498 && grabResult.SizeX >= 2448)
{//相机像素略大于500万情况,只取500万
pictureBox1.Size = new Size(612, 512);
} //另一种500万相机,未列出,分辨率1944*2592
}
catch (Exception)
{
}
#endregion Bsler相机
d,//开启相机,
timer1.Start();
//取像并显示出来
private void timer1_Tick(object sender, EventArgs e)
{
int ww = Convert.ToInt32(inputW.Text);
int hh = Convert.ToInt32(inputH.Text);
try
{
Pylon.DeviceGrabSingleFrame(hDev, 0, ref imgBuf, out grabResult, 500);
int bytes = ww * hh * 3;
byte[] bufercopy = new byte[bytes];
Bitmap cutPic24 = new Bitmap(ww, hh,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
BitmapData _cutPic = cutPic24.LockBits(new Rectangle(0, 0, ww, hh),
ImageLockMode.ReadWrite, cutPic24.PixelFormat);
IntPtr ptr = _cutPic.Scan0;//得到首地址
int yy = (grabResult.SizeY - hh) / 2;//多余不取
int xx = (grabResult.SizeX - ww) / 2;//多余不取
for (int ii = yy; ii < yy + hh; ii++)//图像复原,即由8位图变成24位图像
{
for (int j = xx; j < xx + ww; j++)
{
int n = ii * grabResult.SizeX + j;
int k = (ii - yy) * ww + j - xx;
int m = 3 * k;
bufercopy[m] = imgBuf.Array[n];
bufercopy[m + 1] = imgBuf.Array[n];
bufercopy[m + 2] = imgBuf.Array[n];
}
}
//把cutvalues数组给ptr
System.Runtime.InteropServices.Marshal.Copy(bufercopy, 0, ptr, ww * hh * 3);
cutPic24.UnlockBits(_cutPic);
if (ww == 1600)
{
pictureBox1.Size = new System.Drawing.Size(400, 300);
}
if (ww == 1024)
{
pictureBox1.Size = new System.Drawing.Size(512, 384);
}
if (ww == 2448)
{
pictureBox1.Size = new System.Drawing.Size(612, 512);
}
pictureBox1.Image = cutPic24;
}
catch (Exception)
{
}
}
e,关闭相机
try {
if (hDev.IsValid)//异常,先释放句柄
{
/* ... Close and release the pylon device. */
if (Pylon.DeviceIsOpen(hDev))
{
Pylon.DeviceClose(hDev);
}
Pylon.DestroyDevice(hDev);
}
}
catch (Exception)
{
/*No further handling here.*/
}
Pylon.Terminate();//其次,终结相机