C# + Basler相机采集图像DLL

我的未来我决定 提交于 2020-04-08 03:56:08

文章转自https://blog.csdn.net/qq_39930578/article/details/87897257

在此做个备份,代码如下:

using Basler.Pylon;
using System;
using System.Drawing;
using System.Drawing.Imaging;
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)
        {
            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();
        }
    }
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!