其他分享
首页 > 其他分享> > 大华相机(华睿科技)SDK取像(halcon版)

大华相机(华睿科技)SDK取像(halcon版)

作者:互联网

根据官方demo修改出来的类

 public class DaHuaCam
    {
        List<IGrabbedRawData> m_frameList;        // 图像缓存列表 | frame data list 
        Mutex m_mutex = new Mutex();        // 锁,保证多线程安全 | mutex 
        private IDevice m_dev;
        List<IDeviceInfo> li;
        public delegate void ImageEventHandler(object sender, ImageEventArgs e);
        public event ImageEventHandler ImageEvent;

        public DaHuaCam()
        {
            m_frameList = new List<IGrabbedRawData>();
            li = Enumerator.EnumerateDevices();
        }

        protected void OnImageEvent(HObject image8Grey)
        {
            ImageEventArgs imageEventargs = new ImageEventArgs(image8Grey);
            ImageEvent?.Invoke(this, imageEventargs);
        }

        /// <summary>
        /// 打开相机
        /// </summary>
        /// <param name="index"></param>
        public void OpenCamera(int index)
        {
            if (li.Count > 0)
            {
                m_dev = Enumerator.GetDeviceByIndex(index);
            }

            if (m_dev == null)
            {
                return;
            }

            m_dev.CameraOpened += OnCameraOpen;
            m_dev.ConnectionLost += OnConnectLoss;
            m_dev.CameraClosed += OnCameraClose;

            // 打开设备 ,调用Open方法打开相机
            // open device 
            if (!m_dev.Open())
            {
                MessageBox.Show("Open camera failed");
                return;
            }

            // 设置缓存个数为8(默认值为16) 
            m_dev.StreamGrabber.SetBufferCount(8);

            //注册相机取流回调事件
            m_dev.StreamGrabber.ImageGrabbed += OnImageGrabbed;

        }

        /// <summary>
        /// 是否开启触发模式
        /// </summary>
        /// <param name="fON">true为外触发,false为实时状态</param>
        public void SetTrigger(bool fON)
        {
            if (fON)
            {
                //Open为开启触发模式,设置Line1时,triggermode自动改为On,不用手动写入
                m_dev.TriggerSet.Open(TriggerSourceEnum.Line1);
            }
            else
            {
                //Close为关闭触发模式,此时,triggermode被改为Off
                m_dev.TriggerSet.Close();
            }
        }

        /// <summary>
        /// 设置相机曝光
        /// </summary>
        /// <param name="exposuretime"></param>
        public void SetExposureTime(int exposuretime)
        {
            using (IFloatParameter p = m_dev.ParameterCollection[ParametrizeNameSet.ExposureTime])
            {
                p.SetValue(exposuretime);
            }
        }

        /// <summary>
        /// 设置相机增益
        /// </summary>
        /// <param name="gain"></param>
        public void SetGain(double gain)
        {
            using (IFloatParameter p = m_dev.ParameterCollection[ParametrizeNameSet.GainRaw])
            {
                p.SetValue(gain);
            }
        }

        /// <summary>
        /// 开始取流
        /// </summary>
        public void StartGrabImage()
        {
            if (m_dev == null)
            {
                throw new InvalidOperationException("Start Grab faild");
            }
            else
                m_dev.StreamGrabber.Start();
        }

        /// <summary>
        /// 停止取流
        /// </summary>
        public void StopGrabImage()
        {
            try
            {
                if (m_dev == null)
                {
                    throw new InvalidOperationException("Device is invalid");
                }
                else
                    m_dev.StreamGrabber.Stop();
            }
            catch (Exception exception)
            {
                Catcher.Show(exception);
            }
        }

        /// <summary>
        /// 释放相机
        /// </summary>
        public void ReleaseCamera()
        {
            try
            {
                if (m_dev == null)
                {
                    throw new InvalidOperationException("Device is invalid");
                }

                m_dev.StreamGrabber.ImageGrabbed -= OnImageGrabbed;  // 反注册回调 | unregister grab event callback 
                m_dev.ShutdownGrab(); // 停止码流 | stop grabbing 
                m_dev.Close();  // 关闭相机 | close camera 
            }
            catch (Exception ex)
            {
                Catcher.Show(ex);
            }
        }

        // 相机打开回调 
        // camera open event callback 
        private void OnCameraOpen(object sender, EventArgs e)
        {

        }

        // 相机关闭回调 
        // camera close event callback 
        private void OnCameraClose(object sender, EventArgs e)
        {

        }

        private void OnConnectLoss(object sender, EventArgs e)
        {
            m_dev.ShutdownGrab();
            m_dev.Dispose();
            m_dev = null;
        }

        // 码流数据回调 
        // grab callback function 
        private void OnImageGrabbed(Object sender, GrabbedEventArgs e)
        {
            m_mutex.WaitOne();
            m_frameList.Add(e.GrabResult.Clone());
            m_mutex.ReleaseMutex();


            if (m_frameList.Count == 0)
            {
                Thread.Sleep(10);
                return;
            }

            // 图像队列取最新帧 
            // always get the latest frame in list 
            m_mutex.WaitOne();
            IGrabbedRawData frame = m_frameList.ElementAt(m_frameList.Count - 1);
            m_frameList.Clear();
            m_mutex.ReleaseMutex();

            // 主动调用回收垃圾 
            // call garbage collection 
            GC.Collect();

            HObject himg;

            try
            {
                // 图像转码成bitmap图像 
                // raw frame data converted to bitmap 
                var bitmap = frame.ToBitmap(false);

                Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);

                BitmapData srcBmpData = bitmap.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);

                //bitmap转halcon
                HOperatorSet.GenImage1(out himg, "byte", bitmap.Width, bitmap.Height, srcBmpData.Scan0);

                //用事件传出,用于外部使用
                OnImageEvent(himg);

                himg.Dispose();

            }
            catch (Exception exception)
            {
                Catcher.Show(exception);
            }
        }
    }

    public class ImageEventArgs
    {
        public HObject image;
        public ImageEventArgs(HObject img)
        {
            image = img;
        }
    }

winform用法

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ThridLibray;

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

        DaHuaCam cam0 = new DaHuaCam();

        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;

            cam0.ImageEvent += Cam0_ImageEvent;

            cam0.OpenCamera(0);

            cam0.SetTrigger(true);

            cam0.SetExposureTime(10000);
            cam0.SetGain(5);
        }

        private void Cam0_ImageEvent(object sender, ImageEventArgs e)
        {
            hWindowControl1.HalconWindow.DispObj(e.image);
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            cam0.StartGrabImage();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            cam0.StopGrabImage();

        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            cam0.ReleaseCamera();
        }
    }
}

界面

标签:sender,void,dev,halcon,private,华睿,using,取像,public
来源: https://www.cnblogs.com/yuanqi99977/p/15939391.html