C# + Basler相机采集图像DLL

时间:2024-05-18 16:43:50

以下是我用c#写的basler相机的采集图像的程序,分享给大家。

分两个部分,第一部分是DLL内容,第二部分是调用。

第一部分:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
namespace CameraForms
{
    public class baslerCamcs
    { 
        //相机连接的个数
        public int CameraNumber = CameraFinder.Enumerate().Count();

        //委托+事件 = 回调函数,用于传递相机抓取的图像
        public delegate void CameraImage(Bitmap bmp);
        public event CameraImage CameraImageEvent;
        
        //放出一个Camera
        Camera camera;

        //basler里用于将相机采集的图像转换成位图
        PixelDataConverter pxConvert = new PixelDataConverter();

        //控制相机采集图像的过程
        bool GrabOver = false;


        //相机初始化
        public void CameraInit()
        {
             camera = new Camera();
            //*运行模式
            camera.CameraOpened += Configuration.AcquireContinuous;

            //断开连接事件
            camera.ConnectionLost += Camera_ConnectionLost; 
            
            //抓取开始事件
            camera.StreamGrabber.GrabStarted += StreamGrabber_GrabStarted;

            //抓取图片事件
            camera.StreamGrabber.ImageGrabbed += StreamGrabber_ImageGrabbed;

            //结束抓取事件
            camera.StreamGrabber.GrabStopped += StreamGrabber_GrabStopped;

            //打开相机
            camera.Open();
        }
        private void StreamGrabber_GrabStarted(object sender, EventArgs e)
        {
            GrabOver = true;
        }
        private void StreamGrabber_ImageGrabbed(object sender, ImageGrabbedEventArgs e)
        {
            IGrabResult grabResult = e.GrabResult;
            if(grabResult.IsValid)
            {
                if (GrabOver)
                    CameraImageEvent(GrabResult2Bmp(grabResult));
            }
        }

        private void StreamGrabber_GrabStopped(object sender, GrabStopEventArgs e)
        {
            GrabOver = false;
        }

        private void Camera_ConnectionLost(object sender, EventArgs e)
        {
            camera.StreamGrabber.Stop();
            DestroyCamera();
        }

        public void OneShot()
        {
            if(camera != null)
            {
                camera.Parameters[PLCamera.AcquisitionMode].SetValue(PLCamera.AcquisitionMode.SingleFrame);
                camera.StreamGrabber.Start(1,GrabStrategy.OneByOne, GrabLoop.ProvidedByStreamGrabber);
            }
        }

        public void KeepShot()
        {
            if (camera != null)
            {
                camera.Parameters[PLCamera.AcquisitionMode].SetValue(PLCamera.AcquisitionMode.Continuous);
                camera.StreamGrabber.Start(GrabStrategy.OneByOne, GrabLoop.ProvidedByStreamGrabber);
            }
        }

        public void Stop()
        {
            if (camera != null)
            {
                camera.StreamGrabber.Stop();
            }
        }

        //将相机抓取到的图像转换成Bitmap位图
        Bitmap GrabResult2Bmp(IGrabResult grabResult)
        {
            //using (Bitmap b = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format32bppRgb))
            //{
            Bitmap b = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format32bppRgb);
                BitmapData bmpData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, b.PixelFormat);
                pxConvert.OutputPixelFormat = PixelType.BGRA8packed;
                IntPtr bmpIntpr = bmpData.Scan0;
                pxConvert.Convert(bmpIntpr, bmpData.Stride * b.Height, grabResult);
                b.UnlockBits(bmpData);
                return b;
            //}
        }

        //释放相机
        public void DestroyCamera()
        {
            if(camera !=null)
            {
                camera.Close();
                camera.Dispose();
                camera = null;
            }
        }
    }
}

第二部分:

using System;
using System.Drawing;
using System.Windows.Forms;
namespace CameraForms
{
    public partial class MainForm : Form
    {
        baslerCamcs camera = new baslerCamcs();
        public MainForm()
        {
            InitializeComponent();
            camera.CameraImageEvent += Camera_CameraImageEvent;
        }

        private void Camera_CameraImageEvent(Bitmap bmp)
        {
            pictureBox1.Invoke(new MethodInvoker(delegate
            {
                Bitmap old = pictureBox1.Image as Bitmap;
                pictureBox1.Image = bmp;
                if (old != null)
                    old.Dispose();
            }));
        }

        void Unanble()
        {
            button_stop.Enabled = false;
            button_Oneshot.Enabled = false;
            button_keepShot.Enabled = false;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            if (camera.CameraNumber > 0)
                camera.CameraInit();
            else
            {
                MessageBox.Show("未连接到相机");
                Unanble();
            }
        }
        //停止
        private void button_stop_Click(object sender, EventArgs e)
        {
             camera.Stop();
        }
        //拍一张
        private void button_Oneshot_Click(object sender, EventArgs e)
        {
            camera.OneShot();
        }
        //连续拍
        private void button_keepShot_Click(object sender, EventArgs e)
        {
            camera.KeepShot();  
        }
        //关闭窗体,释放相机
        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            camera.DestroyCamera();
        }
    }
}

效果图:

C# + Basler相机采集图像DLL

2019_02_23;