Basler SDK取像visionpro显示

Basler自带SDK修改的类

class MyBaslar
    {
        private Camera camera = null;
        public delegate void ImageEventHandler(object sender, ImageEventArgs e);
        public event ImageEventHandler ImageEvent;
        private PixelDataConverter converter = new PixelDataConverter();

        CogImage8Grey img;

        public Camera Camera
        {
            get { return camera; }
            set { camera = value; }
        }
        public MyBaslar(string cameraSN)
        {
            camera = new Camera(cameraSN);
            camera.CameraOpened += Configuration.AcquireContinuous;
            camera.StreamGrabber.ImageGrabbed += OnImageGrabbed;
            camera.Open();
        }
        protected void OnImageEvent(CogImage8Grey image8Grey)
        {
            ImageEventArgs imageEventargs = new ImageEventArgs(image8Grey);
            if (ImageEvent != null)
                ImageEvent(this, imageEventargs);
        }
        public void OneShot()
        {
            try
            {
                // Starts the grabbing of one image.
                camera.Parameters[PLCamera.AcquisitionMode].SetValue(PLCamera.AcquisitionMode.SingleFrame);
                camera.StreamGrabber.Start(1, GrabStrategy.OneByOne, GrabLoop.ProvidedByStreamGrabber);
            }
            catch (Exception)
            { }
        }
        public void ContinuousShot()
        {
            try
            {
                // Start the grabbing of images until grabbing is stopped.
                camera.Parameters[PLCamera.AcquisitionMode].SetValue(PLCamera.AcquisitionMode.Continuous);
                camera.StreamGrabber.Start(GrabStrategy.OneByOne, GrabLoop.ProvidedByStreamGrabber);
            }
            catch (Exception)
            {
                //
            }
        }
        public void Stop()
        {
            // Stop the grabbing.
            try
            {
                camera.StreamGrabber.Stop();
            }
            catch (Exception)
            {
                //
            }
        }
        public void SetCameraParams(double exposure, double gain)
        {
            if (camera.Parameters.Contains(PLCamera.Gain))
            {
                camera.Parameters[PLCamera.Gain].SetValue(gain);
            }
            else
                camera.Parameters[PLCamera.GainAbs].SetValue(gain);
            if (camera.Parameters.Contains(PLCamera.ExposureTimeAbs))
                camera.Parameters[PLCamera.ExposureTimeAbs].SetValue(exposure);
            else
                camera.Parameters[PLCamera.ExposureTime].SetValue(exposure);
        }


        private void OnImageGrabbed(Object sender, ImageGrabbedEventArgs e)
        {
            if (e.GrabResult.GrabSucceeded)
            {

                byte[] pixelData = e.GrabResult.PixelData as byte[];
                IntPtr pixelPointer = Marshal.UnsafeAddrOfPinnedArrayElement(pixelData, 0);

                Bitmap bitmap = new Bitmap(e.GrabResult.Width, e.GrabResult.Height, PixelFormat.Format32bppRgb);
                // Lock the bits of the bitmap.
                BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
                // Place the pointer to the buffer of the bitmap.
                converter.OutputPixelFormat = PixelType.BGRA8packed;
                IntPtr ptrBmp = bmpData.Scan0;
                converter.Convert(ptrBmp, bmpData.Stride * bitmap.Height, e.GrabResult);
                bitmap.UnlockBits(bmpData);

                img = new CogImage8Grey(bitmap);//类型转换,将bitmap类型转换成CogImage8Grey类型

                OnImageEvent(img);//使用事件将图片传出,方便外部使用
            }

            GC.Collect();

        }
    }
    public class ImageEventArgs
    {
        public CogImage8Grey cogImage;
        public ImageEventArgs(CogImage8Grey cogImage8)
        {
            cogImage = cogImage8;
        }
    }```



外部引用这个类来完成取像显示


namespace BaslerVPGrab
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        MyBaslar cam;
        int a = 0;

        private void Form1_Load(object sender, EventArgs e)
        {

            cam = new MyBaslar("23685942");//根据相机S/n来加载相机

            cam.ImageEvent += cam_ImageEvent;//注册在类中写的事件

            cam.Camera.Parameters[PLCamera.TriggerMode].TrySetValue("Off");//设置触发模式为内触发
            cam.Camera.Parameters[PLCamera.TriggerSource].TrySetValue("Line1");
            cam.Camera.Parameters[PLCamera.TriggerActivation].TrySetValue("RisingEdge");

            Control.CheckForIllegalCrossThreadCalls = false;

        }

        void cam_ImageEvent(object sender, ImageEventArgs e)
        {
            cogRecordDisplay1.Image = e.cogImage;

            Task t = new Task(() =>
            {
                a += 1;
                label1.Text = a.ToString();
            });
            t.Start();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            cam.ContinuousShot();//连续触发
        }

        private void button2_Click(object sender, EventArgs e)
        {
            cam.Stop();//停止采集
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            cam.Camera.Dispose();//相机释放
        }

        private void button3_Click(object sender, EventArgs e)
        {
            cam.OneShot();//触发一次
        }


    }
}```

界面样子
![](https://img2020.cnblogs.com/blog/2156580/202108/2156580-20210813102435549-1255115592.png)

Basler SDK取像visionpro显示

上一篇:node + express 发送阿里云短信


下一篇:[PowerCLI]命令行管理ESXi-2.查看ESXi虚拟机