C# + Basler相機采集圖像DLL


文章轉自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();
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM