C# Basler相机接口二次封装与调用

介绍

对Basler相机库进行二次封装,封装内容为4部分:初始化相机调用、相机曝光参数、相机捕捉参数、输出结果(图片长、宽、byte流数据)。

备注:使用前需引用Basler.Pylon库文件

源代码(含举例)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
using Basler.Pylon;

namespace qyl
{
    public class Program
    {
        static void Main()
        {
            CameraAction[] camera = new CameraAction[2];  //2个相机
            if (camera[0].camera_Initial(camsn[0]))
                {
                    camera[0].camera_ExpoChange(camex[0]);
                    //MessageBox.Show(string.Format("{0}打开成功", camemg[0]), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Console.WriteLine("{0}打开成功", camemg[0]);
                    if (camera[0].camera_Grabing())
                    {
                        Console.WriteLine("拍照成功");
                        camera[0].sizew;  //int图片宽度
                        camera[0].sizeh;  //int图片高度
                        camera[0].bufferdata;  //byte[]

                    }
                    camera[0].camera_Close();
                    
                }
        }

    }
    public class CameraAction
    {
        #region 变量
        private Camera camera = null;
        private IGrabResult GrabResult = null;
        public int sizew { get; set; }
        public int sizeh { get; set; }
        public byte[] bufferdata { get; set; }

        public bool camisinited { get; set; }
        #endregion

        public bool camera_Initial(string serialno)
        {
            try
            {
                camisinited = false;
                camera = new Camera(serialno);
                camera.CameraOpened += Configuration.SoftwareTrigger;
                camera.Open();
                camera.Parameters[PLTransportLayer.HeartbeatTimeout].SetValue(1000, IntegerValueCorrection.Nearest);
                camera.StreamGrabber.Start(GrabStrategy.OneByOne, GrabLoop.ProvidedByUser);
                camisinited = true;
                return true;
            }
            catch (Exception)
            {
                if (camera != null)
                {
                    camera.CameraOpened -= Configuration.SoftwareTrigger;
                    if (camera.IsOpen)
                    {
                        if (camera.StreamGrabber.IsGrabbing)
                        {
                            camera.StreamGrabber.Stop();
                        }
                    }
                    camera.Close();
                    camera = null;
                }
                camisinited = false;
                return false;
            }

        }

        public void camera_ExpoChange(double exposure)
        {
            camera.Parameters[PLCamera.ExposureTimeAbs].SetValue(exposure * 1000);
        }

        public bool camera_Grabing()
        {
            if (camera.WaitForFrameTriggerReady(100, TimeoutHandling.ThrowException))
            {
                camera.ExecuteSoftwareTrigger();
            }
            GrabResult = camera.StreamGrabber.RetrieveResult(1000, TimeoutHandling.ThrowException);
            //GrabResult = camera.StreamGrabber.GrabOne(1000);
            if (GrabResult.GrabSucceeded)
            {
                sizew = GrabResult.Width;
                sizeh = GrabResult.Height;
                bufferdata = GrabResult.PixelData as byte[];
                GrabResult.Dispose();
                return true;
            }
            else
            {
                return false;
            }
        }

        public void camera_Close()
        {
            camera.StreamGrabber.Stop();
            camera.Close();
            camisinited = false;
        }
    }
}